Repository navigation
Avoid mutable default argument in process_tool_calls - #6189
adtyavrdhn merged 2 commits into
Conversation
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.
There was a problem hiding this comment.
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 aNonedefault foroutput_final_result. - Instantiate a fresh
deque(maxlen=1)insideprocess_tool_callswhenoutput_final_resultis not provided.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 |
process_tool_calls
adtyavrdhn
left a comment
There was a problem hiding this comment.
Just remove the comment block please
|
Done — removed the comment block. Thanks for the review @adtyavrdhn. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
📝 WalkthroughWalkthroughThe Changes
Related PRs: None identified. Suggested labels: none Suggested reviewers: none Poem 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Problem
process_tool_callsin_tool_execution.pyuses a mutable default argument for itsoutput_final_resultoutput parameter:Default values are evaluated once, at function definition time, so every call that omits the argument (the
run_streamon_completepath inagent/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_callsin_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:
Fix
Default the parameter to
Noneand 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.