JsonSchemaValidator rejects valid arrays and skips constraints on empty arrays
Describe the bug
JsonSchemaValidator splits every top-level JSON array and validates each element separately. With an array schema, this rejects valid arrays and skips constraints on empty arrays.
For example, [1, 2] fails {"type": "array", "items": {"type": "integer"}} because the validator checks 1 against the array schema. [] passes {"type": "array", "minItems": 1} because the validation loop runs zero times.
This can send a valid LLM response into a recovery loop or pass an invalid response to the next component.
Error message
For [1, 2], the recovery message includes 1 is not of type 'array'. There is no error for the invalid empty array.
Expected behavior
An array schema should validate the complete array, including items, minItems, maxItems and uniqueItems, rather than each element against that schema.
To Reproduce
import json
from jsonschema import ValidationError, validate
from haystack.components.validators import JsonSchemaValidator
from haystack.dataclasses import ChatMessage
cases = [
({"type": "array", "items": {"type": "integer"}}, "[1, 2]"),
({"type": "array", "minItems": 1}, "[]"),
]
for schema, text in cases:
try:
validate(json.loads(text), schema)
expected = "valid"
except ValidationError:
expected = "invalid"
result = JsonSchemaValidator(json_schema=schema).run(
messages=[ChatMessage.from_assistant(text)]
)
print(text, "jsonschema=" + expected, "Haystack=" + next(iter(result)))Output, reproduced twice on current main (2ae9acd):
[1, 2] jsonschema=valid Haystack=validation_error
[] jsonschema=invalid Haystack=validatedAdditional context
The unconditional list handling is in run() immediately before for content in last_message_json. There is also existing support for validating a list of function-call envelopes, so a fix needs to preserve that behavior rather than remove batch handling outright.
#12513 noted the array-schema failure as out of scope for the scalar fix. #12512 and #12522 concern the earlier scalar crash; this report is about validating the array itself and enforcing array constraints. The empty-schema fix in #12764 does not change this loop.
A narrow fix could distinguish array-instance validation from the existing function-call batch path. Regression tests should cover a valid integer array, an empty array rejected by minItems, and the existing function-call list behavior. No public API change is needed. I can open a draft PR if this approach fits.
FAQ Check
- Have you had a look at the FAQ page?
System:
- OS: Linux
- Python: 3.14.7
- Haystack version: 3.3.0-rc0, current
mainat2ae9acd
Source: deepset-ai/haystack