LM Format Enforcer
A Python library that constrains local LLM output to JSON Schema or regex formats, with MIT licensing and support for llama.cpp, Transformers and vLLM.
LM Format Enforcer is an open-source Python library for developers who need a local LLM to return structured data their applications can parse. It constrains text during generation, so a prompt's formatting request doesn't have to carry the whole burden. The library uses the MIT license.
It supports JSON Schema, JSON without a predefined schema, and regular expressions. JSON structures can include required and optional fields, nested objects, arrays and recursive definitions. The model retains control over whitespace and field order within the allowed structure, rather than having to follow one fixed layout.
The library works with Python language models and tokenizers through integrations including Hugging Face Transformers, llama.cpp, vLLM, ExLlamaV2 and NVIDIA TensorRT-LLM. It also connects with LangChain, LlamaIndex and Haystack. For self-hosted services, its vLLM integration makes constrained output available through an OpenAI-compatible server, including schema-based responses and choices from a defined set.
Batch generation and beam search are supported, with separate constraints for each input or beam. It fits into existing Transformers generation pipelines, and its diagnostics can show where formatting constraints pushed a model away from its preferred output.
Format compliance doesn't establish factual accuracy: the model can still produce incorrect content inside valid JSON. Regex support covers only part of the standard. The library needs access to the model's generation probabilities through Python, so it can't enforce output through closed APIs such as OpenAI ChatGPT. JSON Schema support covers the implemented subset, not every feature of the standard.