Skip to content

perf: reduce unnecessary deepcopies in Agent#10704

Merged
anakin87 merged 4 commits intodeepset-ai:mainfrom
Br1an67:fix/issue-10680-reduce-agent-deepcopies
Mar 6, 2026
Merged

perf: reduce unnecessary deepcopies in Agent#10704
anakin87 merged 4 commits intodeepset-ai:mainfrom
Br1an67:fix/issue-10680-reduce-agent-deepcopies

Conversation

@Br1an67
Copy link
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Related Issues

Proposed Changes:

Reduced unnecessary deepcopy calls in the Agent component for improved performance:

  1. state_schema (init): Replaced _deepcopy_with_exceptions(self._state_schema) with a shallow dict() copy. The only modification to the resolved schema is adding/overwriting the top-level "messages" key — nested values are never mutated, so a shallow copy is sufficient to keep self._state_schema and self.state_schema separate.

  2. agent_inputs span tags (run/run_async): Removed _deepcopy_with_exceptions(agent_inputs) when setting span content tags. The agent_inputs dict is freshly created just before the span tag and is not referenced or mutated elsewhere — it exists solely for tracing purposes.

How did you test it?

  • Ran existing unit tests: pytest test/components/agents/test_agent.py — all 51 non-async tests pass.
  • The single pre-existing async test failure (test_run_async_falls_back_to_run_when_chat_generator_has_no_run_async) is unrelated (missing pytest-asyncio in local env) and also fails on the unmodified main branch.

Notes for the reviewer

  • The removed import _deepcopy_with_exceptions from haystack.core.pipeline.utils is no longer used in agent.py after these changes.
  • Both changes follow the suggestions in the issue.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

Replace deepcopy of state_schema with shallow dict copy since only
top-level keys are modified. Remove deepcopy of agent_inputs for span
tags since the dict is freshly created and only used for tracing.
@Br1an67 Br1an67 requested a review from a team as a code owner March 1, 2026 15:53
@Br1an67 Br1an67 requested review from julian-risch and removed request for a team March 1, 2026 15:53
@vercel
Copy link

vercel bot commented Mar 1, 2026

@Br1an67 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLAassistant commented Mar 1, 2026

CLA assistant check
All committers have signed the CLA.

@anakin87 anakin87 requested review from anakin87 and removed request for julian-risch March 2, 2026 08:32
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my investigation, these deepcopies can be safely removed.

Nice PR.

I only left some minor comments to be addressed.

@vercel
Copy link

vercel bot commented Mar 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Mar 6, 2026 6:30am

Request Review

Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I only added the explanatory comments.

@anakin87 anakin87 enabled auto-merge (squash) March 6, 2026 06:32
@anakin87 anakin87 merged commit e960b59 into deepset-ai:main Mar 6, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate reducing deepcopies in Agent

3 participants