Introduction: The Granularity Mismatch
Large Language Models (LLMs) do not operate directly on characters. Instead, they process sequences of "subword tokens" generated by schemes like Byte-Pair Encoding (BPE) or WordPiece (MaxMatch). This creates a fundamental challenge for guided generation, where we aim to constrain LLM outputs to follow specific formats like JSON schemas or regular expressions.
While constraints are typically defined at the character level, the model selects tokens. Existing tools often allow the model to select any token sequence that spells out the required characters. However, tokenizers are usually "greedy" or follow strict merge rules. If a model generates a string using a non-canonical sequence of tokens—one the tokenizer would not normally produce—it enters an "out-of-distribution" state. This can lead to degraded performance or "hallucinations" because the model was not trained on those specific token combinations.
Methodology: Tokenization as FSTs
The authors propose a mathematically rigorous solution by representing the tokenization process as a Finite-State Transducer (FST). An FST is a machine that maps one sequence (characters) to another (tokens).
Modeling BPE and WordPiece
Modeling BPE is particularly challenging because it involves global merge rules that do not occur in a simple left-to-right fashion. The authors introduce a "merge gadget"—a small, 3-state FST that simulates a single BPE merge rule. By composing these gadgets in the order of the BPE merge list, they can simulate the entire BPE process.
For WordPiece, they adapt the Aho-Corasick algorithm using special failure transitions to model the greedy left-to-right matching.
Pattern Promotion and Subword-Level Automata
The core technical contribution is the process of Pattern Promotion. By composing a character-level pattern automaton (representing the desired regex or schema) with the tokenization FST, the authors produce a "subword-level automaton." This resulting machine only accepts token sequences that are both valid for the pattern and canonical for the tokenizer.
As shown in the figure above, the process involves starting with an input pattern automaton, composing it with a character-to-subword transducer, and then projecting and minimizing the result to create an efficient subword-level automaton.
Theoretical Implications and Complexity
A significant finding of this work is that BPE—despite its reliance on priority queues and global rules—is actually a regular language operation. This allows it to be represented by finite-state machines, which are more efficient than context-free parsers.
The authors prove that "promoting" a pattern to a BPE-preserving subword automaton can be done in polynomial time relative to the pattern size and vocabulary. This establishes that tokenization-aware guided generation is computationally feasible even for large-scale models.
Conclusion
By formalizing tokenization as finite-state transduction, this paper provides a blueprint for "tokenization-aware" constraints in LLM inference engines. This approach ensures that guided generation remains within the model's training distribution, potentially improving the reliability and quality of structured outputs.
Summary updated