AI GitHub
LM Format Enforcer for decoder constraints
Filter next-token options so generation follows a JSON schema or regular expression.
Key Takeaways
- The library matches characters accepted by a parser to token prefixes in a tokenizer.
- Choose a tokenizer and supported decoding integration before working on the schema.
- Start with a schema that has a nested object and an optional field.
- The caller needs access to a compatible decoding integration.
How it works
The library matches characters accepted by a parser to token prefixes in a tokenizer. Integrations use the resulting masks during generation, leaving room for multiple valid ways to produce the same structure.
What you need
Choose a tokenizer and supported decoding integration before working on the schema. Exact accepted tokens depend on how that tokenizer represents the characters the parser expects.
What to try first
Start with a schema that has a nested object and an optional field. Test outputs with whitespace and escaped characters, then check whether the integration returns complete data at the token limit.
If using streaming, feed each emitted fragment to the client parser. A correct final object can still arrive in fragments that the consumer must buffer.
Where to look for failures
The caller needs access to a compatible decoding integration. A model may still invent data while following the schema, and interruption can leave a partial object.
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.
- Generate the variable parts of a JSON document while code supplies the fixed syntax. Jsonformer for schema-shaped local extraction
- Apply structured-output constraints inside a model inference engine. XGrammar for grammar-constrained decoding
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 ↗