NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Automata-based constraints for language model decoding

Terry Koo, Frederick Liu, Luheng He

TL;DR

This paper introduces a novel approach to constrain language models (LMs) to generate output conforming to formal languages (like JSON or API calls) by reformulating the entire problem using automata theory. This method significantly speeds up constraint compilation and offers provable correctness, addressing key limitations of prior work.

What Problem Does This Paper Address?

Large language models (LMs) are increasingly used to generate structured outputs like API calls, code, or JSON. While LMs can be fine-tuned for syntax, they don't guarantee perfect conformance, especially smaller models or for niche formats. This leads to downstream parsing errors. A key challenge is that standard LM tokenizers (which break text into sub-word units) are often misaligned with the formal grammar's structure, making it difficult to impose constraints directly. Previous methods often involve bespoke solutions that are slow, error-prone, or hard to extend.

Methods

The core innovation is framing constrained LM generation within automata theory. The authors reformulate the detokenization process (converting LM tokens back to characters) as a Finite-State Transducer (FST). This FST is then composed with a Finite-State Automaton (FSA) representing the desired formal language (e.g., a regular expression). This composition creates a single automaton that understands both the LM's tokenization and the target grammar. This allows for efficient, closed-form constraint application during decoding. They also extend this to deterministic context-free languages using Pushdown Automata (PDAs).

Key techniques include:

  • Detokenization as FST: Representing the mapping from token sequences to character sequences as an FST.
  • FST-FSA Composition: Combining the detokenizing FST with a grammar-representing FSA to create a unified constraint automaton. This allows tokens to "cross" grammar boundaries.
  • Automata-theoretic approach: Leveraging well-established, optimized algorithms for automata manipulation (like FST composition) for speed and correctness.
  • Extensions: Introducing special regex syntax for efficient wildcard matching and syntactic sugar for complex grammars.

Datasets

Not discussed in the paper. The paper focuses on the methodology and its application to general formal languages rather than specific training datasets. Evaluation is performed using various constraints on Gemma models.

Key Insights and Ideas

  • Tokenization is the bottleneck: The misalignment between LM tokenizers and formal grammars is the primary hurdle for constrained generation.
  • Automata theory as a unifying framework: By treating both detokenization and grammar constraints as automata, the problem becomes tractable and solvable with existing, powerful tools.
  • FST-FSA composition is powerful: This composition elegantly handles the tokenization mismatch, allowing tokens to span grammar elements, which is crucial for correctness and avoiding scoring distortions.
  • Efficiency through reformulation: Reformulating the problem in automata-theoretic terms allows leveraging highly optimized libraries (like OpenFST), leading to dramatic speedups in constraint compilation.
  • Modularity: The FST (vocabulary-specific) and FSA/PDA (grammar-specific) components can be swapped out, making the system highly extensible.

Key Results

  • Compilation Speedup: The proposed system achieves compilation speedups of up to ~7,000x compared to prior work (Outlines).
  • Provable Correctness: The approach is formally proven to be correct, ensuring that the generated output strictly adheres to the specified constraints.
  • Per-Step Overhead: While the compilation speedup is dramatic, the per-step overhead (applying constraints during decoding) is also competitive, with the paper reporting a few microseconds per step.
  • Improved Conformance: For Gemma models, constraints led to perfect output conformance on tasks like JSON schema generation, where unconstrained models struggled.

Limitations and Future Work

  • Deterministic PDAs: For context-free grammars, the system requires deterministic PDAs. The authors acknowledge that ensuring determinism can be challenging and suggest exploring existing literature on deterministic grammars for insights.
  • Finite Budget: The current approach is most effective for constraints with a finite maximum budget of decoded tokens. The authors suggest investigating longer-running, multi-phase constraints where PDAs might offer more benefits.
  • Efficiency of Representation: While PDAs are more expressive, they can be less compact than equivalent FSAs. Further research into PDA representation efficiency is suggested.
  • GPU Optimization: The paper mentions that further optimization is possible, including GPU acceleration for FST operations.

Summary updated

BibTeX
@inproceedings{koo2024automata,
  author = {Terry Koo and Frederick Liu and Luheng He},
  title = {{Automata-based constraints for language model decoding}},
  booktitle = {COLM},
  year = {2024},
  url = {https://openreview.net/forum?id=BDBdblmyzY},
}
Figure