--- id: lm-format-enforcer version: "0.11.3" license: MIT license_treatment: permissive maintenance: active --- # lm-format-enforcer — Enforce the output format (JSON Schema, Regex etc) of a language model License: permissive · Maintenance: active · Downloads: 3.6M/mo ## 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 above — 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 pip install lm-format-enforcer uv add lm-format-enforcer 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_current - Install friction: low - Maintenance: active - Downloads: 3.6M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags language model output format enforcement, json schema validation for llm, constrained token generation, structured output from language models, regex-constrained text generation, llm format control, guided decoding, llm-inference, structured-output, token-filtering [View on SkillFed](https://skillfed.io/packages/lm-format-enforcer) · [View on PyPI](https://pypi.org/project/lm-format-enforcer/)