deep · 6 min
Structured output
Getting JSON that matches a schema instead of free text.
updated 23 Sept 2026
Here's the kind of reply that passes every check in your code:
{
"name": "Maria Lopez",
"email": "maria@example.com",
"plan_interest": "Enterprise",
"demo_requested": true
}
Valid JSON. Every field present, every type right. And the input was a restaurant review. Nobody named Maria asked for a demo.
How do we end up with JSON that's perfectly shaped and completely made up? To see it, we have to go back to the start: what structured output is, and what exactly it promises.
What we wanted
Most of the time, the thing reading a model's answer is our code, not a person. Say our sales tool gets this email:
John Smith (john@example.com) is interested in our Enterprise plan and wants to schedule a demo for next Tuesday at 2pm.
The CRM wants four fields, not a paragraph about John. Structured output means getting the model to answer with JSON that matches a schema we define, so our code can read it without guessing. It's the most common way an LLM ends up inside real software: messy text in, typed data out.
So how do we make sure the reply really is that JSON, every time?
Three ways to ask
We make an ordinary request through the chat API, and there are three ways to ask for JSON. Each one promises more.
| Just ask in the prompt | JSON mode | Strict schema | |
|---|---|---|---|
| Parses as JSON | Usually | Yes | Yes |
| Matches your schema | Usually | No | Yes |
| How | "Reply only with JSON like..." | A flag, e.g. type: "json_object" | Send the schema, strict mode on |
Just asking works more often than you'd think, and fails in annoying ways: broken syntax, a missing field, a number where you wanted a string, and a retry loop to clean it up.
JSON mode is the older fix. It promises the reply parses, nothing more; the keys can be anything. It also has a trap. If nothing in the messages tells the model to write JSON, it can spit out whitespace until it hits the token limit. OpenAI's API refuses the request unless the word "JSON" appears somewhere in it, for that reason.
A strict schema is what both OpenAI and Anthropic call structured
outputs. We send a JSON Schema with the request, and the reply is
guaranteed to follow it: every required key there, every type right, no
enum value off the list. On OpenAI it goes back to GPT-4o in 2024; on
Claude, as of 2026-09, it's the output_config.format parameter. In
practice we rarely write the schema by hand. We define a Pydantic model or
a Zod object, and the SDK turns it into a schema and parses the reply back
into that type.
How "guaranteed" works
The model writes one token at a time, picking each from a list of probabilities (next-token prediction, sampling). A strict schema steps in before each pick and rules out every token that would break it.
That's constrained decoding. It's fast because the hard part happens ahead of time: the schema gets compiled into a state machine, and for each state the allowed tokens are worked out once. At run time it's a lookup.
Two small costs. The first request with a new schema is slower while it compiles; Claude then caches it for 24 hours from last use. And Claude adds a short bit of system prompt explaining the format, which you pay for as input.
Not every schema is allowed, either. Both providers support a subset of
JSON Schema, and the subsets differ. OpenAI wants every field required
and additionalProperties: false on every object. Claude doesn't do
recursive schemas or limits like minimum and maximum, and caps optional
parameters, because each one roughly doubles part of the compiled grammar.
So keep schemas flat, make fields required, and check things like "age
between 0 and 120" in your own code.
Back to Maria
Now we can see the bug. The schema says there must be a demo_requested
boolean. So at that point the model has to pick true or false. It
can't pick "this review has nothing to do with sales". OpenAI's docs say
it plainly: the model will always try to fit the schema, so when the input
is unrelated, it makes up values. That's hallucination
in a tidy JSON wrapper, and the same thing happens without schemas when
you ask a model to pull a field that isn't there.
The fix is to give it a way out. Let fields be null (on OpenAI, where
every field is required, that's a type that allows null), and say in
the prompt when to use it. Then check the values in code, not only the
shape.
"Guaranteed" has a few more holes worth knowing:
- Refusals win over the schema. On Claude you get
stop_reason: "refusal"with a 200 status, and you're billed. On OpenAI there's a separaterefusalfield. - Running out of tokens leaves half an object. Claude says
stop_reason: "max_tokens"; OpenAI saysstatus: "incomplete". Check before you parse. - Enum casing. Claude can return "Conversation Topic 3" when your enum says "Conversation topic 3", with no error. Compare case-insensitively.
Does the format make answers worse?
This one is argued about, and key order is part of it. The model writes
left to right. Put a reasoning field before answer, and it gets to
think before committing (chain of thought inside
JSON). OpenAI writes keys in your schema's order. Claude does too, except
required fields come before optional ones, so if reasoning is optional
and answer is required, the answer comes first.
A 2024 paper, "Let Me Speak Freely?", reported that reasoning got worse under format rules, and worse the stricter the rules. On one of its tasks, every JSON-mode answer from GPT-3.5 Turbo had put the answer key before the reason key. The team behind the Outlines library re-ran the tasks with clearer prompts and real constrained generation, and found structured output slightly ahead. Neither side is neutral (the rebuttal comes from a company that sells structured generation, and it used one small open model). The fair reading: how you add structure matters. Give it a clear prompt and room to reason before the answer. The providers promise the shape, nothing about quality.
Practitioners in 2024 advised against building your own JSON repair tooling, since providers had every reason to solve parsing. They mostly have. What's left for us is the part no schema can check: whether Maria exists. Pulling real fields out of messy documents is its own job, covered in extraction, and when the model needs to call your code with those fields, that's tool calling.
Further reading
- Structured outputs, Anthropic, undated (checked 2026-09-23). Claude's version, with the limits and the cases where output can still break. The sales email example is from here.
- Structured model outputs, OpenAI, undated (checked 2026-09-23). Read the JSON mode section and the warning about unrelated input.
- Efficient Guided Generation for Large Language Models, Willard and Louf, 2023. If you want to see how a schema becomes a state machine over tokens.
- Let Me Speak Freely?, Tam et al., 2024. The claim that format rules hurt reasoning.
- Say What You Mean: A Response to 'Let Me Speak Freely', Will Kurt (.txt), 2024. The rebuttal. Read both and decide for yourself.
- What We've Learned From A Year of Building with LLMs, Yan et al., 2024. Real products built on the messy-text-in, typed-data-out pattern.