Structured Output
JSON schemas, enums, validation and constrained generation — and why valid ≠ correct.
Why this matters
Downstream code can't read prose. It needs {"intent": "interested", "amount": 500000} — every time, with exactly those keys, types and allowed values. At 100,000 calls a day, a 1% malformed-output rate is 1,000 silent failures.
is a contract (a ) plus enforcement (validation, retries, and where available ). The contract guarantees *shape*; only evaluation tells you whether the *values* are right.
Core concept
Technical explanation + simple intuition + example
The schema is the contract
A schema says: these fields exist, these are required, this one is an integer ≥ 0, this one is one of three exact strings, nothing else is allowed. Write it before the prompt — it forces you to decide what you actually need.
Constrained decoding vs validate-and-retry
Two ways to get valid JSON: (1) constrain generation so the model can only pick tokens that keep the output valid; (2) let it generate, then validate, retrying or routing to review on failure. Constrained decoding guarantees syntax; neither guarantees the answer is right.
The 10 questions
Every important concept should answer all of these
1What is it?
Making the model emit machine-readable output (usually JSON) that conforms to a declared schema.
2Why does it exist?
Automation needs predictable fields, types and allowed values.
3What problem does it solve?
It eliminates parsing heuristics and silent failures between the LLM and downstream code.
4How does it work internally?
Declare a JSON Schema, instruct the model (or use JSON/tool mode), validate every response, retry once, then fall back to review; optionally constrain decoding.
5What does it look like on real transcript data?
Amounts like '5 lakh' tempt models into strings; enums drift in case ('Interested'); long reasoning fields hit max_tokens and truncate the object.
6What can go wrong?
Invalid syntax, wrong types, enum violations, missing required fields, extra fields, markdown wrappers, truncation — and schema-valid but factually wrong values.
7How do I evaluate it?
Format validity rate (parse + schema), per-field accuracy against gold, and truncation/stop-reason rates.
8When should I use it?
Every LLM output consumed by code.
9When should I NOT use it?
Free-text summaries for humans (still consider a light schema with a summary field).
10How does it affect downstream systems?
Schema design fixes the interface between stages; enum choices must match the taxonomy and the annotation guideline.
Interactive lab: JSON extraction lab
Edit a JSON Schema and a model output and validate live with a real validator. Diagnose eight deliberately malformed outputs. Step through constrained decoding token by token.
Try it yourself
Conceptual (A), applied (B) and engineering (C) exercises
{"loan_type": "personal", "amount": "5 lakh", "interest_rate_mentioned": true, "intent": "interested"}. The schema says amount is an integer. What's wrong?Real-world application
A nightly pipeline logged “0 interested customers” for three days. The model had started returning "intent": "Interested" after a provider model update; the downstream filter compared against the lowercase enum. No exception was thrown. The team added schema validation with enum, alerting on validity-rate drops, and pinned the model version.
Common mistakes
Silent failures: wrong types and enum drift pass through.
Fix: Validate every output; alert on validity rate.
Downstream code breaks or treats missing as false.
Fix: Require all keys; use null for unknown.
The schema can't know that 5 lakh ≠ 50,000.
Fix: Add grounding checks and field-level evaluation.
Cost and latency explode on systematic failures.
Fix: One retry, then route to review and log.
Engineering decision
Constrained decoding, JSON mode, or validate-and-retry?
- Constrained decoding/strict tool schemas when your provider or runtime supports them
- Validate-and-retry always, as a safety net
- Both for high-volume pipelines
- Relying on 'please return JSON' alone at scale
Quiz
Pass mark 60%. Missed questions go to your mistake log and spaced review.
"additionalProperties": false do?"interest_rate_men and no closing brace. Most likely cause?