AI Structured Outputs Tutorial: JSON Mode, Constrained

TL;DRStructured outputs force a model to return valid JSON or a fixed schema — reliable for programs.

Free-text replies are hard to parse. Structured output modes (JSON mode, schema-constrained decoding) make the model emit output that conforms to a schema you define, so downstream code can rely on the shape. Implementations constrain token generation to valid structures. Essential whenever an LLM feeds another system — extraction, tool arguments, API responses — turning fuzzy generation into dependable data.

Key points

Common mistakes

Try it: Explain why structured output matters when an LLM feeds another system.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>schema: { name: string, age: number }
model must emit: {"name":"drew","age":30}
→ code parses reliably</pre></body></html>
Open the interactive lesson →
Context Caching · Cutting Cost Function Calling · Tools as Schemas