Repository navigation
fix(grammar): enforce array length limits without items - #2463
remote-controlled-man wants to merge 5 commits into
Conversation
1AbdullahButt
left a comment
There was a problem hiding this comment.
The fix is correct for small bounds, and it matches what upstream's common/json-schema-to-grammar.cpp does for an unconstrained array with bounds. But it turns maxItems above 256 from "accepted but unenforced" into "grammar fails to load". Here's what I ran: I built 764088d from source in Docker (Python 3.12, CPU). tests/test_llama_grammar.py passes (46 tests), and with the merge-base llama_grammar.py swapped in I get the 11 failures you describe.
-
Large
maxItemsnow breaks grammar loading._build_repetition(llama_grammar.py:254) expandsmaxItemsinto nested( ... )?groups, and the llama.cpp grammar parser caps nesting at 256. I bisected{"type": "array", "maxItems": N}throughllama_sampler_init_grammar: N up to 256 loads, but N=257 returns NULL withgrammar recursion depth limit exceeded (max 256). On main the same schema got the plainarrayprimitive and loaded fine. Typed arrays already had this problem (items: integer, maxItems: 300fails too, and that code path is unchanged by this PR), but untyped arrays now hit it as well, and something likemaxItems: 1000is a common pydanticmax_length.Upstream avoids this by emitting GBNF's bounded repetition instead of nesting (
build_repetitionin vendor/llama.cpp/common/json-schema-to-grammar.cpp:38). I checked that the grammar form works:"[" space ( value ("," space value){0,4999} )? "]" spaceloads fine in the same sampler. A port of the separator branch could look like this (the Python is untested):if separator_rule: if max_items == 0: return "" lo = max(min_items - 1, 0) hi = "" if max_items is None else str(max_items - 1) rule = f"{item_rule} ({separator_rule} {item_rule}){{{lo},{hi}}}" return f"({rule})?" if min_items == 0 else rule
If you'd rather keep this PR small, a test that
{"type": "array", "maxItems": 1000}still yields a loadable grammar would at least pin the current behaviour. -
A gap for a follow-up:
{"type": ["array", "null"], "maxItems": 1}is still unbounded, because the list-type branch (llama_grammar.py:714) drops the bounds. -
The new
ValueError(llama_grammar.py:257) looks good to me. The CHANGELOG could say that it also applies to typed arrays and string lengths, since those callers used to get a grammar and will now get an exception.
|
Thanks, @1AbdullahButt. The updated branch at |
Array schemas that specify
minItemsormaxItemswithoutitemsorprefixItemsfall back to the unrestricted array primitive. For example,{"type":"array","minItems":1,"maxItems":1}incorrectly permits both[]and[1,2].Use the existing JSON value rule for unconstrained elements. Bounded arrays without
itemsorprefixItemsbuild their length constraints from named repetition rules. This keeps the grammar shallow and enforces large bounds above the bundled parser's 2,000-repeat quantifier threshold. JSON number forms such as2.0retain the same bound as2. Typed arrays and prefix tuples retain their existing conversion paths. Contradictory repetition bounds raiseValueError, including on the typed-array and string-length paths.Validation (macOS arm64, Python 3.12.14, bundled llama.cpp
f5e6915c2):764088d, the native sampler failed to load the new array grammar formaxItemsof 257 and 1,000 because it exceeded the parser depth limit.make test: 83 passed, including 60 grammar tests.make lintandgit diff --checkpassed.stories15M-q4_0.gguf. The follow-up was tested locally on macOS only.The JSON Schema array length contract covers these bounds. Explicit empty
itemsis tracked in #2378. Array bounds inside a list-valuedtypeare a separate converter case.Prepared with AI assistance, including independent review.