NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning

Saibo Geng, Martin Josifoski, Maxime Peyrard, Robert West

Introduction

Large Language Models (LLMs) have demonstrated remarkable proficiency in generating free-form text. However, they often struggle when tasked with producing highly structured outputs, such as database triplets, parse trees, or code, without extensive and expensive finetuning. This paper introduces a framework for Grammar-Constrained Decoding (GCD), a method that leverages formal grammars to prune a model's output space at each decoding step. By ensuring 100% structural validity at inference time, GCD allows off-the-shelf models like LLaMA to outperform task-specific finetuned models on complex structured tasks using only few-shot prompting.

The Challenge of Structured Output

In tasks like Closed Information Extraction (cIE), a model must extract facts as triplets (Subject, Relation, Object) where every element must correspond to an entry in a specific database, such as Wikidata. Standard LLMs typically fail in this domain for two primary reasons:

  1. Structural Coherence: Models frequently fail to maintain the required syntax, such as forgetting closing brackets or misformatting the sequence.
  2. Vocabulary Constraints: The "output vocabulary" for these tasks can be immense—for instance, Wikidata contains over 2.7 million entities. Fitting such a large set of valid candidates into a standard few-shot prompt is impossible.

Traditionally, the solution has been supervised finetuning, which requires massive labeled datasets and significant computational resources. This research explores an alternative: forcing the model to be correct at inference time without modifying its weights.

Methods: Grammar-Constrained Decoding (GCD)

The core of the proposed approach is the integration of an incremental parser into the autoregressive generation process. Typically, an LLM predicts the probability of the next token from its entire vocabulary. With GCD, the parser acts as a "completion engine" that monitors the current prefix. Based on the rules of a formal grammar (Context-Free Grammars), it identifies which tokens are legally allowed to follow and masks out all illegal tokens. This forces the LLM to choose only from the valid subset.

Overview of Grammar-Constrained Decoding (GCD) for Closed Information Extraction.

Key Innovations

  • Input-Dependent Grammars (IDG): While previous methods used static grammars (e.g., for JSON), the authors introduce grammars that adapt based on the input. For Entity Disambiguation, the grammar restricts the model to a specific set of candidate entities relevant to the input sentence.
  • Unified Framework: The authors demonstrate that 14 different NLP tasks can be defined as formal grammars, providing a universal interface for structured extraction.
  • Grammatical Framework (GF): By using GF, the authors decouple the abstract logic of the grammar from the concrete token IDs of specific models, making the approach portable across different LLM architectures.

Experimental Evaluation

The framework was evaluated on three distinct structured tasks:

  1. Closed Information Extraction (cIE): Using the SynthIE-text dataset.
  2. Entity Disambiguation (ED): Tested across six standard benchmarks, including AIDA-CoNLL and WIKI.
  3. Constituency Parsing (CP): Evaluated on the Penn Treebank (PTB).

Key Results

The results indicate that "constrained inference" can be more effective than "unconstrained training" for many applications.

In the cIE task, LLaMA-33B equipped with GCD achieved an F1 score of 36.0, surpassing the supervised GenIE T5-base model (34.8) despite having no task-specific training. For Entity Disambiguation, the use of Input-Dependent Grammars improved LLaMA-33B's average accuracy from 54.1% to 80.3%.

Performance comparison on Closed Information Extraction (cIE). LLaMA-33B with GCD outperforms the supervised GenIE model.

Furthermore, in Constituency Parsing, GCD ensured 100% structural validity of the output parse trees, whereas unconstrained LLaMA models produced valid trees only ~64% of the time. Importantly, the computational overhead is minimal; for most tasks, the added latency was only 1–4ms per token, making it a viable solution for real-time applications.

Discussion and Limitations

While GCD offers a powerful way to enforce structure, it does have limitations. It requires access to the model's logit distribution, meaning it cannot currently be used with "black-box" APIs like GPT-4 unless they provide a constrained decoding interface. Additionally, while GCD ensures a parse tree is syntactically correct, it does not inherently improve the model's semantic understanding of the syntax; LLaMA still trailed behind bespoke, supervised models in parsing accuracy (F1 score).

Future work will focus on scaling grammars to even larger rule sets and exploring more principled ways to handle tokenization ambiguity, where multiple token sequences can represent the same underlying string.

Summary updated

BibTeX
@inproceedings{geng2023grammar,
  author = {Saibo Geng and Martin Josifoski and Maxime Peyrard and Robert West},
  title = {{Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.emnlp-main.674},
}
Figure