AI GitHub
XGrammar for grammar-constrained decoding
Apply structured-output constraints inside a model inference engine.
Key Takeaways
- XGrammar compiles rules and produces token masks for an autoregressive decoder.
- Check that the inference engine exposes a supported grammar interface.
- Serve one schema through a supported engine and compare unconstrained and constrained responses under the same load.
- The schema features and overhead depend on the integration and tokenizer.
How it works
XGrammar compiles rules and produces token masks for an autoregressive decoder. Its engine integrations place that work in the generation path rather than relying on a prompt to teach the format.
What you need
Check that the inference engine exposes a supported grammar interface. If your schema is generated dynamically, measure setup work for a fresh grammar as well as repeated requests.
What to try first
Serve one schema through a supported engine and compare unconstrained and constrained responses under the same load. Include Unicode and nested fields in the sample.
Profile several schema sizes against the same model and tokenizer. If a complex grammar hurts throughput, test a simpler schema before changing the model.
Where to look for failures
The schema features and overhead depend on the integration and tokenizer. Validate the full output after decoding and measure the effect on throughput with your own grammar.
Related Franklin pages
These approaches control generation at different points. Run the same nested schema and check compatibility with the model backend before comparing speed or accepted outputs.
- Filter next-token options so generation follows a JSON schema or regular expression. LM Format Enforcer for decoder constraints
- Generate the variable parts of a JSON document while code supplies the fixed syntax. Jsonformer for schema-shaped local extraction
For the distinction between requesting structured output and enforcing its format, see Franklin’s glossary. Structured output
Project documentation
Try it for your workflow
Explore the project’s documentation for setup and usage, or save this page to a collection for later.
Read the project documentation ↗