Constrained Decoding of Diffusion LLMs with Context-Free Grammars

Niels Mündler, Jasper Dekoninck, Martin T. Vechev

Abstract

Large Language Models (LLMs) often struggle to maintain syntactic validity in structured outputs like code or JSON, particularly when using non-linear generation paradigms. We present a novel method for enforcing Context-Free Grammar (CFG) constraints on diffusion-based LLMs and multi-region infilling tasks. By reducing the decoding process to a formal language intersection problem between a regular language (representing partial output) and a CFG, we achieve near-perfect syntactic correctness and improved functional performance across C++, JSON, and chemical SMILES benchmarks.

Introduction

While LLMs excel at code synthesis, their probabilistic nature offers no guarantees for syntactic validity. This is especially problematic for nested structures such as balanced parentheses or JSON braces. Existing constrained decoding techniques are primarily designed for autoregressive, left-to-right generation. However, emerging paradigms like Diffusion LLMs (DLMs) and Multi-Region Infilling (MRI) generate or refine tokens non-linearly, rendering standard techniques inapplicable. Previous efforts for these models were limited to simple regular expressions, which cannot capture the recursive nature of modern programming languages.

Methodology

The core of our approach is framing constrained decoding as a "completable" decision problem. At each step of the generation process, the system determines if the current partial output—which may contain multiple "holes" or infilling regions—can be completed into a valid string within the target language $L$.

Overview of the constrained decoding pipeline, showing how proposals are checked against CFG constraints.

We solve this by representing the partial output as a Non-deterministic Finite Automaton (NFA) and the target constraints as a CFG. The problem is thus reduced to checking if the intersection of the NFA and CFG is non-empty. To maintain efficiency, we introduce the $C2F^{+\epsilon}$ Normal Form, which supports epsilon productions and unit rules without the typical $O(n^3)$ computational blowup. Furthermore, we employ an implicit bottom-up search that only expands symbols capable of leading to valid terminal strings, ensuring real-time performance even with large models.

Experimental Setup

We evaluated our method across three domains:

  1. C++ Code Infilling: Based on the HumanEval dataset, adapted for multi-region infilling.
  2. JSON Schema Extraction: Using the NousResearch JSON-Schema dataset for structured data extraction.
  3. Chemical SMILES: A new benchmark requiring valid chemical notation from natural language descriptions.

Results

Our method demonstrates significant improvements in both syntactic and functional correctness. In single-gap (1-MRI) settings, we achieved 100% syntactic correctness for models like StarCoder2, CodeGemma, and DeepSeek.

Performance comparison of vanilla vs. constrained decoding across various models and MRI settings.

Beyond syntax, the method provides a "functional boost." By pruning syntactically impossible paths, the model focuses its probabilistic budget on semantically meaningful tokens, improving functional correctness in C++ tasks by up to 7%. For Diffusion LLMs, syntactic correctness in C++ rose from approximately 40% to over 99%. The median runtime overhead for a 7B parameter model was roughly 125%, a manageable trade-off for guaranteed validity.

Conclusion and Future Work

We have demonstrated that formal language theory can be effectively applied to modern non-linear LLM decoding. While highly effective, current limitations include an overapproximation of token limits and a lack of context-sensitivity (e.g., variable declarations). Future work will explore incremental parsing and the integration of more powerful formalisms like context-sensitive grammars.

Summary updated

BibTeX
@inproceedings{mundler2026constrained,
  author = {Niels Mündler and Jasper Dekoninck and Martin T. Vechev},
  title = {{Constrained Decoding of Diffusion LLMs with Context-Free Grammars}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=7Sph4KyeYO},
}
Figure