|
10 | 10 |
|
11 | 11 | import pytest |
12 | 12 |
|
13 | | -from haystack import Pipeline |
| 13 | +from haystack import Document, Pipeline |
14 | 14 | from haystack.components.agents.state import State |
15 | 15 | from haystack.components.builders.prompt_builder import PromptBuilder |
16 | 16 | from haystack.components.generators.chat.openai import OpenAIChatGenerator |
@@ -1062,6 +1062,30 @@ def test_merge_tool_outputs_with_output_mapping_2(self): |
1062 | 1062 | ) |
1063 | 1063 | assert state.data == {"all_weather_results": {"weather": "sunny", "temperature": 14, "unit": "celsius"}} |
1064 | 1064 |
|
| 1065 | + def test_merge_tool_outputs_source_key_absent_does_not_corrupt_list_state(self): |
| 1066 | + """ |
| 1067 | + Simulates a PipelineTool wrapping a pipeline with a conditional branch that may not execute, resulting in the |
| 1068 | + source key being absent from the tool result. The test verifies that in this case, the existing list in state |
| 1069 | + is not corrupted by appending None. |
| 1070 | + """ |
| 1071 | + tool = Tool( |
| 1072 | + name="retrieval", |
| 1073 | + description="mock", |
| 1074 | + parameters={"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}, |
| 1075 | + function=lambda query: {}, |
| 1076 | + outputs_to_state={"documents": {"source": "documents_output"}}, |
| 1077 | + ) |
| 1078 | + invoker = ToolInvoker(tools=[tool]) |
| 1079 | + existing_doc = Document(content="from first call") |
| 1080 | + state = State(schema={"documents": {"type": list[Document]}}) |
| 1081 | + state.set("documents", [existing_doc]) |
| 1082 | + |
| 1083 | + # Tool result where the source key is absent (document extraction branch did not execute) |
| 1084 | + invoker._merge_tool_outputs(tool=tool, result={"result": "no web results found"}, state=state) |
| 1085 | + |
| 1086 | + assert state.data["documents"] == [existing_doc] |
| 1087 | + assert None not in state.data["documents"] |
| 1088 | + |
1065 | 1089 | def test_merge_tool_outputs_with_output_mapping_and_handler(self): |
1066 | 1090 | handler = lambda _, new: f"{new}" # noqa: E731 |
1067 | 1091 | weather_tool = Tool( |
|
0 commit comments