Constrained generation aims to sample from language models (LMs) conditioned on hard constraints. Existing constrained-generation techniques for nondeterministic finite automaton (NFA) constraints either distort the distribution or sacrifice efficiency. Theoretically, this task reduces to counting the length-n sequences accepted by an NFA (#NFA), and the exact #NFA problem is #P-complete. Recent work has shown that #NFA admits a fully polynomial randomized approximation scheme (FPRAS). Inspired by this result, we propose NFA-LM, a polynomial-time engine for NFA-constrained generation with theoretical guarantees under mild assumptions. Experiments show that NFA-LM efficiently generates high-quality outputs with theoretically bounded approximation error.
Figures & tables
Figure 1: Top : An NFA A over Σ={0,1,2} with ordered states q0≺⋯≺q5 and final state q5 . Bottom : The unrolled NFA Au of A for length n=4 .
Family
Constraint
NFA states
DFA states
kth_last
The k1 -th token from the end is one of the tracked keywords.
Θ(k1)
Θ(2k1)
repeat_after_k
Some tracked keyword repeats k1 tokens apart.
Θ(k1k2)
Θ(k2k1)
at_least_k
Some tracked keyword appears at least k1 times.
Θ(k1k2)
Θ(k1k2)
exactly_once
Some tracked keyword occurs exactly once.
Θ(k2)
Θ(3k2)
left_not_right
Some tracked keyword occurs on the left of the separator but not on the right.
Θ(k2)
Θ(2k2)
right_not_left
Some tracked keyword occurs on the right of the separator but not on the left.
Θ(k2)
Θ(2k2)
Table 1: The ten constraint families and their minimum numbers of NFA and DFA states.
Method
Success
Time (s)
Quality
LLM-only
GPT-5.6 Luna
306 (61.2%)
1.9
2.93
Qwen3.5-2B
190 (38.0%)
1.0
1.89
Gemma-4-E2B
227 (45.4%)
1.0
2.70
XGrammar
Qwen3.5-2B
500 (100.0%)
2.8
1.75
Table 2: Overall results of successful constraint-satisfaction, average elapsed time, and quality. The quality score is computed over instances with constraint-satisfying outputs.
Figure 2: Elapsed time (s) of NFA-LM and Ctrl-G versus the number of NFA states for each constraint family. Crosses denote instances that timed out or encountered out-of-memory errors.
Figure 3: Maximum relative error of NFA-LM against exact HMM constraint probabilities for Gemma-4-E2B.
Large language models (LLMs) are powerful tools that have found applications beyond human-machine interfaces and chatbots. Beside free-form generation, there has been an interest in constrained generation, a setting where LLMs are constrained to generate well-formed outputs with respect to the language defined by a formal grammar. Although appealing, this setting may be over restrictive for downstream applications. For example, many LLM tasks require the model to reason freely before generating its final response in a specific format. In this work, we introduce suffix-constrained generation, a constrained generation setting in which only the end of the response is constrained by a grammar, a scenario that is not supported by existing constrained generation methods. We introduce several suffix-constrained generation algorithms that are based on greedy search. We experiment on several datasets, and show that our approach allows to guarantee suffix constraints without having a negative impact on results, and even improving them in many settings.
Constrained decoding is essential for serving LLMs, ensuring that generated outputs follow specific structures such as JSON schema-formatted function calls. Existing systems are designed for autoregressive models and assume left-to-right generation, masking out invalid next tokens at each step. Diffusion language models, however, break this assumption: they sample multiple positions simultaneously from a fully-factorized mean-field distribution at each denoising step. In this paper, we present an exact and tractable algorithm for sampling from the constrained mean-field posterior under any constraint expressible as a finite automaton. Viewing finite automata as graphical models, we obtain tractable representations of the constrained distribution that enable efficient inference. The approach guarantees constraint satisfaction by construction, supports both greedy and sampling-based decoding, and is compatible with parallel and block-wise decoding under arbitrary remasking schedules. Applying depth-reduction techniques from arithmetic circuit theory, we further reduce sampling depth from linear to logarithmic in the sequence length. Empirical evaluations on Dream-7B and LLaDA-8B show substantial accuracy gains across various tasks including function calling (xLAM, BFCL), planning (Sudoku, Countdown), text-to-SQL (Spider), and math reasoning (GSM-Symbolic), with little inference overhead relative to unconstrained decoding. For example, on BFCL-Live, our approach improves Dream-7B's greedy decoding accuracy from 63.9% to 71.5%, and stochastic sampling accuracy from 22.3% to 69.0%, where the unconstrained baseline collapses, with under 5% wall-clock overhead.
We investigate the learning task of language generation in the limit, but shift focus from the traditional time-of-last-mistake metric of a generator's success to a new notion of "mistake-bounded generation." While existing results for language generation in the limit focus on guaranteeing eventual consistency, they are blind to the cumulative error incurred during the learning process. We address this by shifting the goal to minimizing the total number of invalid elements output by a generation algorithm. We establish a formal reduction to the Learning from Correct Demonstrations framework of Joshi et al. (2025), enabling a general recipe for deriving mistake bounds via weighted update rules. For finite classes, we provide an algorithm that simultaneously achieves an optimal last-mistake time of Cdim(L) and a mistake bound of ⌊log2∣L∣⌋, whereas for the non-uniform setting of countably infinite streams of languages, we prove a fundamental trade-off: achieving logarithmic mistakes O(logi) necessarily precludes convergence guarantees established in prior work. Finally, we show that our framework can be extended to accommodate noisy adversaries and guarantee mistake bounds that scale with the adversary's suboptimality.
Jon Kleinberg, Charlotte Peale, Omer Reingold
Departments of Computer Science and Information Science Cornell University Ithaca, NY · Department of Computer Science Stanford University Stanford, CA