Python: drop hosted MCP calls when reasoning is stripped#6210
Open
he-yufeng wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds handling for a replay scenario where reasoning content is stripped (when not using service-side storage) by also dropping the paired hosted-MCP tool call/result to avoid emitting invalid item sequences to the OpenAI API.
Changes:
- Add a unit test that asserts hosted-MCP call/result are dropped when paired reasoning is stripped (no service-side storage).
- Update
_prepare_message_for_openaito detect “reasoning will be dropped” and skip serializingmcp_callaccordingly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/openai/tests/openai/test_openai_chat_client.py | Adds regression test for dropping hosted-MCP call/result when reasoning is stripped. |
| python/packages/openai/agent_framework_openai/_chat_client.py | Drops mcp_call when reasoning is stripped without storage to prevent invalid payloads. |
Comment on lines
+1495
to
+1497
| drops_reasoning_without_storage = not request_uses_service_side_storage and any( | ||
| content.type == "text_reasoning" for content in message.contents | ||
| ) |
Comment on lines
+1552
to
+1555
| # | ||
| # Without storage, a reasoning + hosted-MCP pair cannot be replayed | ||
| # partially: reasoning is stripped above, and a bare mcp_call is rejected. | ||
| if request_uses_service_side_storage or drops_reasoning_without_storage: |
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
Fixes #6074.
When OpenAI Responses history is replayed without service-side storage, the client strips text_reasoning because replaying standalone reasoning items is rejected by the API. Hosted MCP calls from the same model output were still kept, which can leave the request with a bare mcp_call after its paired reasoning item has been removed.
Description
This change treats that stateless replay path as an all-or-nothing pair for hosted MCP items. If a message contains reasoning that will be stripped, hosted MCP call/result contents in the same message are skipped too. Any later unmatched MCP result marker is then dropped by the existing coalescing logic.
The existing behavior for hosted MCP calls without reasoning is unchanged, and storage-backed continuation still strips server-issued MCP items as before.
A regression test covers a reasoning plus hosted MCP call/result history and verifies that the prepared input does not contain a bare reasoning, mcp_call, or function_call_output item.
Contribution Checklist