Syllabus Lesson 245 of 250 · Working with Model Replies
Working with Model Replies

Reading a Real Reply: Blocks and Stop Reasons

Why it matters Every bug in this module starts the same way: code that reads reply["content"][0]["text"] and assumes that is the answer. On a current model the first block is often not text at all.

A reply from the Claude Messages API is a dict. The answer lives in content, a list of blocks, and each block has a type:

  • "thinking": the model reasoned before answering. On current models this block often comes first, and by default its thinking text is empty (the reasoning happened, but is not shown).
  • "text": words for the user. There can be more than one text block; the answer is all of them joined.
  • "tool_use": the model wants you to run a tool (later lessons).

Next to the content sits stop_reason, which says why the model stopped: "end_turn" (finished), "max_tokens" (hit your length limit, so the answer may be cut off or even missing), "tool_use" (waiting for a tool result) or "refusal" (declined). There are a few more, such as "stop_sequence" and "pause_turn", so treat anything other than "end_turn" as not finished. Always check it before trusting the content.

{'id': 'msg_01',
 'type': 'message',
 'role': 'assistant',
 'model': 'claude-opus-5-5',
 'content': [{'type': 'thinking', 'thinking': '', 'signature': 'EuYBCkQYAiJA...'},
             {'type': 'text',
              'text': 'A context window is the amount of text a model can read at once.'}],
 'stop_reason': 'end_turn',
 'stop_sequence': None,
 'usage': {'input_tokens': 412, 'output_tokens': 60}}

About these replies: they are simulated. Each one was written for this course in exactly the shape the Claude Messages API returns (content blocks, stop_reason, usage), and each copies something real models do. They are not recordings of a live model, so the exact wording is ours; the shapes and the problems are the real ones your code will meet.

Your turn

Write reply_text(reply) returning all the text blocks joined in order (an empty string if there are none), skipping every other block type. Write finished(reply) returning True only when stop_reason is "end_turn". Keep the starter's REPLIES as it is: the check reads it.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.