Structured Output: Schema Constraints Beat Prompted JSON
Bottom line: Asking a model to “reply in JSON” fails on 2–8% of calls depending on schema complexity; grammar-constrained decoding fails on roughly 0% and costs the same. If you parse model output with a try/except, switch to a constrained mode.
Three approaches, ranked
| Method | Failure rate | Latency | Schema support |
|---|---|---|---|
| Prompted JSON + regex extraction | 2–8% | baseline | anything |
| Function / tool calling | 0.5–2% | +1 call | flat objects |
| Grammar-constrained decoding | ~0% | baseline | nested, recursive |
Prompted JSON fails more on: long schemas, union types, enums with many members, and any schema where a field name collides with prose the model wants to write.
What constrained decoding is
The decoder masks logits so only tokens valid for the current grammar position can be sampled. The output cannot be malformed because malformed next-tokens are unreachable. Providers expose it as response_format with a schema (OpenAI), response_schema (Gemini), or a grammar backend (vLLM, llama.cpp).
1 | from pydantic import BaseModel |
strict: True is what turns the schema into a hard constraint; without it you get best-effort.
Schemas that break
Some constructs push you back to prompted JSON or a repair pass: additionalProperties: true, recursive $ref, and extremely wide unions. Keep schemas boring — flat enums, no free-form dicts, bounded array lengths (maxItems).
The repair loop still earns its keep
Constrained output guarantees well-formed JSON, not semantically valid JSON. A SKU field can be filled with a plausible-looking code that matches the pattern and nothing else. Validate in application code and retry once with the validator error appended:
1 | for attempt in range(2): |
Track retry rate per field. A field that retries more than 5% of the time is ambiguous in the prompt, not in the schema.
Migration order
- Log the parse failure rate for a week to get a baseline
- Move the hottest endpoints to
strictschemas - Add business validation for anything with a real-world key
- Delete the regex scraper only after the new path has a week of clean runs
Regex extraction on top of JSON output is the worst of both worlds — it hides failures behind a green dashboard until a schema change makes every field silently empty.
Designing the measurement
Log parse outcomes per endpoint per schema version: valid on first pass, valid after retry, invalid after retries. That gives a failure rate you can regress against, and a schema version to blame when it moves.
1 | outcome = {"endpoint": ep, "schema_v": v, |
Sample 200 calls per endpoint before claiming a fix. Single-digit failure rates need that sample to separate 2% from 5%.
Boundary conditions
strict schemas are stricter than they look: every property must be in required, and adding an optional field later is a breaking change unless the client defaults it. Version the schema and keep the old one live until the consumer handles both.
Token cost rises: schema tokens are billed on every call. A 900-token schema on a 200-token task is a 4× overhead. Split large schemas into a two-stage extraction rather than one wide object.
Streaming and strict schemas conflict on some providers — the validator needs the complete object. Do not combine them without checking.
When retries climb
- One field retries → make the prompt name the field’s format explicitly
- All fields retry → schema too wide; split the call
- Validation passes, business rules fail → the model cannot see the rules; put them in the prompt
- Only on long inputs → truncation is cutting the schema anchor; move the schema above the document
Retry rate above 10% on any field means the prompt is under-specified. The schema guarantees shape, never meaning.
FAQ
What is grammar-constrained decoding and why does it eliminate malformed JSON?
Constrained decoding masks logits so only tokens valid for the current grammar position can be sampled. Because malformed next-tokens are unreachable, the output cannot be malformed. Providers expose it as response_format with a schema (OpenAI), response_schema (Gemini), or a grammar backend (vLLM, llama.cpp).
How much does prompted JSON actually fail compared to constrained decoding?
Asking a model to “reply in JSON” fails on 2–8% of calls depending on schema complexity. Grammar-constrained decoding fails on roughly 0% and costs the same in latency. If you parse model output with a try/except, switch to a constrained mode.
Why does constrained decoding still need a validation and repair loop?
Constrained output guarantees well-formed JSON, not semantically valid JSON. A SKU field can be filled with a plausible-looking code that matches the pattern and nothing else. Validate in application code and retry once with the validator error appended.
Which schema constructs break constrained decoding?
additionalProperties: true, recursive $ref, and extremely wide unions push you back to prompted JSON or a repair pass. Keep schemas boring: flat enums, no free-form dicts, and bounded array lengths using maxItems.
What does strict: True do in the OpenAI response_format call?
strict: True is what turns the schema into a hard constraint; without it you get best-effort. Note that strict schemas require every property to be in required, so adding an optional field later is a breaking change unless the client defaults it.











