Skip to content

Python: Fix tool execution bleed-over in aiohttp/Bot Framework scenarios - #2314

Merged
moonbox3 merged 3 commits into
microsoft:mainfrom
moonbox3:fixing-2152
Nov 20, 2025
Merged

Python: Fix tool execution bleed-over in aiohttp/Bot Framework scenarios#2314
moonbox3 merged 3 commits into
microsoft:mainfrom
moonbox3:fixing-2152

Conversation

@moonbox3

Copy link
Copy Markdown
Contributor

Motivation and Context

ChatAgent._prepare_thread_and_messages reused the same ChatOptions instance 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 ChatOptions before each run to isolate per-request mutations. This keeps the agent's configured tools intact across aiohttp request handlers.

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 self-assigned this Nov 19, 2025
@moonbox3 moonbox3 added the agents Usage: [Issues, PRs], Target: Single agent label Nov 19, 2025
Copilot AI review requested due to automatic review settings November 19, 2025 01:48
@markwallace-microsoft markwallace-microsoft added the python Usage: [Issues, PRs], Target: Python label Nov 19, 2025
@markwallace-microsoft

markwallace-microsoft commented Nov 19, 2025

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _agents.py2885182%329, 388–390, 436, 490, 508, 670, 849, 852–854, 977–980, 982, 985–987, 1072, 1113, 1115, 1124–1129, 1135, 1137, 1147–1148, 1155, 1157–1158, 1166–1170, 1178–1179, 1181, 1186, 1188, 1222, 1263–1264, 1266, 1279
   _types.py9459989%130–131, 149–150, 287, 289, 296, 315, 355, 401–402, 438, 588, 702–703, 705, 730, 737, 754–756, 829, 834–835, 837, 844–845, 847, 869, 876, 879–881, 886–887, 893–895, 1019, 1106–1109, 1117–1118, 1209, 1390, 1396, 1640–1642, 1648–1649, 1817, 1948, 1953, 1957, 1961, 2138, 2189–2193, 2203, 2208, 2667, 2753–2755, 2828, 2839–2840, 3014, 3018, 3030–3032, 3133–3135, 3137–3139, 3142, 3146, 3149, 3154, 3199–3200, 3207–3208, 3242–3244, 3259, 3275, 3303, 3310
TOTAL15040221885% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2183 127 💤 0 ❌ 0 🔥 52.674s ⏱️

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 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() to deepcopy() in _prepare_thread_and_messages to prevent shared references
  • Added unit test to verify ChatOptions isolation 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

Comment thread python/packages/core/tests/core/test_function_invocation_logic.py
Comment thread python/packages/core/tests/core/test_agents.py
@moonbox3
moonbox3 added this pull request to the merge queue Nov 20, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 20, 2025
@moonbox3
moonbox3 added this pull request to the merge queue Nov 20, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 20, 2025
@moonbox3
moonbox3 added this pull request to the merge queue Nov 20, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 20, 2025
@moonbox3
moonbox3 added this pull request to the merge queue Nov 20, 2025
Merged via the queue into microsoft:main with commit d714b91 Nov 20, 2025
23 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Usage: [Issues, PRs], Target: Single agent python Usage: [Issues, PRs], Target: Python

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: Tool Execution Failure in aiohttp Context

6 participants