Capabilities of lm-format-enforcer
mainlm-format-enforcer provides several advantages over other constrained generation libraries:
- Broad Compatibility: Works with any Python language model and tokenizer. Supports
transformers,LangChain,LlamaIndex,llama.cpp,vLLM,Haystack,NVIDIA TensorRT-LLM, andExLlamaV2. - Advanced Generation Support: Supports batched generation and beam searches, where each input/beam can have different tokens filtered.
- Flexible Formats: Supports JSON Schema (including nested fields, arrays, dictionaries, and optional fields), JSON Mode (schemaless), and Regular Expressions.
- Natural LLM Behavior: Unlike libraries that force a rigid structure, LMFE allows the model to control whitespace and field ordering. This reduces hallucinations by letting the model generate tokens in its most natural style.
- Non-Intrusive: Does not modify the high-level loop of the
transformersAPI.