Repository navigation
Description
Summary
Follow-up to #1510 / PR #1513, from Jake Lorocco's review comment: investigate bounding the amount of whitespace LocalHFBackend can generate under whitespace_flexible=True, and publish the raw empirical dataset behind PR #1513's fix.
Background
PR #1513 sets whitespace_flexible=True on all llguidance.LLMatcher.grammar_from_json_schema call sites to fix #1510 (silent array truncation). This permits unbounded whitespace between JSON tokens, which is safe from a correctness standpoint but costs ~1.5x more output tokens than compact JSON for the same content.
Jake raised a prior incident where json generation produced large amounts of whitespace and ran out of tokens, and suggested bounding it via llguidance's whitespace_pattern option instead of the unbounded whitespace_flexible=True:
whitespace_flexible: trueis equivalent towhitespace_pattern: r"[\x20\x0A\x0D\x09]+"And we could do:
r"[\x20\x0A\x0D\x09]{1,8}"
What needs investigating
I tested this suggestion directly against llguidance==1.7.3 (the version currently pinned in this repo) using a byte-level tokenizer/matcher and found the naive bound does not work: feeding 200 consecutive space tokens after an open JSON array ([) resulted in all 200 being accepted, across three configurations:
whitespace_flexible=True+whitespace_pattern=r"[\x20\x0A\x0D\x09]{1,8}"whitespace_patternalone (nowhitespace_flexiblekey at all)whitespace_flexible=False+whitespace_pattern=r"[\x20\x0A\x0D\x09]{1,8}"
In none of these did the {1,8} upper bound reject the 9th, 50th, or 200th consecutive space token. Either the bound is being compiled at a grammar scope this test didn't reach (e.g. it may only cap whitespace within a single separator, not prevent the model from repeatedly satisfying-then-reopening a whitespace-accepting state), or there's a version-specific gap between llguidance's documented behaviour and its compiled output. This needs someone with more llguidance internals context to dig into the actual compiled grammar, not just the observed acceptance mask.
Action items
- Determine why
whitespace_pattern's upper bound isn't enforced in the tested configuration (or whether a different llguidance API/version is needed) - If a working bound is found, benchmark it against the existing 250-trial correctness dataset from PR fix(hf): set whitespace_flexible=True in grammar_from_json_schema to prevent silent array collapse #1513 to confirm it doesn't reintroduce the llguidance whitespace_flexible=False can silently collapse constrained JSON arrays to empty (LocalHFBackend) #1510 collapse
- Publish the raw per-trial data behind PR fix(hf): set whitespace_flexible=True in grammar_from_json_schema to prevent silent array collapse #1513's empirical evidence table (currently only the aggregated pass/fail counts are in the PR description) — needed so this and future whitespace-tuning work can be verified against the same trials rather than re-run from scratch
Activity
planetf1 commentedon Aug 10, 2026
looking into this (lower priority) as a followup to 1513
planetf1 commentedon Oct 5, 2026
Status (2026-10-05): the bound landed as an intermediate fix; the investigation items are still open.
Done — verified against
upstream/main:_LLGUIDANCE_GRAMMAR_DEFAULTS(mellea/formatters/granite/base/util.py:41) setswhitespace_pattern: r"[\x20\x0A\x0D\x09]{0,20}", applied at all threegrammar_from_json_schemacall sites inLocalHFBackend. This hard-caps the runaway-whitespace case that motivated the issue while still allowing 4-space indentation to four levels (17 contiguous characters).5128a287a); the in-code comment records it as an intermediate fix addressing the PR fix(hf): set whitespace_flexible=True in grammar_from_json_schema to prevent silent array collapse #1513 review feedback.Not done (the three action items remain open):
whitespace_patternupper bound was not enforced in the tested llguidance 1.7.3 configurationsSo the risk is contained (hard cap in force) and the investigation itself is parked at lower priority, per the 2026-08-10 note here. Revisit if a model ever hits the 20-character ceiling, or if a future llguidance release changes enforcement behaviour.
fix(hf): enforce llguidance whitespace_pattern bound via >=1.8.0 floor
fix(hf): enforce llguidance whitespace_pattern bound via >=1.8.0 floor
markstur commentedon Nov 10, 2026
Root cause found (action item 1): the
whitespace_patternupper bound is not enforced on llguidance ≤ 1.7.6 because the JSON grammar's skip lexeme could re-match at a single position. Fixed upstream in llguidance #370, first released in v1.8.0.Why the
{1,8}experiments failedReproduced against the repo-pinned llguidance 1.7.3 with a byte-level tokenizer/LLMatcher (256 single-byte tokens): after consuming
[, all 200 consecutive space tokens stay in the acceptance bitmask — not just for the three configurations tested in the issue body, but also for the repo's live{0,20}default and even a tight{0,3}. The current_LLGUIDANCE_GRAMMAR_DEFAULTS"hard cap" is therefore not actually in force on 1.7.3 — the risk-containment note from October 5 doesn't hold for the pinned version.Two details pin down the mechanism:
\n; a{3,20}pattern blocks the next JSON token after 1 whitespace char), so the pattern is compiled — only the total upper bound leaks.parser/src/json/compiler.rsregisters the whitespace pattern viaadd_grammar(options, skip), which compiles the skip lexeme as re-matchable at a single grammar position. Once a{1,8}match completes, the skip lexeme re-opens and matches again: 8+8+8… unbounded.Upstream fix: microsoft/llguidance#370 "Add single-match skip lexemes" (commit
5d4ba26b, 2026-08-06) introducedSkipSpec::onceand switched the JSON compiler to it — first released in v1.8.0 (2026-08-10). The current llguidance docs ("matched at most once at each location… so upper bounds are enforced") describe post-#370 behavior, which is the version gap this issue hypothesized.Verified empirically on 1.9.1 with the same byte-level harness: the repo's
{0,20}default caps at exactly 20 consecutive whitespace tokens (space and newline runs, including multi-char whitespace tokens like" "/"\n\n"from real tokenizers); Jake's suggested{1,8}also enforces at exactly 8.Fix
PR #1807 bumps the
hfextra tollguidance>=1.8.0(locked to 1.9.1) and adds enforcement regression tests that run against real llguidance with a byte-level matcher — no model needed. The existing #1510 regression tests mock llguidance, so they only assert the pattern is passed; they can't catch an enforcement gap like this one.Action-item status
One behavioral note now that the ceiling is real: JSON wanting more than 4 levels of 4-space indentation (
\n+ 20+ contiguous whitespace chars) will be forced to a compact continuation. A grammar-compatible continuation always exists (the pattern allows zero whitespace), so the worst case is denser output, not an error or truncation.markstur commentedon Nov 10, 2026
@planetf1 I failed to notice that you might actually be "looking into this". If you are working on it you might want to close my PR. I ran with it a bit and the PR and above comment explain the problem, have the fix (bump version) and adds tests. It "Refs" this issue instead of "Closes" because the benchmark action items are not done (no dataset to use).
fix(hf): raise llguidance whitespace bound to 64, address review
fix(hf): enforce llguidance whitespace_pattern bound via >=1.8.0 floor (