FDEInterviews logo
EMNLP 2023

Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning

The 2023 EPFL paper showing that masking a model's tokens with a grammar guarantees the output's shape, lifts accuracy without training, and has a trap: valid but empty.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 2023
AUTHORS
Geng, Josifoski, Peyrard, West
FROM
EPFL, Université Grenoble Alpes
PUBLISHED
May 2023 · EMNLP 2023
CITED BY
331Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 22% of this breakdown.

01

The paper in one minute

Language models generate text one token at a time, choosing from their whole vocabulary at every step. That is fine for prose and a problem for structure. Ask a model to list facts using only entities from Wikidata and it will write a plausible name that is not in Wikidata. Ask it for a parse tree and the brackets may not balance. The usual fixes were fine-tuning a model per task or validating after the fact and retrying.

The EPFL team used grammar-constrained decoding instead. You write a formal grammar describing every valid output. At each step, an incremental parser looks at what has been generated so far and returns the set of tokens that could still lead to a valid output. Every other token is masked before the model chooses. The output is valid by construction.

Their contribution is showing how general this is and adding one idea: input-dependent grammars. Instead of one grammar for all inputs, build the grammar from the input, for example listing only the candidate entities for this particular mention. With four examples in the prompt and no fine-tuning, LLaMA models improved sharply on closed information extraction and entity disambiguation, and LLaMA-33B beat a purpose-built extractor on extraction F1. On constituency parsing, constraints guaranteed 100% valid trees, though accuracy stayed far below dedicated parsers.

They also found a problem nobody had described: under constraints, the model's highest-scoring output was often the empty string.

THE IDEA, VISUALLY
AT EACH STEP, THE GRAMMAR MASKS WHAT CANNOT BE VALID step 1 Acme Akme <end> step 2 Corp Corporation Bank step 3 <end> Ltd Output: Acme Corporation Allowed tokens come from a grammar. Here it lists the entities valid for this input, so a near-miss like “Acme Corp” is impossible. ENTITY DISAMBIGUATION, LLAMA-33B average micro-accuracy over six datasets, Table 2 54.1 unconstrained 68.2 one grammar 80.3 grammar per input supervised ReFinED 89.4
Structure enforced at the source. At every step the grammar removes tokens that could not lead to a valid output, so a misspelled or invented entity is never generated.
Research notes: the problem, approach and results
54 → 80%

entity disambiguation accuracy for LLaMA-33B, unconstrained against a grammar built per input

100%

valid parse trees with an input-dependent grammar, while parse accuracy stayed near 55% F1

17.5 → 36.0F1

closed information extraction for LLaMA-33B, past a weakly supervised extractor at 34.8

THE PROBLEM

Models asked for structured output (entity lists, typed fields, parse trees) produced near-misses: invented entity names, invalid structure, wrong formats.

THE IDEA

At each decoding step, ask a parser which tokens could still lead to a valid output under a formal grammar, and mask everything else. Build the grammar from the input when the valid answers depend on it.

THE RESULT

Large gains on information extraction and entity linking with no fine-tuning, beating a trained extractor; guaranteed valid parse trees; and a newly documented failure where the best valid output is empty.

02

Why it matters to an FDE

Most enterprise LLM work ends in structure: extracted fields that go into a database, function calls with typed arguments, JSON consumed by another service. Hosted APIs now offer "structured outputs" that constrain generation to a JSON Schema, and open-source servers support grammars. This paper explains what those features do and what they cannot do.

Valid is not correct. The constituency parsing results make the point starkly: constraints produced valid parse trees every time, and the trees were right about 55% of the time by F1, against 95% for supervised parsers. A schema guarantees that a field called amount contains a number. It does not guarantee that it is the right number. Your evaluation still has to check content.

Constrain to the input, not just the type. The biggest gains came from building the grammar from the input. Entity linking jumped from 68.2% with one grammar for all inputs to 80.3% when each input got its own candidate list. On a deployment, that means enums of the customer's actual product codes, account IDs from the record being processed, or the list of valid statuses for this workflow, rather than "any string".

Watch for valid but empty. If the schema allows an empty list or optional fields, constrained decoding can prefer the empty answer, because forcing the model through an unnatural structure lowers the probability of every full answer. The paper's fix is a decoding setting; the deployment fix is to monitor the rate of empty outputs.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 49 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦3 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The ideas to keep, how a grammar masks tokens, the results by task and the misreadings to avoid

    Free account
  2. 04How it works

    Grammars, the incremental parser, input-dependent grammars and the empty-output trap, with a diagram

    Premium
  3. 05The numbers that matter

    Every result by task and model size, the latency cost, and where the method fell short

    Premium
  4. 06Try it yourself

    A toy constrained decoder that shows the invented-entity fix and reproduces the empty-output trap

    Premium
  5. 07Where it breaks

    The limits the authors list, and what changed once providers shipped structured output

    Premium
  6. 08Using it on a deployment

    Schemas from the input, monitoring empties, and validating content after the format is guaranteed

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →