Constrained generation forces LLM outputs to conform to a specific format — JSON schema, regex pattern, BNF grammar, or enumeration. Without constraints, models may generate text anywhere in the output, include explanations, or produce invalid JSON. OpenAI Structured Outputs (json_schema), Anthropic tool use, llama.cpp grammar, and Outlines/LMQL/Guidance frameworks provide different levels of constraint enforcement. Constraints are essential for machine-parseable outputs in production pipelines.
Guaranteed schema-matching JSON with the Structured Outputs API.
Old approach: 'return JSON format' in prompt (fails ~5% of requests). Structured Outputs: enforced at generation time via constrained decoding. Zero parse failures. Use it by default for any machine-parsed output.
For classification tasks, constrained generation prevents the model from inventing new categories ('somewhat negative', 'ambiguous_positive'). Enum constraints guarantee output is one of your defined classes.
Can't use cloud APIs? Outlines + vLLM applies JSON schema, regex, and grammar constraints to any HuggingFace model. Get OpenAI Structured Outputs quality from self-hosted Llama/Mistral.
Sign in to share your feedback and join the discussion.