skillfed

lm-format-enforcer

Enforce the output format (JSON Schema, Regex etc) of a language model

lm-format-enforcer v0.11.3 3.6M downloads/30d#2,578 on PyPI2,028
Permissive license MIT Active released

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-enforcer

uv

uv add lm-format-enforcer

poetry

poetry add lm-format-enforcer

Installing 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

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

Tags

language model output format enforcementjson schema validation for llmconstrained token generationstructured output from language modelsregex-constrained text generationllm format controlguided decoding
llm-inferencestructured-outputtoken-filtering

More Python Modules packages

Further reading