Skip to content

fix(schema): respect maxItems and avoid crash on minItems>=3 in example generator#1553

Open
devteamaegis wants to merge 1 commit into
guardrails-ai:mainfrom
devteamaegis:fix/gen-array-maxitems
Open

fix(schema): respect maxItems and avoid crash on minItems>=3 in example generator#1553
devteamaegis wants to merge 1 commit into
guardrails-ai:mainfrom
devteamaegis:fix/gen-array-maxitems

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

gen_array in guardrails/schema/generator.py reads the array upper bound from maxItem, but the JSON Schema keyword is maxItems (also what the function's docstring claims to support). Two effects:

  1. A schema's maxItems is silently ignored — the generator always uses the default cap of 2.
  2. Any array schema with minItems >= 3 crashes with ValueError: empty range in randrange(...), because randint(min_items, 2) is called with min_items > 2.

generate_example is invoked during reask setup (guardrails/actions/reask.py), so a guard with a minItems >= 3 array field raises on reask.

generate_example({"type":"array","items":{"type":"string"},"minItems":5,"maxItems":10})
# ValueError: empty range in randrange(5, 3)

Why it happens

Misspelled schema key maxItem (should be maxItems) pins the upper bound to the default 2, so maxItems is never read and randint(min_items, 2) blows up whenever minItems > 2.

Fix

Read maxItems (correct key), default it to max(minItems, 2), and clamp max_items = max(min_items, max_items) so a user-supplied minItems > maxItems can't make randint raise.

Test

Added a parametrized test covering the crash case, the previously-ignored maxItems, and a tight maxItems:1 bound. Fails before the change (2 failures), passes after.

@vercel

vercel Bot commented Jul 2, 2026

Copy link
Copy Markdown

@devteamaegis is attempting to deploy a commit to the Guardrails AI Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant