Skip to content

Python: fix: keep reasoning with hosted MCP calls#6110

Closed
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/preserve-reasoning-mcp-pairs
Closed

Python: fix: keep reasoning with hosted MCP calls#6110
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/preserve-reasoning-mcp-pairs

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Fixes #6074

Summary

  • keep text_reasoning items when replaying a hosted MCP call without service-side storage
  • keep storage-backed replay stripping unchanged so stored hosted MCP items are not duplicated
  • add regression coverage for hosted MCP call and coalesced result serialization

Tests

  • python -m pytest python\packages\openai\tests\openai\test_openai_chat_client.py -q -p no:cacheprovider -k "hosted_mcp_call_storage_off or hosted_mcp_call_with_storage or coalesced_hosted_mcp_result or mcp_server_tool_call or coalesces_mcp_call"
  • python -m pytest python\packages\openai\tests\openai\test_openai_chat_client.py -q -p no:cacheprovider -k "reasoning or mcp_server_tool"
  • python -m ruff check python\packages\openai\agent_framework_openai\_chat_client.py python\packages\openai\tests\openai\test_openai_chat_client.py
  • python -m ruff format --check python\packages\openai\agent_framework_openai\_chat_client.py python\packages\openai\tests\openai\test_openai_chat_client.py
  • python -m py_compile python\packages\openai\agent_framework_openai\_chat_client.py python\packages\openai\tests\openai\test_openai_chat_client.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 27, 2026 12:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates OpenAI chat message preparation to optionally keep inline reasoning content when it accompanies a hosted MCP tool call (when service-side storage is disabled), and adds tests covering the new behavior.

Changes:

  • Adjust _prepare_message_for_openai to retain text_reasoning items only when paired with a hosted MCP call and request storage is off.
  • Add tests ensuring reasoning is preserved/dropped depending on request_uses_service_side_storage.
  • Add a test ensuring reasoning is preserved when an MCP call/result is coalesced.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/openai/agent_framework_openai/_chat_client.py Changes reasoning serialization rules based on presence of hosted MCP calls and storage flag.
python/packages/openai/tests/openai/test_openai_chat_client.py Adds regression tests for reasoning + hosted MCP call behavior (with/without storage and with coalesced results).

Comment on lines +1487 to 1503
# without its required following item"), so only keep reasoning inline when it is paired
# with a hosted MCP call in the same assistant message.
has_hosted_mcp_call = any(
item.type == "mcp_server_tool_call" and getattr(item, "call_id", None) for item in message.contents
)
for content in message.contents:
match content.type:
case "text_reasoning":
if not request_uses_service_side_storage and has_hosted_mcp_call:
prepared_reasoning = self._prepare_content_for_openai(
message.role,
content,
replays_local_storage=replays_local_storage,
)
if prepared_reasoning:
all_messages.append(prepared_reasoning)
continue

result = client._prepare_messages_for_openai(messages, request_uses_service_side_storage=False)

types = [item.get("type") for item in result if isinstance(item, dict)]
@github-actions github-actions Bot changed the title fix: keep reasoning with hosted MCP calls Python: fix: keep reasoning with hosted MCP calls May 27, 2026
@he-yufeng he-yufeng force-pushed the fix/preserve-reasoning-mcp-pairs branch from 4bb178b to 33377bc Compare May 27, 2026 18:12
@he-yufeng
Copy link
Copy Markdown
Contributor Author

Closing this in favor of #6139. The newer PR carries the current version of the same hosted MCP reasoning preservation fix, so keeping both open just splits review attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: ] Orphaned mcp_call items cause HTTP 400 in multi-turn reasoning model flows

3 participants