Compare JSON mode with schema-constrained decoding. How does constrained decoding actually enforce the schema?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
JSON mode is a soft mechanism: the model is post-trained/instructed to emit valid JSON, and the provider may check bracket balance, but nothing guarantees your schema - you get valid JSON with a missing field, a string where you wanted an int, or an invented enum value. You must validate (e.g., Pydantic) and retry, which means p99 latency includes retry loops.
Full worked answer with diagram
JSON mode is a soft mechanism: the model is post-trained/instructed to emit valid JSON, and the provider may check bracket balance, but nothing guarantees your schema - you get valid JSON with a missing field, a string where you wanted an int, or an invented enum value. You must validate (e.g., Pydantic) and retry, which means p99 latency includes retry loops.
Schema-constrained decoding (OpenAI Structured Outputs with strict: true and comparable schema-enforced modes from the other major providers; open-source: Outlines, XGrammar, llama.cpp GBNF, vLLM's guided decoding) enforces the schema at the sampling step. Mechanics: the JSON Schema is compiled to a formal grammar - regex-to-FSM for simple constraints, a context-free grammar with pushdown state for arbitrary nesting. During generation, the engine tracks the current grammar state; at each step it computes the set of tokens that can begin a valid continuation and masks all other tokens' logits to −∞ before softmax/sampling. An invalid token has probability zero, so output is valid by construction - no retries. The engineering subtlety is that grammars are defined over characters but models emit BPE tokens, so token boundaries don't align with grammar symbols; efficient implementations precompute token-level transition masks per grammar state (Outlines' FSM indexing, XGrammar's optimizations) to keep per-step overhead near zero, plus a one-time schema compile cost that providers cache.
Tradeoffs a senior answer must include:
- Validity ≠ correctness: the schema guarantees shape, not truth. A constrained model can emit a perfectly-formed wrong answer.
- Quality effects: aggressive format constraints can degrade reasoning - masking may force the model off its preferred continuation onto low-probability paths, and there's published evidence of reasoning-task degradation under strict format restriction (Tam et al. 2024, Let Me Speak Freely?). Standard mitigation: put a free-text
reasoningfield first in the schema so the model thinks before filling constrained fields. - Schema feature limits: strict modes typically support a JSON Schema subset (e.g., restrictions around unions, recursion, or pattern support vary by provider); complex schemas also increase compile cost.
- Greedy-mask pathologies: locally-valid token choices can paint the model into globally awkward strings (whitespace/number-formatting artifacts in early implementations).
Rule of thumb: constrained decoding for anything a program parses; JSON-mode-plus-validation only when you need schema features strict mode doesn't support.
Worth sketching. Putting the mask before the softmax is the whole mechanism, and it explains both the no-retry guarantee and the distribution shift in one picture.
flowchart LR
A["JSON Schema"] --> B["Compile to a grammar:<br/>FSM or pushdown"]
B --> C["Grammar state at step t"]
C --> D["Mask every token that<br/>cannot continue, to minus infinity"]
D --> E["Softmax and sample"]
E --> F["Advance the state"]
F --> C
E --> G["Valid by construction,<br/>no validate-and-retry loop"]
What a good answer covers
- JSON mode is a soft mechanism
- You must validate (e.g., Pydantic) and retry, which means p99 latency includes retry loops
- Schema-constrained decoding (OpenAI Structured Outputs with strict
- Mechanics: the JSON Schema is compiled to a formal grammar - regex-to-FSM for simple constraints, a…
- During generation, the engine tracks the current grammar state; at each step it computes the set of…
Key terms: JSON mode · Schema-constrained decoding · Validity ≠ correctness · Quality effects · Schema feature limits · Greedy-mask pathologies
In the glossary: JSON mode · Structured output · vLLM · BPE · Constrained decoding
Interviewer follow-ups to expect
Why can constrained decoding change output distribution, not just filter invalid outputs?
How would you implement constrained decoding for a non-JSON DSL?
Function-calling arguments vs. structured output - when to use which?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep