AI
New Method Keeps LLM Outputs Grammatical While Preserving Natural Probability Distributions
Researchers propose a decoding algorithm that enforces grammar constraints on language models without distorting the quality of generated outputs.
Large language models frequently struggle to generate properly formatted outputs like program code, mathematical notation, and structured markup—outputs that absolutely must follow precise rules. Existing approaches to this problem work by constraining which tokens the model can choose at each step, forcing grammatical correctness. However, new research reveals a hidden cost to this strategy.
The Problem with Current Grammar Enforcement#
Traditional grammar-constrained decoding (GCD) enforces output validity by restricting token choices during generation. While this guarantees grammatical results, it distorts how likely different valid outputs appear compared to what the model actually learned. Outputs may be syntactically correct but appear with probabilities that diverge significantly from the model's true preferences—resulting in lower-quality text that reads as unnatural or off-target.
A Better Approach: Grammar-Aligned Decoding#
Researchers at arXiv (2405.21047) propose grammar-aligned decoding (GAD), a new framework that preserves the model's probability distribution while still guaranteeing grammatical output. Their algorithm, called ASAp (adaptive sampling with approximate expected futures), uses prior samples to predict which output prefixes can lead to grammatically valid completions.
How ASAp Works#
Rather than cutting off invalid token choices mid-generation, ASAp:
- Estimates the likelihood that different partial outputs can eventually form grammatical results
- Samples tokens according to the model's true conditional distribution (given the grammar constraint)
- Guarantees the final output obeys the grammar rules
This approach ensures the model's learned probability preferences are respected while maintaining structural validity.
Real-World Impact#
Testing on code generation and structured natural language tasks shows ASAp often produces outputs with higher likelihood scores (according to the model's distribution) than existing grammar-constrained methods—all while still enforcing grammatical requirements. This suggests the technique generates both more natural-sounding and more grammatically sound results.

