The problem structured outputs solves
Asking a language model to “return JSON” and then parsing the result is one of
the most fragile patterns in AI engineering. The model adds a markdown fence,
omits a field, invents an extra key, or trails a comment — and your parser
throws. Teams paper over this with retry loops, regex stripping, and
try/catch blocks. OpenAI’s structured outputs removes the whole class of
bugs: you describe the exact shape you want with a JSON Schema, and the model is
constrained to produce output that matches it. Build that request body with
the tool above.
How it works
You pass a response_format of type json_schema containing your schema and
strict: true. Under the hood, OpenAI compiles your schema into a grammar that
constrains decoding, so the model literally cannot emit a token sequence that
violates the structure. The returned message.content is then a JSON string you
can parse directly with full confidence.
Strict mode has two non-negotiable rules that trip people up:
- Every property must be in
required. There is no implicit optional. To express “this field may be absent,” allownullas one of its types and mark it required anyway. additionalPropertiesmust befalse. This is what stops the model from inventing keys you didn’t ask for.
The builder enforces both rules automatically, so the body it produces is valid on the first try.
Tips and patterns
- Pin a dated model. Use
gpt-4o-2024-08-06or newer; older snapshots only support the weakerjson_objectmode that doesn’t guarantee the shape. - Use it for extraction and tool-like APIs. Structured outputs shine when you pull fields out of unstructured text, classify into a fixed enum, or return data an application will consume programmatically.
- Prefer enums over free strings for categories. Adding an
enumto a string property guarantees the model picks one of your allowed values, which is far more reliable than instructing it in the prompt. - Keep schemas reasonable. Very deep or huge schemas cost tokens and can hit limits; model the data you actually need, not everything you might.