Python: Fix tool execution bleed-over in aiohttp/Bot Framework scenarios#2314
Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom Nov 20, 2025
Merged
Python: Fix tool execution bleed-over in aiohttp/Bot Framework scenarios#2314moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3 merged 3 commits intomicrosoft:mainfrom
Conversation
Member
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a critical bug where ChatAgent reused the same ChatOptions instance across multiple runs, causing tool list mutations to persist across requests in aiohttp/Bot Framework scenarios. The fix changes from shallow copy() to deepcopy() to properly isolate per-request mutations.
- Changed
copy()todeepcopy()in_prepare_thread_and_messagesto prevent shared references - Added unit test to verify
ChatOptionsisolation after preparing threads - Added integration tests for aiohttp scenarios (single-threaded and multi-threaded)
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_agents.py | Changed from shallow copy() to deepcopy() for ChatOptions to prevent tool list bleed-over between requests |
| python/packages/core/tests/core/test_agents.py | Added unit test verifying that prepared ChatOptions are isolated from agent's base options |
| python/packages/core/tests/core/test_function_invocation_logic.py | Added integration tests for aiohttp scenarios (both single and multi-threaded) to validate the fix |
eavanvalkenburg
approved these changes
Nov 19, 2025
alliscode
approved these changes
Nov 19, 2025
dmytrostruk
approved these changes
Nov 19, 2025
eavanvalkenburg
approved these changes
Nov 20, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…ios (microsoft#2314) * Deep copy the agent chat options to avoid mutations * avoiding _thread.RLock pickling errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
ChatAgent._prepare_thread_and_messagesreused the sameChatOptionsinstance every run. aiohttp/Bot Framework apps mutate the options (for example by extending tools for approvals) so later requests end up with empty tool lists. When the underlying client asks us to execute a tool we've already stripped away, we return the raw function-call JSON instead of invoking the tool.The fix is to deep copy the agent's
ChatOptionsbefore each run to isolate per-request mutations. This keeps the agent's configured tools intact acrossaiohttprequest handlers.Description
Contribution Checklist