Skip to content

Investigate bounding llguidance whitespace_pattern for LocalHFBackend structured output #1515

Closed

Description

@planetf1

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: true is equivalent to whitespace_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:

  1. whitespace_flexible=True + whitespace_pattern=r"[\x20\x0A\x0D\x09]{1,8}"
  2. whitespace_pattern alone (no whitespace_flexible key at all)
  3. 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

Activity

  1. self-assigned this
    on Aug 10, 2026
  2. planetf1 commented on Aug 10, 2026

    @planetf1
    ContributorAuthor

    looking into this (lower priority) as a followup to 1513

  3. added
    area/backends
    p2
    on Aug 10, 2026
  4. removed their assignment
    on Sep 3, 2026
  5. planetf1 commented on Oct 5, 2026

    @planetf1
    ContributorAuthor

    Status (2026-10-05): the bound landed as an intermediate fix; the investigation items are still open.

    Done — verified against upstream/main:

    Not done (the three action items remain open):

    So 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.

  6. added
    bug
    on Oct 6, 2026
  7. self-assigned this
    on Nov 9, 2026
  8. added 2 commits that reference this issue on Nov 10, 2026
    e3a09d0
    c18f4ba
  9. markstur commented on Nov 10, 2026

    @markstur
    Contributor

    Root cause found (action item 1): the whitespace_pattern upper 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 failed

    Reproduced 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:

    • Character classes and lower bounds do work (a space-only pattern rejects \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.
    • In the v1.7.3 source, parser/src/json/compiler.rs registers the whitespace pattern via add_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) introduced SkipSpec::once and 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 hf extra to llguidance>=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.

  10. markstur commented on Nov 10, 2026

    @markstur
    Contributor

    @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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/backendsbugp2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions