JSON the Model Wrapped in Prose
Why it matters Ask a model for JSON without structured outputs and you will get JSON most of the time, but often with a friendly sentence around it, a code fence, or nothing usable at all. Code that calls json.loads on the raw text breaks on day one.
Five replies to "give me these fields as JSON", each a pattern real models produce:
- clean JSON;
- JSON inside a
```jsonfence with sentences before and after; - JSON in the middle of a sentence;
- a list that was cut off at the token limit (
stop_reasonis"max_tokens"), so the JSON is broken; - no JSON at all: the model explained why it could not do it.
The robust reader: first check stop_reason (a cut-off reply cannot be trusted even if it happens to parse), then take the text, cut out the span from the first { or [ to the matching last } or ], and parse that. Anything that still fails is an error to report, never something to guess at. (In production, structured outputs constrain the model to valid JSON and remove most of this; you still check stop_reason.)
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.
Write parse_json_reply(reply). Raise ValueError if stop_reason is "max_tokens". Otherwise join the text blocks, slice from the first { or [ (whichever comes first) to the last matching } or ], and return json.loads of that slice. Raise ValueError when there is no JSON or it does not parse. 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.
This browser is blocking cookies for learn.floati.life, so lessons you finished here cannot open in order. Allow cookies for this site and reload.