Skip to content

Python: fix thread serialization for multi-turn tool calls - #4684

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 5 commits into
microsoft:mainfrom
eavanvalkenburg:copilot/issue-4672-thread-serialization
Mar 17, 2026
Merged

Python: fix thread serialization for multi-turn tool calls#4684
Eduard van Valkenburg (eavanvalkenburg) merged 5 commits into
microsoft:mainfrom
eavanvalkenburg:copilot/issue-4672-thread-serialization

Conversation

@eavanvalkenburg

@eavanvalkenburg Eduard van Valkenburg (eavanvalkenburg) commented Mar 13, 2026

Copy link
Copy Markdown
Member

Motivation and Context

Fixes #4672.

Persisted Python sessions can retain OpenAI Responses fc_id values on stored function_call content. Those IDs are response-scoped and valid only for the original Responses tool loop. The bug is real, but the fix belongs in the Responses request-preparation path rather than in framework-wide session loading.

Description

  • move stale-fc_id replay handling out of _sessions.py and into OpenAIResponsesClient._prepare_messages_for_openai()
  • preserve stored fc_id values in session history, but ignore them when replaying provider-attributed history back through the Responses API
  • keep live same-run tool-loop fc_id behavior unchanged so active Responses tool calls still use their real item IDs
  • add/update regressions for in-memory history reloads, session serialization, cross-provider shared local storage, and duplicate call_id reuse between replayed history and live tool calls

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.

Copilot AI review requested due to automatic review settings March 13, 2026 11:07
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Mar 13, 2026

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

Fixes restored Python session replay for OpenAI Responses multi-turn tool calls by ensuring provider-ephemeral function-call IDs (fc_id) are not resent when history is reloaded into a new run context.

Changes:

  • Strip fc_id from function_call content when loading stored history into SessionContext (without mutating stored session state).
  • Add a regression test that covers in-memory history loading plus session serialization/deserialization round-trips for multi-turn tool calls.

Reviewed changes

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

File Description
python/packages/core/agent_framework/_sessions.py Sanitizes loaded history by removing provider-ephemeral fc_id before extending messages into the run context.
python/packages/core/tests/openai/test_openai_responses_client.py Adds an async regression test verifying fc_id is preserved live but not replayed from loaded/restored history.

You can also share your feedback on Copilot code review. Take the survey.

@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/openai
   _responses_client.py82112684%312–315, 319–320, 325–326, 336–337, 344, 359–365, 386, 394, 417, 514, 613, 672, 674, 676, 678, 746, 760, 840, 850, 855, 898, 977, 994, 1007, 1072, 1165, 1170, 1174–1176, 1180–1181, 1247, 1276, 1282, 1292, 1298, 1303, 1309, 1314–1315, 1376, 1398–1399, 1414–1415, 1433–1434, 1475–1478, 1640, 1695, 1697, 1777–1785, 1907, 1962, 1977, 1997–2007, 2020, 2031–2035, 2049, 2063–2074, 2083, 2115–2118, 2126–2127, 2129–2131, 2145–2147, 2157–2158, 2164, 2179
TOTAL24006264089% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5234 20 💤 0 ❌ 0 🔥 1m 23s ⏱️

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread python/packages/core/agent_framework/openai/_responses_client.py Outdated
Comment thread python/packages/core/agent_framework/openai/_responses_client.py Outdated
Merged via the queue into microsoft:main with commit cdb51e6 Mar 17, 2026
31 checks passed
@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the copilot/issue-4672-thread-serialization branch June 30, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Thread State Serialization Breaks Multi-Turn Tool Calls

6 participants