How to beginner · 3 min read

How to use lm-format-enforcer

Quick answer
Use lm-format-enforcer to validate and enforce structured output formats from language models by defining schemas and applying them to model responses. Integrate it with the OpenAI SDK to ensure your AI outputs conform to expected JSON or custom formats.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0 lm-format-enforcer

Setup

Install the lm-format-enforcer package and set your OpenAI API key as an environment variable.

  • Run pip install lm-format-enforcer openai
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key'
bash
pip install lm-format-enforcer openai

Step by step

Define a JSON schema for the expected output format, then use lm-format-enforcer to validate the model's response. Below is a complete example using the OpenAI gpt-4o model.

python
import os
from openai import OpenAI
from lm_format_enforcer import FormatEnforcer

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define the expected JSON schema for the output
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "email": {"type": "string", "format": "email"}
    },
    "required": ["name", "age", "email"]
}

# Create the format enforcer instance
enforcer = FormatEnforcer(schema)

# Prompt for the model
prompt = "Provide a JSON object with name, age, and email of a user."

# Call the OpenAI chat completion
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

# Extract the raw content
raw_output = response.choices[0].message.content

# Enforce the format
try:
    validated_output = enforcer.enforce(raw_output)
    print("Validated output:", validated_output)
except Exception as e:
    print("Format enforcement failed:", e)
output
Validated output: {'name': 'Alice', 'age': 30, 'email': 'alice@example.com'}

Common variations

You can use lm-format-enforcer with different models like gpt-4o-mini or Anthropic's claude-3-5-sonnet-20241022. For async usage, wrap calls with asyncio. You can also customize schemas for nested objects or arrays.

python
import asyncio
import os
from openai import OpenAI
from lm_format_enforcer import FormatEnforcer

async def main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    schema = {
        "type": "object",
        "properties": {
            "title": {"type": "string"},
            "tags": {"type": "array", "items": {"type": "string"}}
        },
        "required": ["title", "tags"]
    }
    enforcer = FormatEnforcer(schema)
    prompt = "Return a JSON with title and tags (list of strings)."

    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )

    raw_output = response.choices[0].message.content
    validated = enforcer.enforce(raw_output)
    print("Validated async output:", validated)

asyncio.run(main())
output
Validated async output: {'title': 'My Blog Post', 'tags': ['AI', 'Python', 'API']}

Troubleshooting

  • If format enforcement fails, check the model output for JSON syntax errors or missing required fields.
  • Use print(raw_output) to debug the raw response.
  • Adjust the schema to be more permissive if the model output varies slightly.
  • Ensure your environment variable OPENAI_API_KEY is set correctly.

Key Takeaways

  • Use lm-format-enforcer to validate and enforce structured JSON outputs from language models.
  • Define clear JSON schemas to specify expected output formats and required fields.
  • Integrate lm-format-enforcer with the OpenAI SDK for seamless validation of AI responses.
  • Use async calls and different models as needed, adjusting schemas for complex data structures.
  • Debug format errors by inspecting raw model outputs and refining your schema.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗