Skip to content

Avoid mutable default argument in process_tool_calls - #6189

Merged
adtyavrdhn merged 2 commits into
pydantic:mainfrom
kratos0718:fix/tool-execution-shared-mutable-default-deque
Jul 2, 2026
Merged

adtyavrdhn merged 2 commits into
pydantic:mainfrom
kratos0718:fix/tool-execution-shared-mutable-default-deque

Conversation

@kratos0718

@kratos0718 kratos0718 commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

process_tool_calls in _tool_execution.py uses a mutable default argument for its output_final_result output parameter:

output_final_result: deque[result.FinalResult[NodeRunEndT]] = deque(maxlen=1),

Default values are evaluated once, at function definition time, so every call that omits the argument (the run_stream on_complete path in agent/abstract.py) shares a single module-level deque.

This is not a behavioral bug today: the function only ever appends to output_final_result, and the only caller that reads the result back (CallToolsNode._handle_tool_calls in _agent_graph.py) always passes its own fresh deque. Nothing ever reads the shared default, so no run's result can leak into another and no released version is affected.

It is still worth removing:

  • The shared deque keeps the most recent streamed run's final result (and its output object) alive at module level.
  • It is a latent trap: if a future edit made the function read the parameter (for example, an early exit when it is non-empty), state would leak between streamed runs in long-lived processes, silently skipping tool calls and leaving dangling tool calls in message history, with the failure only surfacing a run later.

Fix

Default the parameter to None and create a fresh deque inside the function, so each call that omits the argument gets isolated state. Callers that pass their own deque are unaffected. No user-visible behavior changes, so no test is added.

output_final_result defaulted to a module-level deque(maxlen=1). It is an output
argument that gets appended to, and one caller relies on the default rather than
passing its own deque, so that single deque is shared across every run through
that path and retains the previous run's final result (a B006 mutable-default
footgun). Default to None and create a fresh deque inside the function.
@github-actions github-actions Bot added size: S Small PR (≤100 weighted lines) bug Report that something isn't working, or PR implementing a fix labels Jul 1, 2026
@hramezani
hramezani requested a review from Copilot July 2, 2026 07:56

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.

Pull request overview

This PR fixes a mutable-default-argument bug in pydantic_ai_slim/pydantic_ai/_tool_execution.py where process_tool_calls(...) previously defaulted output_final_result to a shared deque(maxlen=1), causing state retention across calls that omitted the argument.

Changes:

  • Replace the shared mutable default deque(maxlen=1) with a None default for output_final_result.
  • Instantiate a fresh deque(maxlen=1) inside process_tool_calls when output_final_result is not provided.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +144 to +148
if output_final_result is None:
# `output_final_result` is an output argument that is appended to, and at
# least one caller relies on this default rather than passing its own. A
# shared module-level `deque(maxlen=1)` would be reused across every such
# run, retaining the previous run's final result (a mutable-default / B006
@adtyavrdhn adtyavrdhn changed the title Fix shared mutable default deque in process_tool_calls Avoid mutable default argument in process_tool_calls Jul 2, 2026

@adtyavrdhn adtyavrdhn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just remove the comment block please

@kratos0718

Copy link
Copy Markdown
Contributor Author

Done — removed the comment block. Thanks for the review @adtyavrdhn.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c7ab662b-780e-4954-baa0-3ec003d82823

📥 Commits

Reviewing files that changed from the base of the PR and between 49b9cf9 and e597384.

📒 Files selected for processing (1)
  • pydantic_ai_slim/pydantic_ai/_tool_execution.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/logfire (auto-detected)
  • pydantic/pydantic (auto-detected)

📝 Walkthrough

Walkthrough

The process_tool_calls function in _tool_execution.py now defaults the output_final_result parameter to None instead of eagerly creating a deque(maxlen=1). A lazy initialization step was added to set output_final_result to deque(maxlen=1) when it is None, before end-strategy and processor execution proceed.

Changes

Cohort / File Summary
_tool_execution.py Changed output_final_result parameter default from deque(maxlen=1) to None; added lazy initialization before use.

Related PRs: None identified.

Suggested labels: none

Suggested reviewers: none

Poem
A deque once eager, now waits its turn,
None until needed — a lesson to learn.
When calls come rolling, and results appear,
It springs into being, right on cue, right here. 🐇

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: removing the mutable default from process_tool_calls.
Description check ✅ Passed The description clearly explains the problem, fix, and impact, and is sufficiently complete for this small change.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@adtyavrdhn
adtyavrdhn merged commit e1691ea into pydantic:main Jul 2, 2026
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants