|
12 | 12 | from haystack.core.pipeline import Pipeline |
13 | 13 | from haystack.core.pipeline.breakpoint import ( |
14 | 14 | HAYSTACK_PIPELINE_SNAPSHOT_SAVE_ENABLED, |
| 15 | + _create_agent_snapshot, |
15 | 16 | _create_pipeline_snapshot, |
16 | 17 | _is_snapshot_save_enabled, |
17 | 18 | _save_pipeline_snapshot, |
18 | 19 | _transform_json_structure, |
19 | 20 | load_pipeline_snapshot, |
20 | 21 | ) |
21 | 22 | from haystack.dataclasses import ChatMessage |
22 | | -from haystack.dataclasses.breakpoints import Breakpoint, PipelineSnapshot, PipelineState |
| 23 | +from haystack.dataclasses.breakpoints import AgentBreakpoint, Breakpoint, PipelineSnapshot, PipelineState |
23 | 24 |
|
24 | 25 |
|
25 | 26 | def test_transform_json_structure_unwraps_sender_value(): |
@@ -239,6 +240,74 @@ def to_dict(self): |
239 | 240 | assert any("Failed to serialize original input data for `pipeline.run`." in msg for msg in caplog.messages) |
240 | 241 |
|
241 | 242 |
|
| 243 | +class TestCreateAgentSnapshot: |
| 244 | + def test_create_agent_snapshot_non_serializable_chat_generator(self, caplog): |
| 245 | + class NonSerializable: |
| 246 | + def to_dict(self): |
| 247 | + raise TypeError("Cannot serialize") |
| 248 | + |
| 249 | + agent_breakpoint = AgentBreakpoint( |
| 250 | + agent_name="agent", break_point=Breakpoint(component_name="chat_generator", visit_count=1) |
| 251 | + ) |
| 252 | + |
| 253 | + with caplog.at_level(logging.WARNING): |
| 254 | + snapshot = _create_agent_snapshot( |
| 255 | + component_visits={"chat_generator": 1, "tool_invoker": 0}, |
| 256 | + agent_breakpoint=agent_breakpoint, |
| 257 | + component_inputs={"chat_generator": {"messages": NonSerializable()}, "tool_invoker": {"messages": []}}, |
| 258 | + ) |
| 259 | + |
| 260 | + assert snapshot.component_inputs["chat_generator"] == {} |
| 261 | + assert snapshot.component_inputs["tool_invoker"] != {} |
| 262 | + assert "Failed to serialize the agent's chat_generator inputs" in caplog.text |
| 263 | + |
| 264 | + def test_create_agent_snapshot_non_serializable_tool_invoker(self, caplog): |
| 265 | + class NonSerializable: |
| 266 | + def to_dict(self): |
| 267 | + raise TypeError("Cannot serialize") |
| 268 | + |
| 269 | + agent_breakpoint = AgentBreakpoint( |
| 270 | + agent_name="agent", break_point=Breakpoint(component_name="chat_generator", visit_count=1) |
| 271 | + ) |
| 272 | + |
| 273 | + with caplog.at_level(logging.WARNING): |
| 274 | + snapshot = _create_agent_snapshot( |
| 275 | + component_visits={"chat_generator": 1, "tool_invoker": 0}, |
| 276 | + agent_breakpoint=agent_breakpoint, |
| 277 | + component_inputs={"chat_generator": {"messages": []}, "tool_invoker": {"messages": NonSerializable()}}, |
| 278 | + ) |
| 279 | + |
| 280 | + assert snapshot.component_inputs["tool_invoker"] == {} |
| 281 | + assert snapshot.component_inputs["chat_generator"] != {} |
| 282 | + assert "Failed to serialize the agent's tool_invoker inputs" in caplog.text |
| 283 | + |
| 284 | + def test_create_agent_snapshot_both_non_serializable(self, caplog): |
| 285 | + class NonSerializable: |
| 286 | + def to_dict(self): |
| 287 | + raise TypeError("Cannot serialize") |
| 288 | + |
| 289 | + agent_breakpoint = AgentBreakpoint( |
| 290 | + agent_name="agent", break_point=Breakpoint(component_name="chat_generator", visit_count=1) |
| 291 | + ) |
| 292 | + |
| 293 | + with caplog.at_level(logging.WARNING): |
| 294 | + snapshot = _create_agent_snapshot( |
| 295 | + component_visits={"chat_generator": 1, "tool_invoker": 0}, |
| 296 | + agent_breakpoint=agent_breakpoint, |
| 297 | + component_inputs={ |
| 298 | + "chat_generator": {"messages": NonSerializable()}, |
| 299 | + "tool_invoker": {"messages": NonSerializable()}, |
| 300 | + }, |
| 301 | + ) |
| 302 | + |
| 303 | + assert snapshot.component_inputs["chat_generator"] == {} |
| 304 | + assert snapshot.component_inputs["tool_invoker"] == {} |
| 305 | + assert "Failed to serialize the agent's chat_generator inputs" in caplog.text |
| 306 | + assert "Failed to serialize the agent's tool_invoker inputs" in caplog.text |
| 307 | + assert snapshot.component_visits == {"chat_generator": 1, "tool_invoker": 0} |
| 308 | + assert snapshot.break_point == agent_breakpoint |
| 309 | + |
| 310 | + |
242 | 311 | def test_save_pipeline_snapshot_raises_on_failure(tmp_path, caplog, monkeypatch): |
243 | 312 | monkeypatch.setenv(HAYSTACK_PIPELINE_SNAPSHOT_SAVE_ENABLED, "true") |
244 | 313 |
|
|
0 commit comments