Model and Inference EngineeringQuality and decoding trade-offsStaff
Why can a text stop string truncate schema-constrained JSON inside a quoted value?
By Amit Kumar2 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A gateway treats END as a stop string. The generated JSON contains customer code WEEKEND42 and is cut off before closing its value/object. Explain the competing termination and grammar contracts, trace engine versus gateway behavior and design boundary tests without executing a partial tool action.
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
The stop condition belongs to the serving layer, which can scan generated text for a matching sequence. It does not necessarily parse the JSON grammar before deciding to stop. A text stop string can therefore match inside a quoted JSON value or across token boundaries, depending on the engine's matcher. vLLM's sampling parameters distinguish stop strings, stop token IDs, and whether a matching stop string is included in returned text. The precise matching and streaming behavior must be checked in the deployed version. The parser downstream sees only what the gateway returned.
I would reproduce with the exact tokenizer, stop configuration, output mode and streaming adapter. Capture the completion finish reason and the raw generated token IDs if the engine exposes them safely. Did generation stop at a literal string, a special end-of-turn token, the max-token limit, or a network cutoff? Those all look like a partial JSON to a naïve caller but need different fixes. Compare the engine output with the gateway's streamed chunks and final assembled response. If the gateway also applies its own stop matching, there may be two places truncation can occur. Do not diagnose a model formatting problem solely from the final parsed error.
An arbitrary printable stop word is a poor boundary for structured content that may contain that word. Prefer a protocol with a dedicated end-of-message or end-of-turn marker when the model and engine actually support it, or let constrained generation reach a valid completion and use a parser to validate it. A schema-constrained decoder can prevent certain malformed continuations, but a separate text stop matcher can still interrupt it, depending on pipeline order. Verify that interaction instead of assuming the grammar takes precedence. If the output is truncated, retry under a corrected contract or return an error. Never treat the partial object as a valid tool action.
The interviewer may suggest escaping the customer's value in the prompt. That does not guarantee a model will never emit the stop bytes somewhere else, and stop matching may see the serialized escaped form differently. Test strings that cross token and stream-chunk boundaries, quoted strings, Unicode, and output at the token limit. This question is about the termination contract, not whether a syntactically valid tool call is a wise decision. The latter is already covered by JSON always parses, but the agent still makes bad calls.
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