Lune

NeurIPS2024Top-tier venue

Grammar-Aligned Decoding

Kanghee Park, Jiayu Wang, Taylor Berg-Kirkpatrick, Nadia Polikarpova, Loris D'Antoni

Abstract

Large Language Models (LLMs) struggle with reliably generating highly structured outputs, such as program code, mathematical formulas, or well-formed markup. Constrained decoding approaches mitigate this problem by greedily restricting what tokens an LLM can output at each step to guarantee that the output matches a given constraint. Specifically, in grammar-constrained decoding (GCD), the LLM's output must follow a given grammar. In this paper, we demonstrate that GCD techniques (and in general constrained decoding techniques) can distort the LLM's distribution, leading to outputs that are grammatical but appear with likelihoods that are not proportional to the ones given by the LLM, and so ultimately are low-quality. We call the problem of aligning sampling with a grammar constraint, grammar-aligned decoding (GAD), and propose adaptive sampling with approximate expected futures (ASAp), a decoding algorithm that guarantees the output to be grammatical while provably producing outputs that match the conditional probability of the LLM's distribution conditioned on the given grammar constraint. Our algorithm uses prior sample outputs to soundly overapproximate the future grammaticality of different output prefixes. Our evaluation on code generation and structured NLP tasks shows how ASAp often produces outputs with higher likelihood (according to the LLM's distribution) than existing GCD techniques, while still enforcing the desired grammatical constraints.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

26 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • Diffusion-LM Improves Controllable Text Generation cites COLD Decoding: Energy-based Constrained Text Generation with Langevin Dynamics
  • Taming Imperfect Process Verifiers: A Sampling Perspective on Backtracking cites Constrained Language Models Yield Few-Shot Semantic Parsers
  • Taming Imperfect Process Verifiers: A Sampling Perspective on Backtracking cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
  • Taming Imperfect Process Verifiers: A Sampling Perspective on Backtracking cites Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective
  • Synchromesh: Reliable Code Generation from Pre-trained Language Models cites Constrained Language Models Yield Few-Shot Semantic Parsers
  • DINGO: Constrained Inference for Diffusion LLMs cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
  • Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective cites COLD Decoding: Energy-based Constrained Text Generation with Langevin Dynamics
  • Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective cites Diffusion-LM Improves Controllable Text Generation
  • Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective cites Constrained Language Models Yield Few-Shot Semantic Parsers
  • Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
  • Constrained Sampling for Language Models Should Be Easy: An MCMC Perspective cites Grammar Prompting for Domain-Specific Language Generation with Large Language Models
  • Grammar Prompting for Domain-Specific Language Generation with Large Language Models cites Constrained Language Models Yield Few-Shot Semantic Parsers
  • Grammar Prompting for Domain-Specific Language Generation with Large Language Models cites Synchromesh: Reliable Code Generation from Pre-trained Language Models
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers26

  1. Apertus: Democratizing Open and Compliant LLMs for Global Language EnvironmentsAlejandro Hernández-Cano, Alexander Hägele, Allen Hao Huang, Angelika Romanou et al.ACL 2026 · 51 citations
  2. DINGO: Constrained Inference for Diffusion LLMsTarun Suresh, Debangshu Banerjee, Shubham Ugare, Sasa Misailovic et al.NeurIPS 2025 · 17 citations
  3. Taming Imperfect Process Verifiers: A Sampling Perspective on BacktrackingDhruv Rohatgi, Abhishek Shetty, Donya Saless, Yuchen Li et al.ICLR 2026 · 15 citations
  4. Constrained Sampling for Language Models Should Be Easy: An MCMC PerspectiveEmmanuel Anaya Gonzalez, Sairam Vaidya, Kanghee Park, Ruyi Ji et al.NeurIPS 2025 · 15 citations
  5. Equality Saturation Theory Exploration à la CarteAnjali Pal, Brett Saiki, Ryan Tjoa, Cynthia Richey et al.OOPSLA 2023 · 11 citations

Builds on12

  1. Diffusion-LM Improves Controllable Text GenerationXiang Lisa Li, John Thickstun, Ishaan Gulrajani, Percy Liang et al.NeurIPS 2022 · 1,546 citations
  2. COLD Decoding: Energy-based Constrained Text Generation with Langevin DynamicsLianhui Qin, Sean Welleck, Daniel Khashabi, Yejin ChoiNeurIPS 2022 · 217 citations
  3. Synchromesh: Reliable Code Generation from Pre-trained Language ModelsGabriel Poesia, Alex Polozov, Vu Le, Ashish Tiwari et al.ICLR 2022 · 200 citations
  4. Grammar Prompting for Domain-Specific Language Generation with Large Language ModelsBailin Wang, Zi Wang, Xuezhi Wang, Yuan Cao et al.NeurIPS 2023 · 138 citations
  5. Constrained Language Models Yield Few-Shot Semantic ParsersRichard Shin, Christopher H. Lin, Sam Thomson, Charles Chen et al.EMNLP 2021 · 131 citations

Related papers

  1. Flexible and Efficient Grammar-Constrained DecodingKanghee Park, Timothy Zhou, Loris D'AntoniICML 2025
  2. Guiding LLMs The Right Way: Fast, Non-Invasive Constrained GenerationLuca Beurer-Kellner, Marc Fischer, Martin T. VechevICML 2024 · 93 citations
  3. Approximately Aligned DecodingDaniel Melcer, Sujan Kumar Gonugondla, Pramuditha Perera, Haifeng Qian et al.NeurIPS 2025 · 3 citations
  4. Grammar-Constrained Decoding for Structured NLP Tasks without FinetuningSaibo Geng, Martin Josifoski, Maxime Peyrard, Robert WestEMNLP 2023 · 33 citations
  5. The Hidden Cost of Structured Generation in LLMs: Draft-Conditioned Constrained DecodingAvinash Reddy, Thayne Walker, Jaime Ide, Amrit Singh BediICML 2026 · 6 citations
Dusk over the sea between two cliffs drawn in fine vertical lines