Structured Output
Decodificación restringida · Modo JSON
Escrito por Darjus Kazakevic · revisado el
Get a value back, not a paragraph you have to parse.
A model normally returns prose, and prose is a poor interface between programs. Structured output fixes this. You give a schema, and the provider restricts generation so the reply always matches it. This is called constrained decoding: at each step, tokens that would break the schema are blocked.
So the reply parses, unless the token limit cut it off. It can still be wrong: an invented number in a required field, or an enum value picked because it came first. Checking the shape is free. Checking the meaning is still your job.
En esencia — Constrained output turns the model into a component that returns a typed value. But a value that matches the schema can still be wrong.
Cuándo recurrir a él
Recurre a él cuando
- The output feeds code, not a person: a field, a route, a database row
- You extract or classify data into a fixed shape
- Later code needs a closed set of values, like a switch
Reconsidéralo cuando
- The output is prose for a person to read
- The reasoning is the product, and early structure would cut it short
- The shape changes so much per input that the schema becomes “any”
How it works
- The schema
- A JSON Schema, usually generated from your own types, that says exactly what a valid reply looks like. It controls generation and also explains the task: the model reads field names and descriptions as instructions.
- Constrained decoding
- At each token, the provider allows only tokens that keep the output valid against the schema. A malformed reply becomes almost impossible, not just unlikely.
- Semantic validation
- Checks in your own code after parsing: do these ids exist, do the lines add up to the total, is this date in the future? This covers everything the schema cannot express, which is most of what matters.
- The escape hatch
- A way for the model to say “I don't know”, built into the schema: a nullable field or an "unknown" enum value. Without one, a required field forces an answer, and the model invents one.
Reglas de diseño
- Check the meaning after the parse
- A successful parse proves nothing about the content. In code, check that ids exist, totals add up and dates make sense. Send a failure back to the model as feedback, or escalate it to a person.
- Always allow “unknown”
- A required field with no null or “unknown” option forces an answer even when the input has none. The model will invent one. Allow null, and an unclear case becomes visible instead of silently wrong.
- Keep schemas small and flat
- Accuracy drops as nesting and field count grow. Three fields answered well beat fifteen with four guesses. Give extraction, classification and summary separate calls instead of one large object.
- Use clear field names and short enums
- The model reads the schema as part of the prompt, so customerSentiment works better than field3. Prefer enums to free text, and keep them short: a forty-value enum behaves like free text.
Errores habituales
- Treating schema-valid as correct. This is the most common mistake with the pattern.
- Required fields with no null or “unknown”. They reliably produce invented values.
- Deeply nested objects, which quietly lower extraction accuracy.
- Field names like data1 and data2, when the model reads names as instructions.