ModelRefs / Constrained Generation — Prompt Pattern
Constrained Generation — Prompt Pattern
Restrict the output space with grammars or regex during decoding.
Overview
Use llama.cpp / outlines / xgrammar style decoding constraints to guarantee syntactically valid output, even with weak models.
When to use it: You ship to a small or open model and need strict format guarantees.
Pattern details
| Pattern class | structured-output |
|---|---|
| Difficulty | advanced |
| Also known as | grammar-constrained decoding, regex decoding |
| Last reviewed | 2026-06-07 |
Known failure modes
- Infeasible grammar — Decoder dead-ends before EOS. Mitigation: Test with seeded prompts; widen grammar.
When not to use it
- Using constrained decoding on hosted APIs that don't support it.
Continue your research
Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to Constrained Generation — Prompt Pattern.
Frequently asked questions
When should I use the Constrained Generation prompt pattern?
You ship to a small or open model and need strict format guarantees.
What are common failure modes of Constrained Generation?
Infeasible grammar
Does Constrained Generation work across models?
Yes when the anatomy is preserved — see model compatibility matrix on the pattern page.