Skip to content

llguidance whitespace_flexible=False can silently collapse constrained JSON arrays to empty (LocalHFBackend) #1510

Closed

Description

@planetf1

Summary

Found while investigating #1476. Under llguidance grammar-constrained decoding, LocalHFBackend's hardcoded whitespace_flexible: False setting causes structured output with array-typed fields to be silently truncated — smaller models return an empty array ({"result":[]}), larger models return a partial list with fewer items than requested. In both cases no exception is raised and the caller has no way to detect the failure.

Severity: high impact when it triggers (wrong answer, no error), scoped to LocalHFBackend doing JSON-schema-constrained generation. llguidance is only imported by mellea/backends/huggingface.py — no other backend is affected.

Status: investigation complete. Fix in PR #1513.


What it looks like

Given a Pydantic model with an array field:

from pydantic import BaseModel

class SectionTitles(BaseModel):
    sections: list[str]

A call like:

result = await session.aact(
    backend=LocalHFBackend("ibm-granite/granite-4.1-3b"),
    format=SectionTitles,
)

Returns:

SectionTitles(sections=[])   # ← wrong, no exception raised

instead of the expected:

SectionTitles(sections=["Introduction", "Background", "Methods", "Results", "Conclusion"])

The same silent collapse affects any Pydantic model where the array holds objects:

class Finding(BaseModel):
    id: str
    text: str

class Report(BaseModel):
    findings: list[Finding]

# Returns Report(findings=[]) instead of Report(findings=[Finding(...), Finding(...), ...])

Where it happens

mellea/backends/huggingface.py hardcodes defaults={"whitespace_flexible": False} at all three llguidance call sites (_generate_from_context_with_kv_cache, _generate_from_context_standard, _generate_from_raw). This forces fully compact JSON (no space after :/,) as the only grammar-valid output shape.


Root cause

The compact-JSON grammar (whitespace_flexible=False), combined with greedy decoding, puts the model in a state where right after opening an array [, the highest-probability grammar-compatible token is ] — immediately closing it. The grammar doesn't forbid this (an empty array is schema-valid), so llguidance accepts it silently.

The False was introduced in PR #288 as a mechanical outlines → llguidance port with no documented rationale. The llguidance library's own internal default — documented in _lib.pyi — is whitespace_flexible=True. It was never intentional.


Scope and affected configurations

The collapse affects every LocalHFBackend call with a format= argument containing an array-typed field. Real Mellea usage is particularly susceptible because Mellea's formatters never inject the JSON schema into the user message — format= applies grammar as a logits processor only. Repro scripts that embed the schema in the prompt accidentally mask the bug by shifting token probabilities.

Three masking factors — each independently sufficient to hide the collapse in a repro script:

  1. Schema injected into the user message (Mellea's real path never does this)
  2. Different Pydantic class names (llguidance embeds title fields from the schema into the grammar string)
  3. Minor prompt wording changes (shifts input_ids enough to alter greedy decode at the critical token position)

Empirical results

All runs: 10 trials, 5 schema variants, n=1–8 items. wf=True is the fix.

granite-4.1-3b — CPU, CUDA, MPS

Device wf=False OK wf=True OK
CPU 100/250 250/250
CUDA 100/250 250/250
MPS 100/250 250/250

Failure mode: EMPTY_ARRAY — {"result":[]}. Collapse onset at n=2–3 depending on schema. wf=True is clean on all three devices.

granite-4.1-8b — CPU, CUDA, MPS

Device wf=False OK wf=True OK
CPU 190/250 250/250
CUDA 210/250 250/250
MPS 190/250 250/250

Failure mode: WRONG_COUNT — array closes after 1 item instead of the expected count, e.g. {"result":[{"text":""}]} when 5 items were requested. Larger models are more resistant but still fail silently. wf=True is clean on all three devices.

granite-4.0-micro — CUDA

wf=False OK wf=True OK
CUDA 190/250 230/250

Failure mode: EMPTY_ARRAY on most schemas; WRONG_COUNT on mixed_types. The 20 wf=True failures are single_str_field n=2 producing 1 item instead of 2 on every trial — this appears to be a model-level instruction-following limitation on that schema independent of the grammar setting.


The fix

Change False → True at all three grammar_from_json_schema call sites in mellea/backends/huggingface.py. Each site now reads:

grammar = llguidance.LLMatcher.grammar_from_json_schema(
    schema, defaults={"whitespace_flexible": True}
)

wf=True produces spaced JSON ([ "a", "b" ] rather than ["a","b"]), using ~1.5× more tokens for the same content. Callers with a tight explicit ModelOption.MAX_NEW_TOKENS budget should size accordingly. The default case (no MAX_NEW_TOKENS, generation runs to EOS) is unaffected.


What is NOT affected

  • All other backends (OllamaBackend, OpenAIBackend, LiteLLMBackend, WatsonxBackend): use their own grammar/format paths and do not call llguidance.LLMatcher.grammar_from_json_schema.

Fix: PR #1513

Activity

  1. self-assigned this
    on Aug 6, 2026
  2. added
    bug
    p1
    area/backends
    on Aug 6, 2026
  3. added 4 commits that reference this issue on Aug 7, 2026
    b9115d8
    0e3d56a
    3cfe24b
    d5f6d9e
  4. added a commit that references this issue on Aug 13, 2026
    5128a28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/backendsbugp1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions