Contrast JSON mode and constrained decoding from a context-engineering point of view
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
JSON mode biases the sampler toward valid JSON; constrained decoding restricts the token space to a grammar. The harder the guarantee, the less the prompt has to spell out the shape.
Imagine filling out a form on paper. JSON mode is the difference between writing a polite sentence at the top, please use clear handwriting and keep names in the name box, and handing someone an actual form with boxes that physically prevent them from writing the answer outside the box. The sentence helps, but a person in a hurry can still write outside the lines. The form with real boxes makes that impossible. Constrained decoding is the printed form. JSON mode is the polite sentence. Once you have the printed form, you do not need to spend as many words asking for neat handwriting; the structure does the work.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 minutes: define both features, name the sampler-bias vs sampler-restriction difference, walk the prompt-token reclaim, list one quality risk, name two 2026 implementations.
# Constrained decoding: schema is enforced sampler-side, prompt stays short
from openai import OpenAI
from pydantic import BaseModel
class Extraction(BaseModel):
user_id: str
score: float
tags: list[str]
client = OpenAI()
resp = client.responses.parse(
model="gpt-5.5",
input="Extract the user id, sentiment score, and tags from this review: ...",
response_format=Extraction, # strict JSON-Schema-constrained decoding
)
result: Extraction = resp.output_parsed
# No verbose 'output exactly this shape' instructions needed in the prompt.| Dimension | JSON mode | Constrained decoding |
|---|---|---|
| Mechanism | Output distribution biased toward JSON | Logits masked to enforce a grammar |
| Guarantee | Well-formed JSON | JSON that matches the supplied schema |
| Prompt burden | Must describe the schema in words | Schema lives outside the prompt |
| Failure mode | Wrong field names or types | Forced fills, off-distribution reasoning |
| Content quality | Higher on creative tasks | Can degrade under heavy constraint |
| Typical use | Loose extraction, creative with shape | Strict extraction, tool calls |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Calling them the same feature with different names. JSON mode is a soft bias; constrained decoding is a hard grammar restriction. The difference shows up at the long tail of inputs.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.