prompt-eng
Glossary ↗Constrained Decoding
Constrained decoding (also called grammar-constrained or schema-constrained generation) is a generation-time technique, implemented at the model-serving/inference layer rather than in the prompt text itself, that restricts which tokens a model is allowed to produce at each generation step to only those consistent with a predefined grammar, regular expression, or schema — mechanically guaranteeing the final output is structurally valid, rather than merely making valid output statistically likely through prompt instructions alone. This is the underlying implementation mechanism behind most providers' JSON mode and structured output features: rather than hoping a well-worded prompt convinces the model to produce valid JSON, the inference engine actively masks out any token that would make the in-progress output invalid at each step (for example, refusing to generate a token that would open a new unclosed brace when the schema expects the object to close), making malformed output structurally impossible rather than just less probable. Constrained decoding can enforce far more specific constraints than generic JSON validity — full JSON Schema conformance (correct field names, types, enum membership, required fields), regular-expression-matched formats (a phone number, a date in a specific format, a product SKU pattern), or even a fully custom context-free grammar for specialized output languages (a subset of SQL, a domain-specific configuration format). For SaaS builders, understanding constrained decoding clarifies an important reliability distinction: prompt-level formatting instructions ("please output valid JSON") are probabilistic and can fail, while true constrained decoding at the API/inference level is a mechanical guarantee — this is why structured-output features that use real constrained decoding are meaningfully more reliable than a hand-rolled prompt-engineering approach to the same formatting goal, and why teams building on providers that expose real schema-constrained generation should prefer that mechanism over prompt instructions alone whenever the downstream system requires guaranteed-valid structure. Concrete worked example: an AI SQL-query-generation feature for a no-code database SaaS tool uses constrained decoding with a custom grammar restricting output to a safe, read-only SQL subset — the model is mechanically prevented from ever generating a DROP TABLE, DELETE, or UPDATE statement, because those tokens simply aren't reachable within the constrained grammar at any generation step, regardless of how the user phrases their request or how creatively they might try to prompt-inject a destructive query. This is a fundamentally stronger guarantee than a system-prompt instruction saying "never generate destructive SQL," which remains theoretically bypassable through sufficiently adversarial prompting. For SaaS builders evaluating providers, it's worth asking specifically whether "JSON mode" or "structured output" is implemented via true constrained decoding versus a lighter-weight approach (fine-tuning the model to be very good at following format instructions, without a hard mechanical guarantee) — the two can look identical in casual testing but behave very differently on rare edge cases and adversarial inputs, which only shows up under real production volume or a dedicated red-team pass.
Related terms