From 33f85fdb8a873bf08bfe68dab268009d320a224c Mon Sep 17 00:00:00 2001 From: Eddy Zhang Date: Fri, 2 Oct 2026 11:50:20 +0800 Subject: [PATCH] fix: raise a clear error when JsonSchemaValidator gets no messages JsonSchemaValidator.run() read messages[-1] unconditionally, so an empty list leaked an IndexError. Raise a ValueError that names the problem instead, and document it under :raises:. Same approach as #12766, which was approved but closed without merging. Fixes #12765. --- haystack/components/validators/json_schema.py | 7 +++++-- ...n-schema-validator-empty-messages-e3fcbf73c284f0f4.yaml | 5 +++++ test/components/validators/test_json_schema.py | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/json-schema-validator-empty-messages-e3fcbf73c284f0f4.yaml diff --git a/haystack/components/validators/json_schema.py b/haystack/components/validators/json_schema.py index 2f168bf9365..dc2d4a7472b 100644 --- a/haystack/components/validators/json_schema.py +++ b/haystack/components/validators/json_schema.py @@ -129,9 +129,12 @@ def run( :return: A dictionary with the following keys: - "validated": A list of messages if the last message is valid. - "validation_error": A list of messages if the last message is invalid. - :raises ValueError: If the last message has no text content, or if no JSON schema is provided either in - the `run` method or in the component init. + :raises ValueError: If `messages` is empty, if the last message has no text content, or if no JSON schema is + provided either in the `run` method or in the component init. """ + if not messages: + raise ValueError("The provided list of messages is empty.") + last_message = messages[-1] if last_message.text is None: raise ValueError(f"The provided ChatMessage has no text. ChatMessage: {last_message}") diff --git a/releasenotes/notes/json-schema-validator-empty-messages-e3fcbf73c284f0f4.yaml b/releasenotes/notes/json-schema-validator-empty-messages-e3fcbf73c284f0f4.yaml new file mode 100644 index 00000000000..d65245bc04c --- /dev/null +++ b/releasenotes/notes/json-schema-validator-empty-messages-e3fcbf73c284f0f4.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + ``JsonSchemaValidator.run()`` now raises a ``ValueError`` saying the message list is empty when it receives no + messages, instead of an ``IndexError`` from reading the last message. diff --git a/test/components/validators/test_json_schema.py b/test/components/validators/test_json_schema.py index 49f8eea8397..099ea07b1e7 100644 --- a/test/components/validators/test_json_schema.py +++ b/test/components/validators/test_json_schema.py @@ -85,6 +85,12 @@ def test_validates_message_against_json_schema(self, json_schema_github_compare, assert len(result["validated"]) == 1 assert result["validated"][0] == message + def test_run_raises_clear_error_for_empty_messages(self, json_schema_github_compare): + validator = JsonSchemaValidator(json_schema=json_schema_github_compare) + + with pytest.raises(ValueError, match="The provided list of messages is empty"): + validator.run([]) + def test_accepts_empty_json_schema(self): validator = JsonSchemaValidator(json_schema={}) message = ChatMessage.from_assistant('{"anything": "is valid"}')