Inference, Serving & Production·Question 45 of 76Advanced

How is structured output actually enforced at the serving layer, and what does it cost?

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

The mechanism is constrained (guided) decoding: compile the target format - JSON Schema, regex, or a context-free grammar - into an automaton (FSM for regex/JSON, pushdown for CFGs), and at every decode step mask the logits of all tokens that would violate the format before sampling. The model can only ever emit valid continuations, so the output is syntactically valid by construction - no parse-and-retry loops. This is what Outlines and XGrammar implement, what vLLM/SGLang expose as guided/structured output, what llama.cpp's GBNF grammars do, and what the hosted APIs run server-side: OpenAI's strict JSON-schema mode, Anthropic's structured outputs and strict tool use, and Gemini's response schemas. Each supports a different subset of JSON Schema, so know your provider's exact guarantees.

Full worked answer with diagram

The mechanism is constrained (guided) decoding: compile the target format - JSON Schema, regex, or a context-free grammar - into an automaton (FSM for regex/JSON, pushdown for CFGs), and at every decode step mask the logits of all tokens that would violate the format before sampling. The model can only ever emit valid continuations, so the output is syntactically valid by construction - no parse-and-retry loops. This is what Outlines and XGrammar implement, what vLLM/SGLang expose as guided/structured output, what llama.cpp's GBNF grammars do, and what the hosted APIs run server-side: OpenAI's strict JSON-schema mode, Anthropic's structured outputs and strict tool use, and Gemini's response schemas. Each supports a different subset of JSON Schema, so know your provider's exact guarantees.

Subtleties that show depth: masking operates on tokens, not characters - a JSON string can span token boundaries arbitrarily, so the automaton must be compiled against the tokenizer's vocabulary (the expensive precomputation that Outlines/XGrammar optimise); schema features don't all map cleanly to grammars (unbounded recursion, some regex features, additionalProperties handling), so check the supported subset.

Costs, in three buckets:

  • Compile time: schema→automaton compilation can take noticeable time for complex schemas - cache compiled grammars keyed by schema hash (first-request latency spikes are the telltale of a missing cache).
  • Per-step overhead: computing the valid-token mask each step; modern implementations get this to near-negligible, but naive ones bottleneck the decode loop.
  • Quality tax - the subtle one: forcing format can force premature commitment. If the schema demands {"answer": ...} first, the model must answer before it reasons. Mitigations: put a free-text reasoning field before the constrained answer fields, or run two phases (unconstrained thinking → constrained extraction). Also, over-tight enums/patterns can corner the model into confidently wrong values - leave an escape hatch ("other", nullable fields).

Guarantee scope: constrained decoding guarantees syntax, not semantics - the JSON will parse; the values can still be wrong. Keep application-level validation.

Worth sketching. The per-step mask-and-advance loop is the mechanism; drawing it separates "valid by construction" from "we retry until it parses".

flowchart LR
    S["JSON Schema, regex or grammar"] --> A["Compile to an automaton<br/>against the tokenizer vocabulary"]
    A --> M["Each step: mask every token<br/>that would break the format"]
    M --> SM["Sample from the masked logits"]
    SM --> ADV["Advance the automaton state"]
    ADV --> M
    SM --> OUT["Syntax guaranteed,<br/>values still need application validation"]

What a good answer covers

  • The mechanism is constrained (guided) decoding
  • The model can only ever emit valid continuations, so the output is syntactically valid by construction -…
  • This is what Outlines and XGrammar implement, what vLLM/SGLang expose as guided/structured output, what…
  • Each supports a different subset of JSON Schema, so know your provider's exact guarantees
  • Compile time: schema→automaton compilation can take noticeable time for complex schemas - cache compiled…

Key terms: constrained (guided) decoding · mask the logits · tokens, not characters · Compile time · Per-step overhead · syntax, not semantics

Interviewer follow-ups to expect

  1. Why must the grammar be compiled against the tokenizer vocabulary?

  2. When would you prefer parse-and-retry over constrained decoding?

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