LM Format Enforcer - Structured Output Generation for Language Models

[AI Summary]: LM Format Enforcer is a Python library that ensures language models generate output in precise formats (JSON Schema, Regex, etc.) by filtering allowed tokens at each generation step. The library works with popular frameworks including transformers, LangChain, LlamaIndex, vLLM, and others, supporting batched generation and beam searches. Unlike similar tools, it allows language models to control whitespace and field ordering in JSON outputs, reducing hallucinations while maintaining format compliance. Features include support for nested JSON fields, optional fields, arrays, dictionaries, and integration with vLLM’s OpenAI-compatible server for production deployments.

  • Developer: noamgat
  • License: MIT
  • Platform: Python