LM Enforcer ensures precise output formats from language models using token filtering. Supports JSON Schema, regex, batching/beam search. Integrates with HuggingFace, LangChain, etc. Lets models control whitespace & ordering to reduce hallucinations.
LM Enforcer is an open-source library that enforces precise output formats like JSON Schema and regular expressions from large language models. It ensures models generate text conforming to the required structure while allowing flexibility in aspects like whitespace and field ordering to reduce hallucinations.
🔣 Works by filtering tokens models that can generate at each timestep
🤗 Integrates into HuggingFace, LangChain, LlamaIndex and more
⚖️ Supports batching, beam search and streaming
📐 Covers JSON Schema, JSON and regex formats
❌ Minimizes limitations on models by giving control over non-critical formatting
🔎 Diagnostic tools to detect aggressive enforcement and prompts
Whether you want to build an API backend that responds with structured data or reduces hallucinations, LM Enforcer equips models to handle precise formats reliably. By keeping models "in the loop", LM Enforcer balances structure with flexibility elegantly.
- 📋 Output Control - Enforces precise text formats like JSON and regex from LLMs.
- 🔬 Observability - Logs token scores reveal model struggles from constraints.
- 🎯 Accuracy - Format compliance reduces costly invalid outputs.
- ⚙️ Compatibility - Works with HuggingFace, LangChain, LlamaIndex, and more.
- 🛡 Reliability - Critical for business apps needing structured data from models.
In summary, LM Format Enforcer provides guardrails for engineers to build consistent and robust solutions needing structured outputs from language models. By guaranteeing format compliance, quality and governance increase dramatically.
- 👷🏽♀️ Builders: Noam Gat, Benedikt Fuchs
- 👩🏽💻 Contributors: 2
- 💫 GitHub Stars: 286
- 🍴 Forks: 12
- 👁️ Watch: 2
- 🪪 License: MIT
- 🔗 Links: Below 👇🏽
- GitHub Repository: https://github.com/noamgat/lm-format-enforcer
- Profile at The AI Engineer: https://github.com/theaiengineer/awesome-opensource-ai-engineering/blob/main/libraries/lmformatenforcer.md
🧙🏽 Follow The AI Engineer for daily insights tailored to AI engineers and subscribe to our newsletter. We are the AI community for hackers!