diff --git a/haystack/components/agents/agent.py b/haystack/components/agents/agent.py index dbc2f2f9344..e99f249b814 100644 --- a/haystack/components/agents/agent.py +++ b/haystack/components/agents/agent.py @@ -520,6 +520,12 @@ def _initialize_fresh_execution( system_prompt = system_prompt or self.system_prompt messages = messages or [] + if not isinstance(messages, list): + raise TypeError( + f"Agent component expects list[ChatMessage] for 'messages', got {type(messages).__name__}. " + "Check that the 'messages' input is correctly wired in the pipeline." + ) + if user_prompt is not None: if self._user_chat_prompt_builder is None: raise ValueError( diff --git a/releasenotes/notes/agent-messages-type-validation-de6a494e1512c23d.yaml b/releasenotes/notes/agent-messages-type-validation-de6a494e1512c23d.yaml new file mode 100644 index 00000000000..731a7b25654 --- /dev/null +++ b/releasenotes/notes/agent-messages-type-validation-de6a494e1512c23d.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Agent component now raises a clear TypeError when the `messages` input is not a list + (e.g. a string wired from an upstream component), instead of failing with the opaque + "can only concatenate list (not 'str') to list" error. diff --git a/test/components/agents/test_agent.py b/test/components/agents/test_agent.py index 155f70e82be..086f3f19523 100644 --- a/test/components/agents/test_agent.py +++ b/test/components/agents/test_agent.py @@ -968,6 +968,12 @@ def test_run_no_messages(self, monkeypatch): result = agent.run([]) assert result["messages"] == [] + def test_run_messages_string_raises_type_error(self): + chat_generator = MockChatGeneratorWithoutRunAsync() + agent = Agent(chat_generator=chat_generator, tools=[], system_prompt="You are helpful.") + with pytest.raises(TypeError, match="Agent component expects list\\[ChatMessage\\] for 'messages', got str"): + agent.run("this is a string not a list") + def test_run_only_system_prompt(self, caplog): chat_generator = MockChatGeneratorWithoutRunAsync() agent = Agent(chat_generator=chat_generator, tools=[], system_prompt="This is a system prompt.")