haystack icon indicating copy to clipboard operation
haystack copied to clipboard

JsonSchemaValidator rejects valid arrays and skips constraints on empty arrays

Open Harsh23Kashyap opened this issue 2 months ago • 5 comments

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=validated

Additional 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

  • [x] Have you had a look at the FAQ page?

System:

  • OS: Linux
  • Python: 3.14.7
  • Haystack version: 3.3.0-rc0, current main at 2ae9acd

Harsh23Kashyap avatar Sep 30 '26 11:09 Harsh23Kashyap

JsonSchemaValidator splits a top-level JSON array and checks each element against the array schema. [1, 2] then fails {"type": "array", "items": {"type": "integer"}} ("1 is not of type array"), and [] skips minItems because the loop never runs.

I'll validate the payload once, as a whole document, and add tests for a valid integer array, an empty array that violates minItems, and a non-array value that should still fail.

HarshXAI avatar Oct 01 '26 07:10 HarshXAI

Taking this. Plan: add failing cases for valid arrays and for constraint skipping on empty arrays, then fix the array handling in JsonSchemaValidator. PR incoming.

Harbor404 avatar Oct 01 '26 17:10 Harbor404

hi @Harbor404 , I will plan accordingly , it would be great if you can assign me this issue.

HarshXAI avatar Oct 01 '26 18:10 HarshXAI

Hi @Harbor404 I'd like to pick this up. Root cause: in JsonSchemaValidator.run(), the for content in last_message_json: validate(...) loop is meant for the OpenAI function-calling case (a list of tool calls, each validated individually), but it runs unconditionally. So when the schema itself is an array schema, a valid array like [1, 2] gets each element validated against the array schema instead of the array as a whole — and an empty [] skips the loop entirely, so minItems constraints never fire. Proposed fix: only do the per-element loop when using_openai_schema is true; otherwise call validate(instance=last_message_json, schema=validation_schema) once on the whole payload. Will add regression tests per your note: a valid int array, an empty array rejected by minItems, and the existing function-call list behavior. Opening a draft PR shortly unless you'd rather discuss the approach first.

Faizan21-Analyst avatar Oct 01 '26 20:10 Faizan21-Analyst

Implemented in #13283. The existing tests also validate lists of function-call envelopes against ordinary object schemas, so the fix preserves that per-item behavior and applies whole-payload validation only to explicit array types (including nullable arrays).

Ten regression cases failed on baseline; the final validator suite passes 34 tests, including OpenAI calls with array arguments.

Generated and tested by OpenAI Codex on behalf of Himanshu Chawla.

himanshuchawla0807-gif avatar Oct 15 '26 21:10 himanshuchawla0807-gif