{"id":2103,"library":"lm-format-enforcer","title":"LM Format Enforcer","description":"LM Format Enforcer is a Python library designed to constrain the output of large language models (LLMs) to specific formats like JSON Schema or Regular Expressions. It integrates with popular LLM frameworks such as Hugging Face Transformers and vLLM. The current version is 0.11.3, and it typically releases minor updates frequently to support new integrations or fix compatibility issues.","status":"active","version":"0.11.3","language":"en","source_language":"en","source_url":"https://github.com/noamgat/lm-format-enforcer","tags":["LLM","format enforcement","JSON Schema","Regex","transformers","vLLM","generation constraints"],"install":[{"cmd":"pip install lm-format-enforcer","lang":"bash","label":"Standard installation"},{"cmd":"pip install lm-format-enforcer[vllm]","lang":"bash","label":"Installation with vLLM support"}],"dependencies":[{"reason":"Used for JSON Schema parsing and validation.","package":"pydantic","optional":false},{"reason":"Required for Hugging Face Transformers model integrations.","package":"transformers","optional":false},{"reason":"Required for vLLM engine integration, often installed as an extra.","package":"vllm","optional":true}],"imports":[{"symbol":"JsonSchemaParser","correct":"from lm_format_enforcer.json_schema_parser import JsonSchemaParser"},{"symbol":"RegexParser","correct":"from lm_format_enforcer.regex_parser import RegexParser"},{"note":"The core 'LMFormatEnforcer' class is usually an internal component; users interact with integration-specific builder functions like this one, combined with a parser.","wrong":"from lm_format_enforcer import LMFormatEnforcer","symbol":"build_transformers_prefix_allowed_tokens_fn","correct":"from lm_format_enforcer.integrations.transformers import build_transformers_prefix_allowed_tokens_fn"},{"note":"Similar to transformers integration, vLLM users should import the builder function rather than a direct enforcer class.","wrong":"from lm_format_enforcer import VLLMFormatEnforcer","symbol":"build_vllm_prefix_allowed_tokens_fn","correct":"from lm_format_enforcer.integrations.vllm import build_vllm_prefix_allowed_tokens_fn"}],"quickstart":{"code":"from transformers import AutoTokenizer, AutoModelForCausalLM\nfrom lm_format_enforcer.json_schema_parser import JsonSchemaParser\nfrom lm_format_enforcer.integrations.transformers import build_transformers_prefix_allowed_tokens_fn\nimport torch\n\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\n\n# Define the JSON schema\njson_schema = {\n    \"type\": \"object\",\n    \"properties\": {\n        \"name\": {\"type\": \"string\"},\n        \"age\": {\"type\": \"integer\", \"minimum\": 0},\n        \"isStudent\": {\"type\": \"boolean\"}\n    },\n    \"required\": [\"name\", \"age\", \"isStudent\"]\n}\n\n# Create the parser\njson_parser = JsonSchemaParser(json_schema)\n\n# Build the prefix_allowed_tokens_fn for transformers integration\nprefix_allowed_tokens_fn = build_transformers_prefix_allowed_tokens_fn(tokenizer, json_parser)\n\nprompt = \"Please generate a JSON object describing a person with name, age, and student status:\\n\"\n\n# Encode the prompt\ninput_ids = tokenizer.encode(prompt, return_tensors=\"pt\")\n\n# Generate text with format enforcement\n# GPT2 might not perfectly follow instructions but the *format* will be enforced.\noutput = model.generate(\n    input_ids,\n    max_new_tokens=100,\n    prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,\n    pad_token_id=tokenizer.eos_token_id,\n    do_sample=False, # For deterministic generation where possible\n    num_beams=1\n)\n\n# Decode and print the result\ngenerated_text = tokenizer.decode(output[0], skip_special_tokens=True)\nprint(generated_text)\n# Example output (format enforced):\n# {\"name\": \"Alice\", \"age\": 25, \"isStudent\": true}","lang":"python","description":"This quickstart demonstrates how to enforce a JSON Schema output using a Hugging Face Transformers model. It initializes a tokenizer and model, defines a JSON schema, creates a `JsonSchemaParser`, and then uses `build_transformers_prefix_allowed_tokens_fn` to generate text that strictly adheres to the defined format."},"warnings":[{"fix":"Avoid direct calls to `TokenEnforcer.get_allowed_tokens()`. If you need similar functionality, check the latest integration patterns or raise a GitHub issue. For vLLM integration, ensure `use_bitmask=True` is handled if customizing.","message":"The return type of `TokenEnforcer.get_allowed_tokens()` changed in v0.11.1 to be torch tensor bitmask based for vLLM V1 integration. This is a breaking change if you directly call or rely on the return type of this internal function.","severity":"breaking","affected_versions":">=0.11.1"},{"fix":"Refer to the library's examples and documentation for the correct integration pattern, typically importing `build_transformers_prefix_allowed_tokens_fn` or `build_vllm_prefix_allowed_tokens_fn` from their respective `integrations` submodules.","message":"The primary user-facing API for integrating `lm-format-enforcer` with LLMs (e.g., Transformers, vLLM) involves specific `build_..._prefix_allowed_tokens_fn` functions, rather than directly instantiating a generic `LMFormatEnforcer` class. This is a common point of confusion for new users.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your environment uses a compatible Python version (e.g., Python 3.8, 3.9, 3.10, 3.11).","message":"The library has specific Python version requirements (`>=3.8, <4.0`). Using unsupported Python versions may lead to unexpected errors or installation issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use the officially recommended or latest compatible versions of `transformers` (>=4.38.0) and `pydantic` (>=2.0.0,<3.0.0) as specified in the `pyproject.toml` or `setup.py`.","message":"Compatibility with `transformers` and `pydantic` libraries can be strict. Older versions of `transformers` (pre-4.38.0) and `pydantic` (pre-2.0.0) might cause issues or not be fully supported.","severity":"gotcha","affected_versions":"<0.10.11 for transformers, all for pydantic"},{"fix":"Consult the `lm-format-enforcer` GitHub README and release notes for the recommended vLLM version and any specific flags or configuration needed for stable integration.","message":"When integrating with vLLM, ensure your vLLM version is compatible with the `lm-format-enforcer` version. Specific vLLM versions (e.g., vLLM V1) may require particular `lm-format-enforcer` versions and features like the `use_bitmask` flag.","severity":"gotcha","affected_versions":"All versions with vLLM integration"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}