Skip to content
Inference Academy

Structured output interview questions (constrained decoding)

Interview questions on structured output for LLMs: how constrained decoding forces valid JSON, compiling schemas to automata, token boundary problems, per-step overhead, jump-forward decoding and quality effects, with model answers.

6 questions·Model answers and follow-upsFree
Share

Structured output (JSON that matches a schema, tool calls, regex-shaped answers) is now a standard feature of every serving engine, and interviewers use it to check whether you understand what happens between the logits and the sampled token. Strong answers explain how the guarantee works, what it costs per step, and how it can quietly hurt answer quality.

Question 1

How does constrained decoding guarantee valid JSON?

Model answer

At every decode step the model produces logits over the whole vocabulary. Constrained decoding keeps a state that tracks where the output is in the target structure (inside a string, expecting a comma, expecting the key "name" and so on). From that state it computes a mask of the tokens that can legally come next, sets the logits of all other tokens to minus infinity, and only then applies temperature, top-p and sampling. After the token is chosen, the state advances by the characters of that token.

Because an illegal token can never be sampled, the output is valid by construction, unlike "JSON mode" prompting, which only makes valid output likely. Generation ends when the state reaches an accepting state and the end-of-sequence token is allowed.

The structure comes from a JSON schema, a regular expression or a context-free grammar, compiled ahead of time into an automaton the engine can step through cheaply.

Likely follow-up questions

  • Why must the mask be applied before temperature and top-p rather than after?
  • What does the engine do if the model puts almost all its probability on masked tokens?
  • How is this different from validating the output and retrying?

Question 2

How is a JSON schema turned into something the engine can run?

Model answer

The schema is compiled into a grammar, then into an automaton:

  • Regular structures (fixed keys, enums, numbers with a pattern, bounded nesting) can be compiled into a finite-state machine. Libraries such as Outlines popularised this approach: build the FSM over characters, then precompute, for each FSM state, which vocabulary tokens keep it valid. That makes each step a lookup.
  • Recursive structures (arbitrary nested JSON, most programming languages) need a context-free grammar, run with a pushdown automaton that has a stack. Engines such as XGrammar and llguidance compile the grammar, precompute masks for the many tokens whose validity does not depend on the stack (context-independent tokens), and check only the remaining tokens at runtime.

Compilation can take from milliseconds to seconds for big schemas, so engines cache compiled grammars by schema, which matters when every request in a tool-calling product sends the same schema.

Likely follow-up questions

  • Why can't a plain finite-state machine handle arbitrarily nested JSON?
  • What makes some schemas much slower to compile than others?
  • Where would you cache compiled grammars in a multi-replica deployment?

Question 3

Why are tokens a problem for grammars, and how do engines handle it?

Model answer

Grammars are written over characters, but the model emits tokens that can span several characters and cross grammar boundaries. A single token such as ", closes a string and emits a comma; another might contain the end of a key and the start of a value. So the mask cannot be computed by looking at the first character of each token. The engine has to check that the whole token's characters can be consumed from the current state.

Other pitfalls:

  • Token healing and boundaries: forcing a prefix that ends mid-word can push the model into an unnatural tokenisation it rarely saw in training, which lowers quality.
  • Whitespace: a grammar that allows any whitespace lets the model wander into long runs of spaces or newlines; good schemas constrain it.
  • Vocabulary size: with 100,000 to 250,000 tokens, checking every token at every step is too slow, which is why masks are precomputed per state wherever possible.

Likely follow-up questions

  • Give an example of a single token that is valid only part of the way through.
  • Why can forcing an exact field name produce a worse tokenisation than letting the model write it?
  • How would you test that a grammar engine handles multi-byte UTF-8 characters correctly?

Question 4

What does constrained decoding cost per step, and how do engines hide it?

Model answer

The cost is CPU (or sometimes GPU) time to advance the automaton and build the token mask for each sequence at each step, plus applying the mask to the logits. A decode step for a mid-sized model takes a few milliseconds on the GPU, so if mask building takes a millisecond per sequence and runs serially before sampling, throughput drops noticeably, especially with large batches of constrained requests.

Ways engines keep it small:

  • Precomputed masks per automaton state, stored as bitsets, so most steps are a lookup.
  • Overlap: compute the masks for step t+1t + 1 on the CPU while the GPU runs the forward pass for step tt, since the mask depends only on the tokens already sampled.
  • Parallelism across sequences in the batch.
  • Jump-forward decoding to skip steps entirely when the next characters are forced.

A good answer says you would measure throughput and inter-token latency with and without constraints at realistic batch sizes, because a modest per-step overhead can become the bottleneck at high concurrency.

Likely follow-up questions

  • Why does constrained decoding overhead grow with batch size?
  • How do heterogeneous schemas within one batch complicate the implementation?
  • Which profile signal would show that mask computation is the bottleneck?

Question 5

What is jump-forward decoding?

Model answer

Often the grammar allows exactly one continuation: after {"name": in a fixed schema, or the closing characters of a known key. Generating those characters token by token wastes forward passes, because the model has no real choice.

Jump-forward decoding (introduced in SGLang with a compressed finite-state machine) detects runs of forced characters, tokenises them, and appends them in one go. The engine then runs a single prefill-style pass over the appended tokens to fill their KV cache and continues sampling at the next real decision point. For schemas with many fixed keys this cuts the number of decode steps substantially.

The subtlety is tokenisation: the forced characters must be re-tokenised together with what came before them, or the model sees an unusual token split. Implementations re-tokenise across the boundary and may roll back a token to keep the sequence natural.

Likely follow-up questions

  • Why is a forced run cheaper to process as a small prefill than as decode steps?
  • How does jump-forward interact with speculative decoding?
  • In which schemas would you expect almost no benefit?

Question 6

Can constrained decoding hurt answer quality? How would you mitigate it?

Model answer

Yes. The mask guarantees valid syntax, not good content, and it can distort what the model would have said:

  • Forced field order: if the schema puts "answer" before "reasoning", the model must commit to an answer before reasoning, which hurts accuracy on hard tasks. Put reasoning or explanation fields first.
  • Probability mass in masked tokens: if the model wanted to write something the grammar forbids (an apology, a clarifying question, a value outside an enum), the mask pushes it onto a low-probability legal token, which can produce a confident wrong value.
  • Unnatural tokenisation at forced boundaries, as above.
  • Over-tight schemas: very strict patterns for free text (dates, names) leave the model few natural paths.
  • Reasoning models: constraining the thinking section breaks it; constrain only the final answer and let the reasoning run free.

Mitigations: design schemas with reasoning first and permissive types where possible, allow an explicit "unknown" or error value, evaluate task accuracy with and without constraints, and monitor how often outputs hit fallback values.

Likely follow-up questions

  • How would you measure whether constraints changed accuracy on your task?
  • When would you prefer validate-and-retry over constrained decoding?
  • How do function-calling formats fit into this picture?

The lesson behind these answers · 15 min · Pro, first part free

Constrained decoding engines: grammars, token masks and jump-forward

Worked numbers, step-through diagrams and a quiz, so you can derive the answer in the interview instead of reciting it.

Read the lesson →

Related interview questions

All interview topics →