Structured Outputs ⚠ verify
Definition
Structured Outputs use constrained decoding to guarantee model output conforms to a JSON Schema — via output_config.format with {type:'json_schema', schema} for message output, and Strict Tool Use (strict: true) for tool inputs.
Key points
- Set
output_config.format={"type": "json_schema", "schema": {...}}. The param moved fromoutput_format→output_config.format(old name works during a transition period). ⚠ verify (2026-07-08) additionalProperties: falseis required on every object (both JSON outputs and strict tool schemas).- Constrained decoding compiles the schema into a grammar that constrains output token-by-token → always valid (no
JSON.parse()errors), type-safe, no retries needed for schema violations. - SDK helper
client.messages.parse()deserializes into Pydantic / Zod models. - Supported: object/array/string/integer/number/boolean/null;
enum(strings/numbers/bools/nulls only) andconst;anyOf/allOf(allOfwith$refnot supported);$ref/$def/definitions;default;required; string formats (date-time,date,email,uri,uuid, ...);minItemsonly 0 or 1; regex quantifiers/character-classes/groups. - NOT supported (→ 400 with details): recursive schemas; complex types in enums; external
$ref(e.g.http://...); numeric constraints (minimum/maximum/multipleOf); string length (minLength/maxLength); array constraints beyondminItems0/1;additionalPropertiesset to anything butfalse; regex backreferences/lookaround/word-boundaries. - Grammar caching: first request adds compile latency; compiled grammars cached 24h from last use; invalidated when schema structure or tool set changes; changing only
name/descriptiondoes NOT invalidate. Changingoutput_config.formatinvalidates the prompt cache for that thread. - Combine with Strict Tool Use (same constrained-decoding family).
Why it matters for the exam
- The mandatory
additionalProperties: falseand the supported-vs-unsupported schema feature list are heavily tested in Structured Data Extraction scenarios. - "Constrained decoding eliminates syntax errors" is the key link to Validation-Retry Loops.
Common gotchas
- Forgetting
additionalProperties: falseon any nested object is the classic trap. - Reaching for
minimum/maxLength/recursive schemas — all unsupported, all 400. - Structured outputs kill syntax errors but not semantic ones (still need validation) — see Validation-Retry Loops.
See also
Sources
Referenced by
- Building Evals
- CI CD Headless Flags
- Claude Code Configuration & Workflows
- Few-Shot Prompting
- Hook Exit Codes And JSON Output
- Message Batches API
- Prefill Migration
- Prompt Engineering & Structured Output
- Prompt Technique Taxonomy
- prompting-01
- Resilient schema design
- Strict Tool Use
- Token-Efficient Tool Use
- Tool Definition Schema
- Validation-Retry Loops
- XML Structuring
Practice questions optional · AI
Generate fresh practice questions about this concept with AI. These are not vault-verified.