Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions haystack/components/validators/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions test/components/validators/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')
Expand Down
Loading