Model and Inference EngineeringTransformer and model mechanicsPrincipal
How can a mismatch between grammar masks and stop-token IDs produce incomplete JSON?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
In a hypothetical xgrammar integration, the tokenizer declares EOS token A, the engine’s generation configuration also stops on token B, and the grammar backend receives only A. A request ends inside a JSON string with finish_reason=stop. Explain how to establish the cause, define a shared termination contract, and design regression tests without assuming every backend or release has this defect.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
EOS means end of sequence. The grammar and the engine can disagree about which token IDs end the output. A grammar-constrained decoder masks next-token IDs so the visible output can still satisfy its grammar. The serving engine also has stop-token IDs that terminate generation. If the engine recognizes an end token the grammar backend was never told about, the backend may treat that ID as ordinary allowed vocabulary while the engine treats it as stop. Generation ends before the closing quote and braces. A reported vLLM xgrammar integration reproduction shows exactly this contract mismatch. It is an issue report and reproduction, not proof that every backend or current release has the bug.
I would first capture the raw generated token IDs, the stop reason, grammar mask at the last step, model generation config and tokenizer's special tokens. Did generation hit an EOS token, a configured stop string, a length limit, or a transport cancellation? Those failure modes need different fixes. If it is the extra EOS case, enumerate all token IDs the engine can use to stop, pass that same set to the grammar backend, and allow those IDs only when the parser is in an accepting state. Also verify that padding or tool-control special tokens cannot slip through the grammar path. vLLM's structured-output documentation describes the public JSON and grammar modes, while the exact stop-token wiring is an implementation detail.
The regression test should put generation inside an open JSON string, make each possible stop ID highly likely in turn, and assert that it is masked until the document is complete. Then check legitimate termination immediately after a complete document, plus max-token exhaustion and client cancellation. A JSON parser at the response boundary should still reject incomplete output. Retrying may help a transient length cut, but it does not repair a mismatch between the grammar and the engine. The tokenizer, parser state and engine stop rules must agree on the same token stream.
The model’s tokenizer, generation configuration and request-level controls can contribute different termination rules. In the hypothetical case, A and B are labels for token IDs, not literal JSON characters. The causal claim is established only if the trace shows B was eligible in the current grammar mask, selected by the sampler and recognized as terminal by the engine before the parser reached a complete document.
The cited issue was reported in August 2026 and describes a TokenizerInfo construction path deriving one stop ID unless an explicit stop-token set is passed. Treat that report as evidence about the described integration, rather than a tested statement about an unspecified current package. Record engine version or commit, xgrammar version, tokenizer revision, generation configuration and request overrides when reproducing it. This repository does not include that GPU runtime, so the proposed regression is a test design rather than a claim that it was executed here.
At an open-string parser state, the test should enumerate the effective engine EOS IDs and check their grammar-mask bits. Then finish the smallest valid JSON document and check that legitimate termination becomes possible under the backend’s completion convention. Keep request stop strings and maximum length separate: either can intentionally truncate an unfinished document even if EOS masking is correct. A shared EOS set cannot make arbitrary cancellation or an exhausted token budget structurally valid.
At the response boundary, inspect both the finish reason and the parsed payload. Retry only under a bounded policy that distinguishes incomplete generation from a persistent configuration mismatch. If the output feeds a tool, validation must finish before the tool receives it. Grammar conformance establishes syntax under a specific decoding contract. It does not authorize an action or prove the JSON fields are semantically correct.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux