Skip to content

fix: preserve tool deserialization input - #13519

Closed
simpleqt wants to merge 1 commit into
deepset-ai:mainfrom
simpleqt:fix/tool-from-dict-preserves-input
Closed

simpleqt wants to merge 1 commit into
deepset-ai:mainfrom
simpleqt:fix/tool-from-dict-preserves-input

Conversation

@simpleqt

@simpleqt simpleqt commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Tool.from_dict(), ComponentTool.from_dict(), PipelineTool.from_dict(), and AgentTool.from_dict() deserialized in place, writing the result back into the dictionary supplied by the caller:

  • Tool.from_dict() replaced data["data"]["function"] / ["async_function"] with the resolved callables and rewrote data["data"]["outputs_to_state"] / ["outputs_to_string"] handlers with live callables.
  • ComponentTool.from_dict() / AgentTool.from_dict() replaced data["data"]["component"] / ["agent"] with the live component instance, and PipelineTool.from_dict() popped is_pipeline_async and rewrote the outputs_to_* configs inside the caller's dict.

Consequences of one from_dict() call on a user-held dictionary:

  • the dictionary no longer holds serialized data, so it fails json.dumps/yaml.dump and no longer compares equal to tool.to_dict();
  • calling from_dict() on the same dictionary a second time crashes with AttributeError: 'function' object has no attribute 'split' inside deserialize_callable() — this also hits Toolset.from_dict() and deserialize_tools_or_toolset_inplace(), which pass their nested tool dictionaries straight to Tool.from_dict().

The fix deserializes a copy instead (shallow copy of the tool parameters; deepcopy of the nested serialized component/agent, because the nested component's own from_dict() also rewrites its init_parameters in place), so the caller's dictionary keeps the serialized configuration and can be loaded more than once. This mirrors what BranchJoiner.from_dict() ("Copy so the caller's data keeps the serialized type and can be deserialized again") and the merged MockChatGenerator.from_dict() fix (#13471) already do.

The parameters=inner_data.get("parameters", None) → get("parameters") adjustments in ComponentTool.from_dict are ruff --fix (SIM910) output: the copied dict is now statically a dict, which newly triggers the rule on those lines.

How did you test it?

  • New regression tests (one per class) assert that after from_dict(data) the input still equals its deep-copied snapshot and that a second from_dict(data) succeeds:
    • test/tools/test_tool.py::TestToolAsync::test_from_dict_preserves_input
    • test/tools/test_component_tool.py::TestComponentToolInAgent::test_from_dict_preserves_input
    • test/tools/test_pipeline_tool.py::TestPipelineTool::test_from_dict_preserves_input
    • test/tools/test_agent_tool.py::TestAgentTool::test_from_dict_preserves_input
  • Red/green evidence: with the tests added and the source fix reverted, all 4 fail (4 failed in 1.68s, failing at assert data == snapshot); with the fix, all 4 pass (4 passed in 1.05s).
  • pytest test/tools/ test/components/agents/: 661 passed, 18 skipped (pre-existing optional-dependency skips).
  • Wider local run (test/core test/components test/utils test/tools, minus files needing heavy optional deps not installed locally): 5405 passed / 230 failed / 150 skipped with the fix vs 5400 passed / 231 failed on clean main. The failure sets are identical except one flaky async cancellation test (test_async_pipeline_stream.py::test_consumer_cancellation_cancels_pipeline) that failed only in the main run; all failures come from missing optional deps (torch, tiktoken, pillow, pypdf, jq, ...) and network-dependent fetcher tests on this machine. Zero tests newly fail with this change.
  • ruff check and ruff format --check (pinned v0.16.0) pass on all changed files.

Notes for the reviewer

  • Pipeline.from_dict() was already safe (it deep-copies the payload in _from_dict_impl); this PR fixes the direct Tool/Toolset/component-tool paths, which had no such protection.
  • Nested tool dictionaries are preserved; a nested component's own init_parameters can still be rewritten by that component's own from_dict() (a repo-wide pattern). This PR scopes the copy to what the tool classes themselves deserialize, so components with in-place from_dict() implementations (e.g. OpenAIChatGenerator) can be migrated opportunistically later, as was done for MockChatGenerator and BranchJoiner.
  • No behavior change for freshly unmarshalled (single-use) dictionaries; only reuse/preservation semantics change.

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.

This PR was fully generated with an AI assistant. I have reviewed the changes and run the relevant tests.


AI-assistance disclosure: this change and this description were prepared by an AI coding agent (DeepSeek Harness, model dsv41) working under the direction of the submitter, who reviewed the diff and the evidence before opening the pull request and takes responsibility for it.

Tool.from_dict(), ComponentTool.from_dict(), PipelineTool.from_dict()
and AgentTool.from_dict() rewrote the caller's dictionary in place,
replacing serialized callables and the serialized component/agent/pipeline
with live objects. After one deserialization the original configuration no
longer held serialized data, and loading the same dictionary a second time
crashed with AttributeError inside deserialize_callable().

Deserialize a copy instead, mirroring what BranchJoiner.from_dict() and
MockChatGenerator.from_dict() already do, so the same dictionary can be
loaded more than once and stays serializable.

Signed-off-by: simpleqt <2015900818@qq.com>
Copilot AI balanced review requested due to automatic review settings November 1, 2026 18:10
@simpleqt
simpleqt requested a review from a team as a code owner November 1, 2026 18:10
@simpleqt
simpleqt requested review from julian-risch and removed request for a team November 1, 2026 18:10
@vercel

vercel Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

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

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core/pipeline
  pipeline.py
  haystack/tools
  agent_tool.py
  component_tool.py
  pipeline_tool.py
  tool.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl

sjrl commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR! In general from_dict modifying the input dictionary is expected behavior, and our recommendation is to deepcopy the dictionary yourself before passing it in if you want to reuse it. Since this change touches all four tool classes and adds deep copies of the nested components, I don't think it's worth the extra complexity, so I'll close this one.

@sjrl sjrl closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants