← all repositories

noamgat/lm-format-enforcer

A Python library that enforces structured output formats (JSON Schema, Regex) on language models by filtering allowed tokens during generation.

lm-format-enforcer
Velocity · 7d
+2.0
★ / day
Trend
steady
star history

The library addresses the common problem of language models failing to consistently produce outputs in requested formats. It works by wrapping generation calls and filtering the token vocabulary at each inference step to only allow tokens consistent with the specified schema or regex pattern. This approach ensures format compliance without requiring additional prompt engineering or retraining. It integrates with popular libraries like transformers and supports Pydantic models for defining JSON schemas.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.