Structured Output — JSON Schema, Pydantic, Zod, Constrained Decoding
"Ask the model nicely to return JSON" fails 5 to 15 percent of the time, even on frontier models. Structured outputs close that gap with constrained decoding: the model is literally prevented from emitting a token that would violate the schema. OpenAI's strict mode, Anthropic's schema-typed tool use, Gemini's responseSchema, Pydantic AI's outputtype, and Zod's .parse are five surface forms of the same idea. This lesson builds the schema validator and the strict-mode contract learners will use for every production extraction pipeline. Write a JSON Schema 2020-12 for an extraction target using the right constraints (enum, min/max, required, pattern). Explain why strict mode and constrained decoding give different guarantees from "validate after generation". Distinguish the three failure modes: parse error, schema violation, model refusal. Ship an extraction pipeline with typed repair and typed refusal handling. An agent reading a purchase-order email needs to turn free text into {customer, lineitems, totalusd}. Three approaches. Approach one: prompt for JSON. "Reply in JSON with fields customer, lineitems, totalusd." Works 85 to 95 percent of the time on frontier models. Fails in six ways: missing brace, trailing comma, wrong types, hallucinated fields, truncated at token limit, leaked prose like "Here is your JSON:". Approach two: validate after generation. Generate freely, parse, validate against schema, retry on failure. Reliable but expensive — you pay for…
Structured Output — JSON Schema, Pydantic, Zod, Constrained Decoding: "Ask the model nicely to return JSON" fails 5 to 15 percent of the time, even on…
This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.
Browse the complete course catalog or open this lesson on GitHub.