lm-format-enforcer
Enforce the output format (JSON Schema, Regex etc) of a language model
What it is and what it does
lm-format-enforcer is a token-filtering library that constrains language model generation to produce valid output matching a specified format. Rather than post-processing or prompt engineering, it operates at the token level during inference, filtering which tokens the model is allowed to emit at each step. This ensures the output strictly conforms to JSON Schema, regular expressions, or other structured formats while giving the model freedom in whitespace and field ordering to reduce hallucinations.
The library works by combining a character-level parser (for the target format) with a tokenizer prefix tree (representing all possible token sequences). It integrates into existing inference pipelines—transformers, vLLM, LangChain, llama.cpp, and others—without modifying the model or high-level loop. It supports batched generation, beam search, optional JSON fields, and nested structures, making it practical for production use cases where strict output format is required.
Use it for:
- Extract structured data (names, dates, classifications) from language model responses as valid JSON matching a Pydantic schema
- Enforce regex patterns (e.g., phone numbers, email addresses, sentiment labels) in model outputs without post-processing
- Generate valid JSON arrays or objects from language models in batch or beam-search scenarios with different constraints per input
- Integrate format enforcement into vLLM or other inference servers via guided decoding without custom inference code
- Reduce hallucinations by constraining optional fields and allowing the model to control JSON formatting within valid bounds
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Constrains language model token generation to enforce JSON Schema, regex, or other structured output formats, ensuring models produce valid formatted output at every generation step.
Yes. The package solves a real problem—ensuring language models produce valid structured output—with low install friction, active maintenance, no security vulnerabilities, and permissive licensing. It integrates cleanly into existing inference pipelines and supports multiple format types and model frameworks. Install if you need deterministic output format from language models; skip if you only need post-processing validation.
Install
lm-format-enforcer on PyPI
pip
pip install lm-format-enforceruv
uv add lm-format-enforcerpoetry
poetry add lm-format-enforcerInstalling lm-format-enforcer
Before you install
Low friction: pure Python wheel with four stable runtime dependencies (pydantic, interegular, packaging, pyyaml). Active maintenance with recent commits and 2028 GitHub stars. Supports Python 3.8–3.13.
License in practice
MIT license permits commercial and private use with minimal restrictions. No copyleft obligations or attribution requirements beyond license inclusion.
Quickstart
pip install lm-format-enforcer
from pydantic import BaseModel
from lmformatenforcer import JsonSchemaParser
from lmformatenforcer.integrations.transformers import build_transformers_prefix_allowed_tokens_fn
class MyFormat(BaseModel):
field: str
parser = JsonSchemaParser(MyFormat.schema())
prefix_fn = build_transformers_prefix_allowed_tokens_fn(tokenizer, parser)
# Pass prefix_fn to your language model pipeline
Requires a language model and tokenizer integration (e.g., transformers, vLLM, LangChain, llama.cpp). The package provides token filtering; you supply the model.
Verify before relying
- Performance overhead of token filtering during generation compared to unfiltered inference
- Regex coverage: documentation notes manual regex implementation via interegular does not cover 100% of Python regex standard
- Compatibility matrix with specific model architectures and quantization schemes beyond the examples shown
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — pydantic, interegular, packaging, pyyaml |
| Maintenance | actively maintained — 355 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,555,892/month — #2,578 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: lm_format_enforcer-0.11.3-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
llguidanceEnforces structured output from large language…
permissive · top 5,000 on PyPI
outlines-coreOutlines-core provides structured text…
permissive · top 5,000 on PyPI
renderersConverts chat messages to token IDs for LLM…
permissive · top 15,000 on PyPI
stop-sequencerAdds stop-sequence support to Huggingface…
unclear · top 15,000 on PyPI
instructorInstructor wraps LLM APIs to extract validated,…
permissive · top 5,000 on PyPI
vllmvLLM is a high-throughput inference and serving…
permissive · top 5,000 on PyPI
lm-evalUnified framework for evaluating generative…
permissive · top 5,000 on PyPI
llmcompressorllmcompressor optimizes large language models…
permissive · top 15,000 on PyPI
xgrammarConstrains LLM text generation to follow…
permissive · top 5,000 on PyPI
trustcallTrustcall helps LLMs reliably generate and…
permissive · top 5,000 on PyPI