Skip to content
Closed
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
13 changes: 11 additions & 2 deletions haystack/components/routers/llm_messages_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
_RESERVED_OUTPUT_NAMES = ("chat_generator_text", "unmatched")


def _chat_generator_text(generator_result: dict[str, Any]) -> str:
"""Return the first reply text from a chat generator, or an empty string if none is available."""
replies = generator_result.get("replies") or []
if not replies:
return ""
text = getattr(replies[0], "text", None)
return text if isinstance(text, str) else ""


@component
class LLMMessagesRouter:
"""
Expand Down Expand Up @@ -158,7 +167,7 @@ def run(self, messages: list[ChatMessage]) -> dict[str, str | list[ChatMessage]]
with _trace_chat_generator_run(self._chat_generator, {"messages": messages_for_inference}) as span:
generator_result = self._chat_generator.run(messages=messages_for_inference)
span.set_content_tag("haystack.component.output", generator_result)
chat_generator_text = generator_result["replies"][0].text
chat_generator_text = _chat_generator_text(generator_result)

output = {"chat_generator_text": chat_generator_text}

Expand Down Expand Up @@ -207,7 +216,7 @@ async def run_async(self, messages: list[ChatMessage]) -> dict[str, str | list[C
with _trace_chat_generator_run(self._chat_generator, {"messages": messages_for_inference}) as span:
generator_result = await _execute_component_async(self._chat_generator, messages=messages_for_inference)
span.set_content_tag("haystack.component.output", generator_result)
chat_generator_text = generator_result["replies"][0].text
chat_generator_text = _chat_generator_text(generator_result)

output = {"chat_generator_text": chat_generator_text}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
``LLMMessagesRouter`` now routes messages to ``unmatched`` when the chat generator returns no replies
or a reply with no text, instead of raising ``IndexError`` or ``TypeError``.
24 changes: 24 additions & 0 deletions test/components/routers/test_llm_messages_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ def test_run_input_errors(self):
with pytest.raises(ValueError):
router.run([ChatMessage.from_system("You are a helpful assistant.")])

def test_run_empty_replies_routes_unmatched(self):
chat_generator = Mock(spec=["run"])
chat_generator.run.return_value = {"replies": []}
router = LLMMessagesRouter(
chat_generator=chat_generator, output_names=["safe", "unsafe"], output_patterns=["safe", "unsafe"]
)
messages = [ChatMessage.from_user("Hello")]
result = router.run(messages)
assert result["chat_generator_text"] == ""
assert result["unmatched"] == messages
assert "safe" not in result
assert "unsafe" not in result

def test_run_none_reply_text_routes_unmatched(self):
chat_generator = Mock(spec=["run"])
chat_generator.run.return_value = {"replies": [ChatMessage.from_assistant(text=None)]}
router = LLMMessagesRouter(
chat_generator=chat_generator, output_names=["safe", "unsafe"], output_patterns=["safe", "unsafe"]
)
messages = [ChatMessage.from_user("Hello")]
result = router.run(messages)
assert result["chat_generator_text"] == ""
assert result["unmatched"] == messages

def test_run_no_warm_up_with_unwarmable_chat_generator(self):
chat_generator = Mock(spec=["run"])
chat_generator.run.return_value = {"replies": [ChatMessage.from_assistant("safe")]}
Expand Down