Your product needs a model's answer to land in a database column, an API call or a workflow step. That means JSON your code can trust. LLM structured output features now make that far easier than the prompt-and-pray era, but they solve a narrower problem than most teams assume. This post covers what they guarantee, what they quietly don't, and the validation layer that closes the gap.
What does LLM structured output actually guarantee?
There are three levels, and teams often mix them up.
| Approach | Valid JSON? | Matches your schema? | Correct values? |
|---|
| Prompt only ("reply in JSON") | Usually | Sometimes | Not guaranteed |
| JSON mode | Yes | No | Not guaranteed |
| Schema-constrained structured outputs | Yes | Yes, for supported schemas | Not guaranteed |
OpenAI's structured outputs guide is explicit that both JSON mode and Structured Outputs ensure valid JSON, but "only Structured Outputs ensure schema adherence", and it recommends using them over JSON mode when possible. Anthropic's structured outputs docs describe the same idea: constrain the response to a schema, either as a JSON output format or as strict tool use.
The last column is the important one. A schema constrains shape. It cannot know that an invoice total should equal the sum of its line items, or that a date should not be in the past. That part is still your job.
When does structured output still fail?
Three documented cases, all of which return a normal-looking response.
1. Refusals. Anthropic's docs say a refusal comes back with stop_reason: "refusal" and a 200 status, you are billed for the tokens, and "the output might not match your schema because the refusal message takes precedence over schema constraints." OpenAI similarly says a refusal does not necessarily follow the schema and exposes a refusal field to check.
2. Truncation. If the model hits the token limit, the output can be cut off. Anthropic's docs say that with stop_reason: "max_tokens" the output "might be incomplete and not match your schema". OpenAI's guide shows the same check against an incomplete status.
3. Schema features the provider doesn't support. Both providers accept a subset of JSON Schema. Anthropic lists recursive schemas, numerical constraints such as minimum and maximum, string constraints such as minLength, and additionalProperties other than false as unsupported, and an unsupported feature returns a 400 error. So a constraint you wrote in the schema may not be enforced by the decoder at all.
The practical rule: read the stop reason before you parse. A 200 is not a success signal.
Why a valid object can still be wrong
Schema-valid output fails in ways that look fine to a parser:
- Wrong value, right type. The amount is a number, just the wrong number.
- Silent enum drift. Anthropic notes that structured outputs don't guarantee the capitalization of string
enum values, so compare them case-insensitively.
- Plausible invention. If a field is required but the source document doesn't contain it, the model has to put something there. Make such fields nullable so "not found" is a legal answer.
- Cross-field inconsistency. Each field is valid; together they contradict each other.
This is the same gap that sits behind most agent failures in production: the output looks right, so nothing flags it.
What schema design makes failures less likely?
Design the schema for the model as well as for your database.
- Keep it flat where you can. Deep nesting and many optional or union fields make a schema harder to satisfy. Anthropic documents explicit complexity limits per request (for example, 24 optional parameters and 16 union-type parameters) and returns a 400 if a schema is too complex.
- Make "unknown" representable. Nullable fields or an explicit
not_found value beat forced guesses.
- Use enums for closed sets, and normalise them on your side.
- Put short reasoning in its own field, not step-by-step thinking. Anthropic warns that a property asking for the model's thinking can trigger a refusal and suggests asking for a short explanation instead.
- Set
additionalProperties: false. OpenAI's examples do this on every object, and Anthropic requires it to be false if set.
What does a production validation layer look like?
A reliable pipeline has four steps after the model call:
- Check the stop reason. Branch on refusal, truncation and normal completion before touching the payload.
- Parse and validate against the schema locally. Zod in TypeScript or Pydantic in Python. Anthropic's SDK helpers do this: they can transform schemas that use unsupported features and then enforce the original constraints locally.
- Run business-rule checks. Totals add up, dates are plausible, IDs exist in your system, enum values are in your allowed set.
- Route failures by type. This is where cost control happens.
| Failure | Right response | Wrong response |
|---|
| Truncated output | Retry once with a higher token limit | Retry with the same limit |
| Refusal | Surface to a human or a fallback path | Loop retries |
| Schema violation (unsupported constraint) | Fix the schema or validate locally | Retry the same call |
| Business-rule violation | Retry once, passing the specific error back | Retry with no feedback |
| Repeated failure | Queue for human review | Retry forever |
Every retry is a full paid call, so unbounded loops show up directly in your bill. If you are already working on spend, the same discipline applies as in reducing LLM API costs: cap attempts, and measure.
What should you measure?
Track these separately from overall error rate:
- Parse or schema failure rate, per prompt and per schema version. A jump after a prompt change is a regression you can catch the same day.
- Refusal and truncation rates. They point to different fixes (prompt versus token budget).
- Business-rule failure rate, by rule. The rule that fails most tells you which field the model struggles with.
- Retries per successful request. This is the cost of unreliability, expressed in calls.
These feed naturally into an evaluation framework: keep a fixed set of real inputs with expected outputs, and re-run it whenever you change a model, prompt or schema. If you route between providers, remember that schema support differs between them, as covered in LLM API fallback for outages and rate limits. A fallback model needs the same validation layer, and possibly a schema it supports.
What should you do next?
- Switch from prompt-only or JSON mode to schema-constrained structured outputs on every path where code consumes the result.
- Read the stop reason before parsing, and handle refusal and truncation explicitly.
- Add local validation with Zod or Pydantic, plus business-rule checks the schema can't express.
- Make "unknown" a valid value in your schema.
- Cap retries, route failures by type, and log every failure with its category.
- Build a small regression set from real inputs before your next prompt or model change.
If you are building an AI product where model output feeds billing, records or customer-facing workflows, this validation layer is part of the architecture, not an afterthought. Our AI SaaS page covers how we approach it.