lm-format-enforcer
Enforce the output format (JSON Schema, Regex etc) of a language model
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires a language model and tokenizer integration (e.g., transformers, vLLM, LangChain, llama.cpp).
- The package provides token filtering; you supply the model.
- Low friction: pure Python wheel with four stable runtime dependencies (pydantic, interegular, packaging, pyyaml).
License · maintenance · safety
MIT (permissive) — MIT license permits commercial and private use with minimal restrictions. No copyleft obligations or attribution requirements beyond license inclusion.
last release 2025-08-24 (355 days) · last repo commit 2026-04-04 · 2,028 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 3,555,892 downloads/mo, #2,578 on PyPI
Alternatives
Verify before relying
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- 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
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 on it.
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
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.
Requires a language model and tokenizer integration (e.g., transformers, vLLM, LangChain, llama.cpp). The package provides token filtering; you supply the model.
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
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 packagespydanticinteregularpackagingpyyaml |
| 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 |
| Classifiers | Development Status :: 3 - AlphaIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python Modules |
Evidence: lm_format_enforcer-0.11.3-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “language model output format enforcement”
- lm-format-enforcerConstrains language model token generation to enforce JSON Schema,…
- llguidanceEnforces structured output from large language models by computing…
- guardrails-aiGuardrails is a Python framework that validates and constrains LLM…
Give your agent the search over MCP, or paste the wish link into any chat.
More Python Modules packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
PyYAML parses and emits YAML 1.1 data format, enabling serialization and deserialization of configuration files and Python objects to and from human-readable YAML text.
Pydantic validates Python data structures against type hints, coercing and checking input at runtime to ensure it matches a declared schema.
Provides reusable metadata objects for use with PEP-593 `typing.Annotated` to express common constraints like bounds, collection sizes, and predicates on types.
Install it if you use or build libraries that need to express type constraints in a standardized, inspectable way—or if you want to annotate your own types with…
Provides runtime tools to inspect and introspect Python type annotations, enabling programmatic examination of type hints at execution time.
See also llguidance · outlines-core · renderers · stop-sequencer · instructor · vllm · lm-eval · llmcompressor · xgrammar · trustcall