Skip to content

fix(grammar): enforce array length limits without items - #2463

Open
remote-controlled-man wants to merge 3 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-510163f23201
Open

remote-controlled-man wants to merge 3 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-510163f23201

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Array schemas that specify minItems or maxItems without items or prefixItems fall back to the unrestricted array primitive. For example, {"type":"array","minItems":1,"maxItems":1} incorrectly permits both [] and [1,2].

Route these schemas through the existing repetition builder, using the existing JSON value rule for unconstrained elements. Keep typed arrays and prefix tuples on their existing paths. Explicitly raise ValueError for unsatisfiable repetition bounds (maximum below minimum), including the shared typed-array and string-length paths, instead of producing a grammar that violates the maximum.

Add native grammar-sampler regressions for minimum/maximum/exact/zero lengths, nested arrays, all JSON element types (including UTF-8), invalid/incomplete documents, and typed-array/tuple controls. The same tests on the original implementation produce 11 failures and 35 passing controls; the fix passes all 46 grammar tests.

Validation on macOS arm64 / Python 3.12.14, bundled llama.cpp f5e6915c2:

  • Targeted grammar tests: 46 passed.
  • Real CPU completion with stories15M-q4_0.gguf and controlled logits: requested exact lengths 0, 1, 2 and 3 are respected; the original implementation returns an empty array for the length-1 case.
  • Full make test: 69 passed, no skips.
  • Full make lint: Ruff lint and formatting passed for all 29 files.
  • Hosted CI: pending. Other platforms were not tested locally.

This follows the JSON Schema array length contract. The explicit empty-items schema case is handled separately in #2378.

Prepared with AI assistance, including independent review.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 04:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants