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
10 changes: 6 additions & 4 deletions haystack/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
_should_trigger_tool_invoker_breakpoint,
_validate_tool_breakpoint_is_valid,
)
from haystack.core.pipeline.utils import _deepcopy_with_exceptions
from haystack.core.serialization import component_to_dict, default_from_dict, default_to_dict
from haystack.dataclasses import (
AgentBreakpoint,
Expand Down Expand Up @@ -290,7 +289,8 @@ def __init__(
self._state_schema = state_schema or {}

# Initialize state schema
resolved_state_schema = _deepcopy_with_exceptions(self._state_schema)
# shallow copy is sufficient: we only add a top-level "messages" key, never mutate nested values
resolved_state_schema = dict(self._state_schema)
Comment thread
anakin87 marked this conversation as resolved.
if resolved_state_schema.get("messages") is None:
resolved_state_schema["messages"] = {"type": list[ChatMessage], "handler": merge_lists}
self.state_schema = resolved_state_schema
Expand Down Expand Up @@ -750,7 +750,8 @@ def run( # noqa: PLR0915
)

with self._create_agent_span() as span:
span.set_content_tag("haystack.agent.input", _deepcopy_with_exceptions(agent_inputs))
# agent_inputs is local and not used after this point, so we avoid deepcopying it
span.set_content_tag("haystack.agent.input", agent_inputs)
Comment thread
anakin87 marked this conversation as resolved.

while exe_context.counter < self.max_agent_steps:
# We skip the chat generator when restarting from a snapshot from a ToolBreakpoint
Expand Down Expand Up @@ -983,7 +984,8 @@ async def run_async( # noqa: PLR0915
)

with self._create_agent_span() as span:
span.set_content_tag("haystack.agent.input", _deepcopy_with_exceptions(agent_inputs))
# agent_inputs is local and not used after this point, so we avoid deepcopying it
span.set_content_tag("haystack.agent.input", agent_inputs)
Comment thread
anakin87 marked this conversation as resolved.

while exe_context.counter < self.max_agent_steps:
# We skip the chat generator when restarting from a snapshot from a ToolBreakpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
enhancements:
- |
Reduced unnecessary deepcopies in Agent for improved performance.
Replaced deepcopy of ``state_schema`` with a shallow dict copy since only top-level keys are modified,
and removed deepcopy of ``agent_inputs`` for span tags since the dict is freshly created and only used
for tracing.