[Paper Review] Efficient Guided Generation for Large Language Models
The paper reformulates guided LLM generation as finite-state machine transitions to build a vocabulary index, enabling O(1) average masking per step and extending to regular expressions and CFGs, with an implementation in Outlines.
In this article we show how the problem of neural text generation can be constructively reformulated in terms of transitions between the states of a finite-state machine. This framework leads to an efficient approach to guiding text generation with regular expressions and context-free grammars by allowing the construction of an index over a language model's vocabulary. The approach is model agnostic, allows one to enforce domain-specific knowledge and constraints, and enables the construction of reliable interfaces by guaranteeing the structure of the generated text. It adds little overhead to the token sequence generation process and significantly outperforms existing solutions. An implementation is provided in the open source Python library Outlines
Motivation & Objective
- Motivate constrained text generation from large language models to satisfy rigid formatting and domain-specific constraints.
- Develop a finite-state machine (FSM) based framework to guide generation with regular expressions and CFGs.
- Create an index over the model vocabulary to efficiently determine valid next tokens.
- Extend the approach to context-free grammars and LALR(1) parsers to support structured outputs.
- Demonstrate model-agnostic applicability and low overhead compared to existing methods.
Proposed method
- Represent constraints via FSMs corresponding to regular expressions and parsers.
- Use masking of the LLM output logits to form a conditional distribution that enforces constraints.
- Pre-process the vocabulary to build an index mapping FSM states to valid next tokens (sigma) for O(1) masking at each step.
- Provide algorithms (sample_tokens, sample_tokens with masking, find_sub_sequences, map_states_to_vocab) to enable guided generation.
- Extend the FSM approach to pushdown automata for CFG-based parsing and discuss potential integration with LALR(1) parsers.
- Demonstrate implementation in the Outlines library and compare against existing guidance-based methods.
![Figure 1 : FSM masking for the regular expression ([0-9]*)?\.?[0-9]* .](https://ar5iv.labs.arxiv.org/html/2307.09702/assets/x1.png)
Experimental results
Research questions
- RQ1How can guided generation be executed efficiently when enforcing regular expressions or CFG constraints?
- RQ2Can a finite-state machine based indexing of the vocabulary enable constant-time determination of valid next tokens per step?
- RQ3How can this FSM-based guiding approach be extended from regex constraints to CFGs and practical programming-language grammars (e.g., Python, SQL)?
- RQ4What are the practical trade-offs (memory vs. compute) of constructing and using such indices in real LLM workflows?
Key findings
- The approach achieves O(1) average cost for determining valid next tokens via a vocabulary-state index.
- Masking the LLM outputs with the FSM/CFG-informed mask enables constraint-guided generation with low runtime overhead.
- Index construction is performed offline and uses a map from FSM states to vocabulary subsets, enabling fast runtime masking.
- The method extends beyond regex to CFGs and lays groundwork for integrating LALR(1) parsers to support structured outputs.
- Empirical comparisons indicate substantial efficiency gains over some existing constrained decoding approaches, with an open-source implementation in Outlines.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.