40. Advanced Structured Output
Overview and links for this section of the guide.
On this page
The Complexity Cliff
Getting a flat JSON object is easy. Getting a recursive tree of objects with polymorphic types (where `items` can be `TextItem` or `ImageItem`) is hard. Models often mess up nesting depth or forget required fields in deep children.
Expert Strategies
- Schema Decomposition: Don't ask for the whole tree. Ask for the top level, then ask for the children of Node A, then Node B. Assemble it in code.
- Grammar-Constrained Decoding: Using tools that force the model's output tokens to strictly adhere to a BNF grammar (some providers support this natively).
- Error Reflection: If the JSON fails to parse, feed the error message back to the model: "You generated invalid JSON. Error at line 5: ... Fix it."
Where to go next
Explore next
40. Advanced Structured Output sub-sections
5 pages
40.1 Deep schemas: nested objects and arrays
Open page
40.2 "Explain errors as JSON" for debuggability
Open page
40.3 Schema evolution and backwards compatibility
Open page
40.4 Internationalization schemas (multi-locale outputs)
Open page
40.5 Contract tests for schema compliance
Open page