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

Guiding LLMs The Right Way: Fast, Non-Invasive Constrained Generation

Luca Beurer-Kellner, Marc Fischer, Martin T. Vechev

TL;DR

DOMINO is a novel constrained decoding algorithm that ensures Large Language Model (LLM) outputs strictly follow formats like JSON or Python without sacrificing speed or intelligence. By solving the "token misalignment" problem—where forcing a format accidentally makes the model "dumber"—DOMINO maintains full task accuracy while achieving up to a 2x speedup over unconstrained generation.

What Problem Does This Paper Address?

As LLMs are integrated into production pipelines, we often need them to produce structured data (e.g., JSON, XML, or specific code syntax). "Constrained decoding" is the technique used to force this by masking out invalid tokens during the generation process.

However, the authors identify two major flaws in current approaches:

  1. Efficiency: Checking every possible token against a complex grammar at every step is computationally expensive, creating significant latency.
  2. Token Misalignment (The "Invasiveness" Problem): LLMs "see" text as subword tokens, not individual characters. If a constraint forces the model to pick a sub-optimal token (e.g., choosing a space token and then a quote token instead of a single "space+quote" token), it can derail the model's internal logic. This leads to higher "perplexity" (uncertainty) and a measurable drop in reasoning accuracy.

Methods

The authors introduce DOMINO, an algorithm designed to be "minimally invasive" and highly efficient. Its core components include:

  • Scanner/Parser Separation: Like a traditional compiler, DOMINO splits the task. A "scanner" handles low-level regex (like identifying a string or an integer), while a "parser" handles the high-level structure (the CFG).
  • Vocabulary-Aligned Subterminal Trees: This is the "secret sauce." DOMINO pre-computes how every token in the LLM's vocabulary fits into the grammar. These are stored in prefix trees, allowing the engine to determine valid tokens at runtime with almost zero overhead.
  • Speculative Decoding: Since structured formats (like JSON keys) are highly predictable, DOMINO uses a simple, count-based model to "guess" the next few tokens. If the guesses are valid, the LLM verifies them in a single forward pass, significantly boosting throughput.
  • Opportunistic Masking: Instead of calculating a full mask for every step, DOMINO first checks if the LLM’s top choice is valid. If it is, it proceeds immediately, saving time.

Datasets

The researchers evaluated DOMINO using several benchmarks, converting the tasks into structured JSON formats:

  • GSM8K: A standard benchmark for grade-school math word problems (testing reasoning).
  • CoNLL-2003: A named-entity recognition (NER) dataset.
  • Custom Grammars: They tested the system against complex structures including the C programming language, XML with schemas, and various JSON schemas.

Key Insights and Ideas

  • The "Bridge Token" Discovery: The paper highlights that many tokens "bridge" multiple parts of a grammar (e.g., a single token that represents a comma, a newline, and a space). If a decoder isn't "subword-aligned," it misses these efficient tokens, forcing the model into a "unnatural" state that degrades performance.
  • Constraints as an Accelerator: Counter-intuitively, the authors show that constraints can make LLMs faster. Because the grammar limits the search space, speculative decoding can skip over predictable structural elements (like ": " in JSON) without needing the full power of the LLM for every character.
  • Minimal Invasiveness: They define a "minimally invasive" decoder as one that produces the exact same token sequence as an unconstrained model, provided that sequence is valid. DOMINO is the first to achieve this for context-free grammars (CFGs).

Key Results

  • Accuracy: On GSM8K with Mistral 7B, DOMINO achieved 41.8% accuracy, actually slightly outperforming unconstrained decoding (41.5%) and significantly beating other constrained methods like Guidance (34.5%), which suffered from token misalignment.
  • Speedup: DOMINO achieved massive throughput gains. On the CoNLL2003 task with Llama-2 13B, it was 2.71x faster than unconstrained decoding.
  • Zero Overhead: Unlike previous state-of-the-art tools (like llama.cpp or outlines), DOMINO adds virtually no latency to the generation process thanks to its pre-computation strategy.

Limitations and Future Work

  • Pre-computation Time: While the runtime is fast, the initial "compilation" of the grammar and vocabulary alignment takes time (ranging from 1 to 20 seconds in their tests). This makes it less suitable for grammars that change every single request.
  • Lookahead Parameter ($k$): The algorithm relies on a lookahead parameter to avoid "dead ends" in tokenization. If $k$ is set too low, the model might still be forced into sub-optimal token choices.
  • Speculative Model Simplicity: The current speculative model is a simple count-based prior. The authors suggest that more sophisticated (but still lightweight) models could drive even higher speedups for complex languages like C or Python.

Summary updated

BibTeX
@inproceedings{beurerkellner2024guiding,
  author = {Luca Beurer-Kellner and Marc Fischer and Martin T. Vechev},
  title = {{Guiding LLMs The Right Way: Fast, Non-Invasive Constrained Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=pXaEYzrFae},
}
Figure