# Structured output: answers a program can read
> JSON mode, schema-enforced output and strict tool calls each guarantee something different. None of them guarantees correct values, so the validation code stays.
Source: https://rfriedmann.de/blog/structured-output-for-programs/
Published: 2026-11-29 · Track: learn · Level: Ops
The first time I wired a model into a script, the job was simple: read a support
ticket, return a category. The model answered "This looks like a hardware issue,
most likely the docking station." Correct, helpful, and useless to the `case`
statement waiting for the word `hardware`. The next answer was "Category: Hardware".
The one after that was wrapped in a markdown code block.
A person reads past all of that. A program does not. As soon as a model's answer
feeds another system, the format matters as much as the content, and asking politely
for a format is not the same as getting it.
## Why free text breaks automation
Free text has no contract. The model may add a sentence in front, change the
capitalisation, rename a field, nest something differently, or stop halfway because
it ran out of tokens. Each variant is rare, and rare is the problem: at a few thousand
calls a day, a format error that shows up once in a hundred calls is a daily incident.
Prompting gets you most of the way. "Answer only with JSON in this format" works most of
the time. But "most of the time" means a parser that throws and someone writing regex
cleanup around it. There are better tools now, and they promise different things.
## Three levels of structure
**JSON mode** makes the output valid JSON. That is all. OpenAI's
[structured outputs guide](https://developers.openai.com/api/docs/guides/structured-outputs)
says it directly: "While both ensure valid JSON is produced, only Structured Outputs
ensure schema adherence." JSON mode will happily return `{"cat": "HW"}` when you wanted
`{"category": "hardware"}`. The same page also says you must tell the model to produce
JSON somewhere in the conversation, otherwise the API returns an error.
**Schema-enforced output** takes a JSON Schema and guarantees that the answer matches
it: the right field names, the right types, every required field present, enum fields
only from your list. OpenAI calls this Structured Outputs, Anthropic calls it
[structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
as well. As of November 2026 the parameters are:
| | OpenAI | Anthropic |
|---|---|---|
| Schema-enforced answer | `text.format` with `type: "json_schema"` and `strict: true` (Responses API), `response_format` (Chat Completions) | `output_config.format` with `type: "json_schema"` |
| Schema-enforced tool call | `strict: true` on the function definition | `strict: true` on the tool definition |
**Tool or function calling** is the third route. You describe a function with a JSON
Schema for its arguments, and the model answers with a call to it instead of text.
This is how [agents use tools](/blog/how-an-agent-uses-tools-and-memory/), and
[MCP](/blog/what-is-mcp/) describes every tool the same way. Without strict mode the
arguments follow the schema on a best-effort basis. OpenAI's
[function calling guide](https://developers.openai.com/api/docs/guides/function-calling)
says Chat Completions requests are "non-strict by default" and recommends always
enabling strict mode. Anthropic has the same switch as
[strict tool use](https://platform.claude.com/docs/en/agents-and-tools/tool-use/strict-tool-use).
If you only need data back, use the schema-enforced answer. If the model has to choose
between several actions, use strict tool calls. JSON mode is the fallback for models
or endpoints that offer nothing better.
What each level guarantees
[diagram omitted — see the page for the chart]Each level adds a guarantee about form. No level adds one about content.
## How enforcement works
The model does not get more obedient. The engine stops it from producing anything
else.
At every step a model produces a score for every token in its vocabulary. The engine
turns your schema into a grammar, tracks where in that grammar the output currently
is, and masks every token that would break it. After `{"category": "` only the
beginnings of your enum values are allowed. After the last required field only `}` is.
Anthropic's docs describe exactly this: schemas are compiled into a grammar, and the
compiled schema is cached for up to 24 hours after last use. The deep version, with the
token masks and the cost per step, is in
[making a model emit valid JSON](/blog/constrained-decoding-valid-json/). imp, the
inference engine I build, does the same.
Compilation has limits. Anthropic, as of November 2026, allows at most 20 strict tools,
24 optional parameters and 16 parameters with union types per request, and rejects
features such as `minimum`, `maximum`, `minLength` and recursive schemas. OpenAI
requires every field to be listed in `required` and `additionalProperties: false` on
every object. Check the current lists before you design a large schema.
## What the guarantee does not cover
Both vendors are clear about the gaps:
- **Wrong content.** OpenAI's guide: "Structured Outputs can still contain mistakes."
A perfectly valid `{"category": "software"}` for a broken docking station is still
wrong. Valid structure makes a [made-up value](/blog/why-models-make-things-up/)
harder to spot, not less likely.
- **Refusals.** Both document that a refusal may not follow your schema. OpenAI adds a
`refusal` field, Anthropic sets `stop_reason: "refusal"`.
- **Cut-off output.** If the answer hits `max_tokens`, it can be incomplete and invalid.
- **Small print.** Anthropic's docs note that the capitalisation of string `enum`
values is not guaranteed, and recommend comparing them case-insensitively.
## Designing the schema
The schema is the part you control, and most of the quality comes from it:
- **Enums instead of free strings.** If there are five categories, list them. A free
string invites synonyms you then have to map.
- **A way to say "unknown".** Add `"unknown"` to the enum, or allow `null`. Without it,
a required field forces the model to pick something, and it will.
- **Everything required, optional as null.** OpenAI requires it anyway, and it makes
the output predictable: every field is always there, sometimes with `null`.
- **Flat over deep.** Nested objects with optional parts are hard for the model and,
per Anthropic's docs, expensive to compile. Two levels are usually enough.
- **Short descriptions on fields.** A `description` tells the model what belongs in the
field, and the model reads it like part of the prompt.
For the ticket example:
```json
{
"type": "object",
"properties": {
"category": {
"type": "string",
"enum": ["hardware", "software", "access", "network", "unknown"]
},
"priority": { "type": "string", "enum": ["low", "normal", "high"] },
"device_id": {
"type": ["string", "null"],
"description": "Inventory number if the ticket names one, else null"
},
"summary": { "type": "string", "description": "One sentence, English" }
},
"required": ["category", "priority", "device_id", "summary"],
"additionalProperties": false
}
```
## Validate anyway, then retry
Even with enforcement I validate every answer in code, for three reasons. The
guarantee has the documented exceptions above. Some constraints cannot be expressed in
the schema, such as a range, a checksum or "this device ID exists in the inventory".
And the same code has to work against a model or endpoint without enforcement.
The order is: check the stop reason, parse, validate against the schema, then check
the business rules. On a failure, retry once with the error message added to the
request, because a specific error often fixes it. If the second attempt fails too,
the item goes to a queue for a person, not into a loop. The same principle holds for
models in shell pipelines, covered in
[LLMs in the terminal](/blog/llms-in-the-terminal/): the model drafts, and something
deterministic checks before anything happens.
## Bottom line
Free text is for people. For programs, use schema-enforced output or strict tool
calls where the API offers them: they guarantee valid JSON that matches your schema,
for answers that finish normally. JSON mode only guarantees the syntax. None of them
guarantees that the values are right. Design the schema with enums, required fields and
an explicit "unknown", and keep the validation and retry logic in your code regardless.