What Is Structured Output? β Definition and How It Works
Learn what structured output is, how schema-constrained generation works, why it beats parsing prose, and how to handle validation failures.
Structured output is a model returning data that conforms to a declared schema, so downstream code can consume typed fields instead of parsing prose.
1. What It Is
A contract on the shape of the response.
- Key idea: the caller declares the shape; the model fills it.
- Mechanism: a JSON Schema supplied with the request, enforced during generation or validated after.
- Goal: make model output a dependable input to ordinary software.
Example Schema
{
"type": "object",
"properties": {
"title": { "type": "string" },
"price": { "type": "number" },
"in_stock": { "type": "boolean" },
"currency": { "type": "string", "enum": ["USD", "EUR", "GBP"] }
},
"required": ["title", "in_stock"]
}
2. How Enforcement Works
Two mechanisms exist, and they differ in strength.
- Constrained decoding. The schema restricts which tokens may be generated, so output is valid by construction. Strongest guarantee.
- Post-generation validation. The model is asked for JSON and the result is validated, with a re-prompt on failure. Weaker, and works with any provider.
Constrained decoding guarantees shape, not correctness: a valid object can still contain a wrong value.
3. Why It Beats Parsing Prose
| Aspect | Prose parsing | Structured output |
|---|---|---|
| Reliability | Regexes break on phrasing changes | Shape is guaranteed or rejected |
| Types | Everything is a string | Numbers, booleans, enums |
| Missing values | Ambiguous | Explicit null or absent |
| Failure | Silent misparse | Loud validation error |
That last row is the real gain: a schema turns a silent wrong value into a visible failure.
4. Designing a Good Schema
- Use enums for closed sets; they eliminate a whole class of invalid value.
- Mark required fields deliberately, and allow null where absence is meaningful.
- Describe each field. Descriptions are read by the model and improve accuracy.
- Keep it flat where possible; deep nesting increases error rates.
- Avoid free-form catch-alls, which reintroduce parsing.
5. Handling Failures
Even with validation, values can be wrong or implausible. Layer checks:
- Schema validation for shape.
- Range and plausibility checks for values.
- Cross-field consistency β a discount larger than the price is invalid whatever the schema says.
- A bounded re-prompt path that includes the specific validation error, since a model can correct a fault it is told about.
6. Common Uses
- Extracting fields from unstructured text or scraped pages.
- Classification with a fixed label set.
- Tool arguments, which are structured output by another name.
- Multi-step pipelines where one step's output is the next step's typed input.
7. Limits
- Shape is not truth. A confidently wrong number passes validation.
- Constraints can distort. Forcing a value where none exists produces invention; allow null.
- Complex schemas degrade quality. Split large extractions into several calls.
- Support varies. Not every provider offers constrained decoding.
8. Summary
Structured output makes a model's response conform to a declared schema, turning generation into a typed interface rather than text to be parsed.
Design schemas with enums, descriptions, and honest nullability; validate shape and then plausibility; and remember that a schema guarantees the container, never the contents.