skip to content
hiroshi.dev

Verbatim extraction with byte-level constrained decoding

/ 23 min read

Table of Contents

If you ask a small LLM to extract a passage from a document verbatim, you’ll likely realize that these models are surprisingly bad at the task. Not at just finding the right passage, but especially at copying it out exactly. The flaws are often subtle: maybe it will drop a word, or fix a typo, or straighten a quote mark, or paraphrase the last few lines.

Verbatim information extraction matters anywhere a quote has to be traced back to its source. It could be citing evidence in a report, highlighting the quote on screen for human-in-the-loop (HITL) review, or in editing a file where the text being replaced has to match exactly. This post is about why this happens, why the obvious fix fails in subtle ways, and what it takes to make the fix actually work.

How often models fail to copy

I used seven recent small models (Qwen3.5 0.8B, 2B and 9B, Gemma 4 E2B, LFM2.5 1.2B, LFM3 1.7B, and Granite 4.2 3B) and a realistic extraction task: finding clauses in commercial contracts from CUAD. We give every model 100 contracts and ask each model to find passages for 8 common clause types,1 making for 800 prompts per model. The prompt essentially just says: copy each passage exactly as it appears, character for character.2

The figure below shows what comes back when we check every returned span against the contract.

What small models return when asked to quote

0%25%50%75%100%Qwen3.5-0.8B63.9%Qwen3.5-2B62.2%Gemma 4 E2B54.8%LFM2.5-1.2B51.7%LFM3-1.7B72.3%Granite 4.2 3B75.9%Qwen3.5-9B78.3%
Show table
Runexact copywhitespace changedquotes / case / unicodesmall edits (≥ 90% overlap)not in the source
Qwen3.5-0.8B63.9%21.0%0.7%12.1%2.3%
Qwen3.5-2B62.2%13.7%0.0%23.2%1.0%
Gemma 4 E2B54.8%32.0%0.7%11.5%1.1%
LFM2.5-1.2B51.7%10.8%1.5%23.5%12.5%
LFM3-1.7B72.3%13.3%0.4%11.0%3.1%
Granite 4.2 3B75.9%18.4%0.0%5.7%0.0%
Qwen3.5-9B78.3%17.7%0.3%3.1%0.7%

Whitespace changes are often harmless because we can just collapse the document’s whitespace and find the span again. Everything else, though, is a hallucinated span, because they cannot be found in the source. Depending on the model, that’s 4–38% of spans, and 6–55% of answers contain at least one.

So why would a model that can clearly see the passage fail to copy it? Its because an LLM cannot copy anything, it can only generate. At every step the model produces a distribution over its vocabulary, and the decoder takes the most likely token. To reproduce a passage verbatim, the most likely token has to be the “correct” one at every single step.

If we suppose that each token has a small, independent chance ε of going subtly wrong, then a span of L tokens survives intact with probability

P(exact) = (1 − ε)L ≈ e−εL

The per-token error can be tiny and still dominate. At ε = 0.1%, one slip per thousand tokens, a 10-token party name is right 99% of the time, but a 300-token clause is right only 74% of the time. The chance of an error increases with length, and extraction is a task where outputs are often very long.

We can see this in the data too. The chart below plots the share of hallucinated spans against span length, for each model. For almost every model, the longer the span, the more likely it is to be hallucinated.

Longer spans are more likely to be hallucinated

Share of spans not found in the document.

0%20%40%60%< 88–1616–3232–6464–128128–256256+span length (tokens)average 48.6%
Show table
< 88–1616–3232–6464–128128–256256+
Qwen3.5-0.8B0.0%6.6%8.5%15.6%11.4%27.5%36.2%
Qwen3.5-2B––9.3%16.7%28.2%39.4%69.2%
Gemma 4 E2B–8.3%10.6%9.6%15.5%20.8%–
LFM2.5-1.2B27.9%30.4%35.1%39.9%42.2%52.4%55.0%
LFM3-1.7B30.3%10.2%9.2%11.3%12.1%18.1%34.1%
Granite 4.2 3B––0.0%10.7%4.5%––
Qwen3.5-9B0.0%0.0%3.3%1.8%3.8%9.6%–
average14.6%11.1%10.8%15.1%16.8%28.0%48.6%

Constrained decoding

If we can’t trust the model to copy, the natural fix is to somehow not let the model write anything that isn’t present in the source document. One obvious way to do this is to constrain the next-token selection during inference to only those tokens that guarantee a valid span. This is called constrained decoding. Structured generation libraries (such as those for JSON-schema decoding) do this to guarantee valid grammar. What we want is the same thing, but for the content instead. That means that inside a “value” (in the JSON sense), the only allowed tokens are the ones that keep the value a substring of the document. The model still decides which passage to copy and where to stop, we just don’t allow it to write anything else. Once it starts generating a passage, it has no way to drift.

For almost every token, this changes nothing, because the model’s top choice is already in the document. Here’s a real answer where it does matter:

Where the copy constraint steps in

Three real answers from LFM2.5-1.2B. Percentages are the model’s own next-token probabilities.

The contract says

This Agreement will be governed in all respects by the laws of the State of Delaware, including its conflict with law provisions.

The model has written

… of Delaware, including its conflict

tokenwhat the model wantsafter the constraint␣of53.1%removed: not in the contract␣with46.9%100%

The tokens the constraint allows hold 46.9% of the model’s probability, rescaled to 100%.

✗ Without the constraint“… of Delaware, including its conflict of law provisions.”✓ With the constraint“… of Delaware, including its conflict with law provisions.”

In the first example, the contract says conflict with law provisions, but conflict of law is the far more common phrase, so the model assigns 53% on ␣of. In the absence of the constraint, that is the token that gets picked and the quote is no longer in the contract. With the constraint, ␣of is masked/removed, and ␣with is all that’s left and so the quote stays exact.

The second and third examples are more interesting, because here the model is far more sure of the wrong word. In the second example, it assigns 99.9% on is, where the contract says shall be, and in the third, it assigns 88% on bear where the contract says bare which is a typo. The constraint obviously eliminates these errors as well.

As good as this looks so far, constrained decoding isn’t a silver bullet. Masking doesn’t change what the model wants to generate, it only removes options and renormalizes over what’s left. So the question to ask of any constraint is: how much of the model’s own probability does it keep? If the allowed tokens hold most of the probability, the model barely notices the constraint. If they hold only a sliver, the model is forced to choose among tokens it considers unlikely, and the “best” of those is often a “strange” choice, and leads the model down into a weird decoding trajectory.

Token boundaries

…which brings us to an important concept. An LLM doesn’t see characters, it only sees tokens. Its tokenizer has a fixed vocabulary of text pieces (fixed forever during the training), and it converts text into those pieces using a fixed set of rules. One string can usually be spelled with many different token sequences, and which sequence the tokenizer picks depends on the text surrounding the string. As an example, Qwen3.5’s tokenizer writes ␣Agreement (with its leading space) as a single token, but Agreement at the start of a line as two: Ag + reement.

The sequence the tokenizer picks for a given text is called its canonical tokenization. Every other sequence that spells the same text, such as ␣Ag + reement for ␣Agreement, is non-canonical. Since the model only ever saw canonical tokenizations during training, that is what it both expects to see and wants to predict, and all other tokenizations of the same text get very little probability, even though they decode to exactly the same characters.

Now let’s think about the simplest way to build a copy constraint: tokenize the document once, and let the model write any contiguous run of the document’s tokens. This assumes that a span is tokenized the same way inside the document and inside the model’s output.

The interactive figure below shows how this assumption doesn’t always hold up. Click on any underlined phrase and compare its tokenization within the document and within an output JSON.

Same text, different tokens

Click a phrase. Top: its tokens in the document. Bottom: its tokens in the model’s answer.

This Agreement is made between , the Director of Analytics (""), and Acme Corp. The Licensor's total liability shall not exceed $1,500. A fee of is due within 30 days.

Three things break that assumption:

  1. Leading spaces: BPE tokenizers attach a space to the following word. So in the document, the name starts with the token ␣Michael. But during generation, right after the opening quote, the model wants to write Michael without the leading space which doesn’t exist in our list of constrained tokens.
  2. Punctuation fuses with content: In the output, the opening quote often merges with whatever follows it ([", "$, "() and the closing quote with whatever comes before it (.",, "]). The model wants to generate these, but we don’t have these in our list of constrained tokens either.
  3. Spans sometimes end mid-token: This one is rarer, but still quite possible. About 1% of the gold answers stop inside a document token (for every tokenizer except LFM3’s where it’s 33%). For example, one of the gold answers ends with ("Initial Term"), but the document goes on with a period (("Initial Term"). The …). Qwen3.5’s tokenizer writes the quote + the parenthesis + the period as a single token, ").

So how often does a span survive all this? Let’s take the gold answers for the contracts and tokenize each one twice with each model’s tokenizer, once inside the document and once inside a JSON string.

How often is a span tokenized the same way in the document and in the output?

Share of 464 gold answers.

0%25%50%75%100%Qwen3.50.8B, 2B, 9B0.9%89.9%83.2%Gemma 4 E2B1.9%71.8%82.8%LFM2.5-1.2B1.1%89.9%83.4%LFM3-1.7B0.9%89.9%83.4%Granite 4.2 3B0.9%89.9%83.2%
Show table
identical token IDs in document and JSON outputstart is off by the leading spacea value token fuses with a JSON quote
Qwen3.5 0.8B, 2B, 9B0.9%89.9%83.2%
Gemma 4 E2B1.9%71.8%82.8%
LFM2.5-1.2B1.1%89.9%83.4%
LFM3-1.7B0.9%89.9%83.4%
Granite 4.2 3B0.9%89.9%83.2%

For the vast majority of the cases, a naive constrained decoding over the document’s token sequence will produce a sequence of tokens that the model would never choose.

So what does this do to the model? Say a model writes {"spans": [". Given how often JSON appears in the training, the tokenizer usually writes the opening bracket and the opening quote together, as a single token: ␣[". Our current constraint can’t allow that. It spells the JSON syntax with fixed tokens, ending in ␣[ followed by a separate ", so that the span can start on a document token. The model gives the forced ␣[ a median of only 0–2.6% of its probability, and the " after it only 0–0.4%.3

When thrown into an unknown territory, the easiest way out for the LLM is to not answer at all. Closing the list with ]} is always allowed, so that’s what the model picks. Here’s what that looks like for one real answer:

What the token-ID constraint does to one answer

Qwen3.5-2B, from the point where its answer reads {"spans":. Percentages are the model’s own next-token probabilities.

the answer it’s trying to write{"spans": ["14. Construction. The Joint Venturers declare that in …

{"spans":
␣["95.9%model’s top choice
✗ not allowed
␣[0.087%forced by the
token-ID constraint
122.3%model’s top choice
✗ not allowed
"0.0018%allowed
]}0.015%allowed, 8× likelier
than ", so it’s picked
→ {"spans": []} an empty answer
{"spans": []}an empty answer
token-ID constraint’s pathblocked by the constraint

So essentially, the model starts outputting empty lists. Across our 800 prompts, this ends up happening a lot. Under the current constraints, Qwen3.5-0.8B goes from 7% empty answers to 60%, and LFM3 from 14% to 47%.4 So, even when the constraint is doing its job perfectly, the result are much worse than what we expected.

A byte-level constraint

The current constraints failed because they only allowed one way of spelling each span: the way the document happened to be tokenized. What we actually care about though is the text the model produces, not which token IDs it uses to produce it. So instead of asking if the next token is in the document, we should just check if the token’s text continues some piece of the document.

In other words, we should stop treating the document as a sequence of tokens and treat it as a sequence of bytes. So at every step, a token is allowed if its bytes (+ whatever we’ve written so far) still spell something that appears in the document, or is necessary to create valid JSON. This fixes all three boundary problems at once:

  • Michael is allowed right after the quote, because the bytes Michael occur in the document, one byte after the space.
  • ", is allowed at the end of a span, because the automaton reads " as closing the value and , as the separator.
  • Every word gets every spelling. ␣Reynolds can be written as one token, as ␣Re + yn + olds, or byte by byte. They all spell the same bytes, so they’re all allowed, and the model gets to pick the one it prefers.

Here’s how the two constraints compare at the start, the middle and the end of a span:

Token IDs vs bytes, from the start of a span to its end

Percentages are the model’s own next-token probabilities.

Qwen3.5-2B, starting its answer.

The contract says

… below his signature.↵↵14. Construction. The Joint Venturers declare …

The model has written

{"spans":

tokenwhat the model wantsmodelafter token IDstoken IDsafter bytesbytes␣["95.9%removed98.3%␣[{"2.3%removedremoved␣[0.09%100%0.09%kept0.09%97.5%

The token-ID constraint spells the JSON syntax with fixed tokens, so here it only allows ␣[, and the quote has to follow as a separate token.

In the middle of a span, the two constraints behave the same. They both catch/fix whenever the model drifts away from the document, and they both keep about 98% of the model’s probability on average over the whole answer from before. So bytes never do worse than token IDs; they only ever do better at the edges.

Specifically, at the start, bytes let the model write ␣[" as one token, the way it wants to. At the end, they let it close the span the way it wants to, with .". This fixes the the token-ID constraint where it only allows the document’s next token, .\: the period fused with the line break after it. And so the models start answering again:

Empty answers, by constraint

Share of 800 prompts answered with an empty list.

0%25%50%75%100%Qwen3.5-0.8B6.6%59.5%12.3%Qwen3.5-2B50.0%97.4%58.3%Gemma 4 E2B47.9%45.8%47.4%LFM2.5-1.2B0.5%8.8%0.1%LFM3-1.7B14.1%46.5%25.3%Granite 4.2 3B91.1%99.8%94.6%Qwen3.5-9B46.6%93.8%46.5%56.8%
Show table
unconstrainedcopy document token IDsbyte-level copy constraint
Qwen3.5-0.8B6.6%59.5%12.3%
Qwen3.5-2B50.0%97.4%58.3%
Gemma 4 E2B47.9%45.8%47.4%
LFM2.5-1.2B0.5%8.8%0.1%
LFM3-1.7B14.1%46.5%25.3%
Granite 4.2 3B91.1%99.8%94.6%
Qwen3.5-9B46.6%93.8%46.5%

Under the byte-level constraint, empty answers come back down to near the unconstrained rate.5

Making this fast takes two static data structures:

  • The byte trie for the vocabulary: every token’s bytes form a path from the root, and each node lists the tokens that end there so that the tokens that share a prefix share a path.
  • The suffix array for the document: every position in the document, sorted by the text that starts there. Whatever we’ve copied so far matches one contiguous range of rows in it, [lo, hi): one row for every place in the document where that text appears.6

To find every allowed token, we walk the trie and the suffix array together. We descend into a child only if its byte keeps the range non-empty, and collect the tokens at every node along the way. The cost grows with the number of allowed tokens (as opposed to the vocabulary) because the walk never enters a branch the document cannot continue.

As a bonus, the range also tells us where the span came from. When the value closes, the suffixes left in [lo, hi) are exactly the places in the document it was copied from.

The good part is that you probably don’t need to write any of this yourself. llguidance, the grammar engine behind one of vLLM’s structured-output backends, already works on bytes over a token trie, and it has a little-known regex extension for this exact case: substring_chunks, which accepts any contiguous run of the chunks you give it (it uses a suffix automaton rather than a suffix array). We just give it the JSON-escaped characters of the document.

The complete script is printed below.

extract.py
import json
from vllm import LLM, SamplingParams
from vllm.sampling_params import StructuredOutputsParams
def spans_grammar(text: str) -> str:
chunks = [json.dumps(ch, ensure_ascii=False)[1:-1] for ch in text] # one chunk per JSON-escaped character
return f"""
start: "{{\\"spans\\": [" (span (", " span)*)? "]}}"
span: "\\"" SPAN "\\""
SPAN: %regex {{ "substring_chunks": {json.dumps(chunks, ensure_ascii=False)} }}
"""
contract = open("contract.txt").read()
doc = " ".join(contract.split()) # whitespace-normalized; map spans back to the original with an offset table
prompt = (f"<contract>\n{contract}\n</contract>\n\n"
'Find every passage in the contract above that relates to "Governing Law". '
"Copy each passage exactly as it appears in the contract, character for character. "
'Respond with JSON only: {"spans": ["<passage>", ...]}.')
if __name__ == "__main__": # vLLM starts its engine in a new process
llm = LLM("Qwen/Qwen3.5-2B", max_model_len=16384, structured_outputs_config={"backend": "guidance"})
params = SamplingParams(temperature=0, max_tokens=512,
structured_outputs=StructuredOutputsParams(grammar=spans_grammar(doc)))
out = llm.chat([{"role": "user", "content": prompt}], params, chat_template_kwargs={"enable_thinking": False})
print(out[0].outputs[0].text)

If you’re on vLLM, this is just a config change.7 Most of the habit accommodations in the next section are a few more lines of grammar on top.

Formatting habits

Another problem we face with constrained decoding is that the same constraints cannot be applied to all model families. Beyond tokenization, models also differ in how they like to write. Instruction tuning gives each model family its own style: some pretty-print JSON, some wrap everything in a Markdown code fence, some put quotes around quotes, and almost all of them tidy up whitespace. These habits are often very strong, especially in smaller LLMs.

Each model's house style

Share of unconstrained outputs with each habit.

0%25%50%75%100%Qwen3.5-0.8B68%0%2%70%Qwen3.5-2B18%0%10%44%Gemma 4 E2B53%96%0%94%LFM2.5-1.2B1%0%0%82%LFM3-1.7B100%0%1%38%Granite 4.2 3B47%0%0%59%Qwen3.5-9B21%0%0%46%
Show table
pretty-printed JSONwrapped in a ``` code fenceextra quotes around the passagewhitespace tidied up
Qwen3.5-0.8B68%0%2%70%
Qwen3.5-2B18%0%10%44%
Gemma 4 E2B53%96%0%94%
LFM2.5-1.2B1%0%0%82%
LFM3-1.7B100%0%1%38%
Granite 4.2 3B47%0%0%59%
Qwen3.5-9B21%0%0%46%

Any constraint that forbids a “habit” runs into the same problem as before. At the step where the habit would kick in, the allowed tokens hold almost none of the model’s probability, so the model takes the cheapest legal way out, which is - as we saw before - very often undesirable. Here are three examples from our traces:

Whitespace. Contract text extracted from PDFs is full of irregular whitespace (the 1st of␣␣␣␣␣␣March). The model wants to write a single space, but the constraint only allows either the six spaces the document has, or closing the quote. So the model closes the quote mid-sentence, giving the span "The Joint Venture shall commence on the 1st of␣␣␣␣␣␣", and after that it tends to lose track and wander off.

Wrapping quotes. Qwen3.5-2B likes to escape the quotes with \, and wants to generate "\"The Dolphins shall, at its own expense, ...\"". The leading \" isn’t in the document, so the closest legal move is to start the span at some quoted term that is in the document ("\"Inde), where it immediately gets stuck. What it does next is quite clever/weird: it routes around the constraint through the separator, and ends up writing what it wanted to anyway, just one word per token, because the grammar allows ", " between values:

{"spans": ["\"Inde", "The", "Dolphins", "shall,", "at", "its", "own", "expense,", "maintain", ...]}

Every one of those is a verbatim substring, so the constraint technically did its job, but the output is garbage.

Layout. Granite pretty-prints about half of its answers, but when we force it into compact {"spans": [", it mostly answers {"spans": []}.

Forcing compact JSON doesn’t always hurt though. Gemma 4 has a formatting habit too (its code fence), and it just switches to compact JSON and answers normally. Whether a forbidden habit is costly depends on what the model’s next-best move is.8

The fix in every case is to make the constraint accept the habit, as long as the habit is harmless:

  • Whitespace: most models tidy up whitespace, but some copy tend to copy it exactly, so the constraint has to allow both. We constrain against a normalized view of the document with every whitespace run collapsed to one space, and let the model write any run of whitespace (spaces, tabs or line breaks) wherever the normalized document has a space. A map from each normalized character back to its original offset gives us the span in the original text.9
  • Wrapping quotes: accept an optional \" at each end of a value and strip it afterwards.
  • Layout: allow JSON whitespace between the structural tokens.

Accommodating each model's habits

Span F1 (× 100).

0204060Qwen3.5-0.8B6.48.36.78.412.214.0Qwen3.5-2B32.629.324.745.344.247.3Gemma 4 E2B48.542.954.251.950.454.1LFM2.5-1.2B6.67.99.99.48.88.9LFM3-1.7B14.313.815.115.013.515.3Granite 4.2 3B21.912.412.214.319.823.0Qwen3.5-9B45.045.343.544.845.546.7
Show table
JSON schemacopy constraint alone+ normalized whitespace (one space)+ optional wrapping quotes+ free JSON layout+ any whitespace run (all fixes)
Qwen3.5-0.8B6.48.36.78.412.214.0
Qwen3.5-2B32.629.324.745.344.247.3
Gemma 4 E2B48.542.954.251.950.454.1
LFM2.5-1.2B6.67.99.99.48.88.9
LFM3-1.7B14.313.815.115.013.515.3
Granite 4.2 3B21.912.412.214.319.823.0
Qwen3.5-9B45.045.343.544.845.546.7

Most fixes help the model whose habit they remove and can cost a little for a model that didn’t need them, but not always: LFM3 pretty-prints almost every answer, and yet allowing free layout lowers its F110 from 15.0 to 13.5. Allowing a whitespace run (instead of exactly one space) helps the most where the single space degraded accuracy the max: Granite goes from 19.8 to 23.0 and LFM3 from 13.5 to 15.3. Even Gemma 4 (which tidies up whitespace in almost every answer) gains (50.4 → 54.1), because it still copies some line breaks. So these are knobs to set per model after measuring what/where the model fails on.11 The general rule is to look at what your model writes when it’s unconstrained, and make sure the constraint allows every harmless habit in it.

Results

Putting it all together, here’s the before and after on all 800 prompts per model, with the byte-level constraint and all the habit accommodations, the same for every model:

Hallucinated spans

Share of predicted spans not found in the document.

0%10%20%30%40%Qwen3.5-0.8B15.1%15.7%0.0%Qwen3.5-2B24.1%25.2%0.0%Gemma 4 E2B13.3%15.3%0.0%LFM2.5-1.2B37.5%38.2%0.0%LFM3-1.7B14.4%14.1%0.0%Granite 4.2 3B5.7%6.0%0.0%Qwen3.5-9B4.0%4.3%0.0%
Show table
unconstrainedJSON schemacopy constraint + fixes
Qwen3.5-0.8B15.1%15.7%0.0%
Qwen3.5-2B24.1%25.2%0.0%
Gemma 4 E2B13.3%15.3%0.0%
LFM2.5-1.2B37.5%38.2%0.0%
LFM3-1.7B14.4%14.1%0.0%
Granite 4.2 3B5.7%6.0%0.0%
Qwen3.5-9B4.0%4.3%0.0%

Hallucinated spans go to exactly zero for every model, and so do answers containing one.

Accuracy when hallucinated spans don't count

Span F1 (× 100).

0204060Qwen3.5-0.8B6.414.0Qwen3.5-2B32.647.3Gemma 4 E2B48.554.1LFM2.5-1.2B6.68.9LFM3-1.7B14.315.3Granite 4.2 3B21.923.0Qwen3.5-9B45.046.7
Show table
JSON schemacopy constraint + fixes
Qwen3.5-0.8B6.414.0
Qwen3.5-2B32.647.3
Gemma 4 E2B48.554.1
LFM2.5-1.2B6.68.9
LFM3-1.7B14.315.3
Granite 4.2 3B21.923.0
Qwen3.5-9B45.046.7

The copy constraint beats JSON mode for all seven models.

The gains are largest for the models that hallucinate the most (Qwen3.5-0.8B 6.4 → 14.0, LFM2.5 6.6 → 8.9, Qwen3.5-2B 32.6 → 47.3), and small for the ones that already copy well (Qwen3.5-9B 45.0 → 46.7, Granite 21.9 → 23.0, LFM3 14.3 → 15.3). Gemma 4 is in between (48.5 → 54.1).

Even the best scores are only around 50 though, with or without the constraint. That’s because most of the remaining errors are not about copying at all, but about choosing what to copy. Some models quote a passage for almost every question, even though 57% of the prompts have no relevant clause, while Granite goes the other way and answers empty on most prompts that do have one.12

While the constraint cannot make the model choose the right passage, it guarantees that the predicted quote will always be real.

Footnotes

  1. 1.

    CUAD has 41 clause types. I kept the ones that occur in at least a quarter of our contracts and whose answers are usually a full clause rather than a few words (a median of at least 15 words): Anti-Assignment, Cap On Liability, Expiration Date, Governing Law, License Grant, Renewal Term, Revenue/Profit Sharing and Termination For Convenience. That drops the rare types, where almost every prompt has no relevant clause, and the short ones, like party names and dates, which appear many times in a contract and leave very little to copy. The rule only looks at the lawyers’ annotations, never at what the models wrote. ↩

  2. 2.

    The exact prompt, sent as a single user message. The clause type and its description come from CUAD’s own questions; this one is for “Governing Law”. The contract goes first, so every question about the same contract shares a cacheable prefix.

    <contract>
    …full contract text…
    </contract>
    Find every passage in the contract above that relates to "Governing Law". (Which state/country's law governs the interpretation of the contract?)
    Copy each passage exactly as it appears in the contract, character for character. Do not paraphrase, summarize, fix typos, or change whitespace or punctuation. Respond with JSON only: {"spans": ["<passage>", ...]}. If there are no relevant passages, respond with {"spans": []}.
    ↩
  3. 3.

    Having seen an unusual ␣[, Qwen3.5-2B’s top guess for the next token is a digit in 84 of 150 prompts, as if a list of numbers were coming. ↩

  4. 4.

    Not every empty answer is wrong: in the lawyers’ annotations, 57% of these prompts have no relevant clause. The effect is clearest for these two models because they usually answer. Qwen3.5-2B, Qwen3.5-9B and Granite already answer empty about half the time or more without any constraint (50%, 47% and 91%), and the token-ID constraint takes them to 97%, 94% and 99.8%. Granite is over-silent to begin with: it answers empty on 80% of the prompts where a clause does exist. ↩

  5. 5.

    This holds for most models. LFM3 is the exception, at 25% empty against 14% unconstrained. The two smaller Qwen models also stay a few points above their unconstrained rate (12% against 7%, and 58% against 50%). ↩

  6. 6.

    Why sort the suffixes? In sorted order, suffixes that start with the same text sit next to each other, the same way all the words starting with “con” sit together in a dictionary. That keeps the constraint simple:

    • State: whatever we’ve copied so far, say Lic, matches one block of adjacent rows, one row for every place where Lic appears in the document. The state is just where that block starts and ends, [lo, hi), plus how many bytes we’ve written.
    • Step: when the model adds a byte, say e, we keep the rows in the block whose next byte is e. Since the block is sorted, those rows are next to each other too, so two binary searches find the new [lo, hi).
    • Check: if no row continues with e, the new block is empty, and e isn’t allowed.
    ↩
  7. 7.

    I did write my own byte-level processor and checked it against llguidance step by step. With all five tokenizers in the boundary chart (Qwen3.5, Gemma 4, LFM2.5, LFM3 and Granite), the two produce identical masks at every step inside a value. ↩

  8. 8.

    The step-by-step probability measurement shows this from the inside, with a copy constraint that only allows compact JSON. Right after {, Granite put most of its probability on a newline in 89 of 150 prompts (Qwen3.5-0.8B in 103, Qwen3.5-2B in 24), and the compact grammar kept essentially none of it. Granite’s greedy decode then closed the list without an answer on 88% of the prompts that have a gold answer (unconstrained, it already does so on 76% of them). Gemma 4 wants to open every answer with a ``` fence, and LFM3 wants to open 149 of 150 with a { and a newline; the grammar forbids both at the very first token, and both models write compact JSON instead. Gemma 4 then answers as often as it does unconstrained. ↩

  9. 9.

    This is the one accommodation llguidance can’t express: substring_chunks only matches literal text, so allowing a run of whitespace where the document has a single space needs a custom logits processor. With llguidance you have to pick one per model. Collapsing to one space suits Gemma 4 and LFM2.5, the two that tidy up whitespace the most, and keeping the document’s whitespace as it is suits the other five. Allowing any run beats the better of the two for six models, and ties on Qwen3.5-9B. ↩

  10. 10.

    F1 uses CUAD’s matching rule: a predicted span finds a gold answer if at least half of their words overlap. That’s lenient about where a span starts and ends, but not about hallucinations: a span that isn’t in the contract (whitespace aside) never matches, and counts as a false positive. Copy-constrained spans are always in the contract, so this never removes any of theirs. ↩

  11. 11.

    Whitespace normalization lifts Gemma 4 from 42.9 to 54.2, the quote fix nearly doubles Qwen3.5-2B (24.7 → 45.3), and free layout takes Granite from 14.3 to 19.8. Gemma 4 gives back 4 points once the other two fixes are added, and Qwen3.5-2B gives back 1 when free layout is added. ↩

  12. 12.

    With the copy constraint, the three smallest models (Qwen3.5-0.8B, LFM2.5 and LFM3) get only 6–9% of their spans right: LFM2.5 quotes something for 99.6% of the prompts with no relevant clause, and Qwen3.5-0.8B for 91%. Granite is the opposite: 74% of what it returns is right, but it finds only 14% of the gold answers, and 95% of the ones it misses are on prompts where it answered with an empty list. Qwen3.5-9B finds the most, 65%. ↩