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
Constrain output to JSON / a schema
Downstream code can rely on the shape
Constrained decoding enforces validity
Essential for LLM→program pipelines
Common mistakes
Parsing free text with brittle regex
No schema → inconsistent fields
Assuming the model always returns valid JSON without constraints
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>