← Back to Pull requests

fix: avoid deepcopy of live State when injecting tool arguments #13980

Open
unknown opened this pull request with 245 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 28, 2026 09:56 UTC
Author

What

_inject_state_args in haystack/components/agents/tool_calling.py was reading from state via State.get(), which returns deepcopy(self._data.get(key, default)). Every tool invocation with inputs_from_state therefore paid an unconditional deepcopy of the referenced value, and a non-deepcopyable value (a lock, DB connection, open file, or generator) raised TypeError from argument injection rather than from the tool itself.

This PR reads through state.data.get(...) instead, matching the invariant documented in AGENTS.md:

Read live Haystack State resources via state.data.get(...) or state.data[...] — avoids deep-copy bugs.

Repro

import threading
from haystack.tools import Tool
from haystack.components.agents.state import State
from haystack.components.agents.tool_calling import _inject_state_args

lock = threading.Lock()

def tool_using_lock(resource) -> str:
    return "ok"

tool = Tool(
    name="lock_tool",
    description="receives a non-deepcopyable resource",
    parameters={"type": "object", "properties": {}},
    function=tool_using_lock,
    inputs_from_state={"shared": "resource"},
)
state = State(schema={"shared": {"type": object}}, data={"shared": lock})

_inject_state_args(tool=tool, llm_args={}, state=state)
# before: TypeError: cannot pickle '_thread.lock' object
# after:  args["resource"] is lock

Tests

Two regression tests added to TestCore in test/components/agents/test_tool_calling.py:

  • test_inject_state_args_does_not_deepcopy_non_picklable_state_values — asserts a threading.Lock survives injection by identity.

  • test_inject_state_args_injects_live_state_value_without_copy — asserts a shared list[Document] is passed by identity, not copied.
  • Notes

    Only the internal injector changes. State.get() still deepcopies in its public contract, which is the right default for user code reading state; only the internal argument-injection path reads live.

    Diff: 245 lines (+183 / -62) across 3 files Reviewer: 1AbdullahButt Review duration: 4040m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.39) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 245 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.329
    Real PR Conversation Timeline
    1 comments
    "Review active on #13980"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38.9% (MEDIUM) · Higher = larger blast radius
    38.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.7% · Safe to merge
    20.7%