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

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.