Experiments / Structured Output
Back to ExperimentsStructured Output Reliability
Five ways to make a model return valid JSON, measured as schema complexity climbs. Parse rate lies: a response can be perfect JSON and still break your schema. What actually holds up under load, and what it costs in latency.
Flat schema, 5 fields
Extract 5 fields (name, email, company, role, seniority) from a messy contact blob. Types are strings and one bounded enum. This is the easy case: everyone does well, the gaps are small but they exist.
samples: n=1000model: claude-sonnet-4-6date: 2026-06
Method
Schema-valid
Retry rate
Latency
Notes
- Naive promptprompt94.3%parses 96.2%5.8%baselineBaseline: 'return JSON'. Fails on markdown fences (```json), trailing prose after the object, and the odd trailing comma. Parses 96%, but 2pt more break the schema (wrong enum casing).
- Prompt + 3 examplesprompt97.7%parses 98.5%2.3%+90msThree examples kill most of the formatting noise. Overhead is the extra input tokens on every call. Still occasionally wraps the object in prose.
- Forced tool calltool-use99.1%parses 99.7%0.9%+40mstool_choice forces a single function call whose input_schema is the target. Structure is guaranteed by the API layer. Remaining misses are semantic (right shape, wrong enum value).
- Native json_schemaschema99.9%parses 100.0%0.1%+150msStrict response_format with the schema attached. Structurally impossible to return invalid JSON. Overhead is schema compilation on the provider side, amortized under caching.
- Constrained grammargrammar100.0%parses 100.0%0.0%+320msSelf-hosted Llama-3.3-70B via vLLM + xgrammar. Token sampling is masked to the grammar, so every token is legal by construction. 100% valid, but the grammar-compile and masking cost real latency.
Takeaway
On easy schemas the naive prompt is already 94% valid, so the question is what the last 6% costs you. If a bad row means a failed enrichment, few-shot is enough. If it means a broken downstream job, force the structure at the API layer: tool-use gets you to 99% for almost no latency.