Skip to content

Python: Fix Anthropic server-tool continuation and replay - #9718

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 9 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-anthropic-history
Oct 28, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 9 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-anthropic-history

Conversation

@eavanvalkenburg

@eavanvalkenburg Eduard van Valkenburg (eavanvalkenburg) commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Motivation & Context

Anthropic server-side tool calls are parsed into generic function-call content, but the outgoing adapter previously serialized every generic call as a client tool_use. Completed web-search and web-fetch results were likewise reduced to string function results, and provider-specific raw blocks do not survive persisted history. Mixed local/server tool turns could therefore fail validation immediately, while completed server-tool transcripts could not be replayed faithfully from file history.

The adapter also mapped Anthropic pause_turn to normal completion. Agents returned unfinished server-side work after one request instead of replaying the paused assistant content with the same tools.

This change preserves the minimum plain Anthropic metadata required to reconstruct native server-tool calls and results, and continues bounded paused server turns in both streaming and non-streaming modes.

Description & Review Guide

  • What are the major changes? The Anthropic parser now records replay-critical server-tool block data in Content.additional_properties["anthropic"]["server_tool_block"]. The serializer reconstructs server_tool_use, web_search_tool_result, and web_fetch_tool_result blocks with their original assistant role, structured content, encrypted payload fields, and optional caller metadata. Streamed server-tool input deltas remain hosted and informational while aggregating into the persisted call. Legacy srvtoolu_ calls are recovered from their reserved ID prefix, while legacy results whose structured data was already lost fail before an invalid request is sent. pause_turn now replays the returned assistant content with unchanged tools for up to 10 continuation requests; bounded exhaustion preserves finish_reason="pause_turn". The regression suite also incorporates Python: Keep pending Anthropic server tool calls as server_tool_use #9513's applicable unique scenarios: in-memory and reloaded pending calls, later occurrences reusing a call ID, MCP/code-execution delta guards, and the full mixed streaming continuation path. The package documentation and function-loop scenario matrix describe the resulting behavior.
  • What is the impact of these changes? Pending Anthropic server tools remain server-owned when local tool results are returned, completed server-tool transcripts survive JSON or MessagePack history round-trips, and paused hosted-tool work continues instead of being reported as successfully complete. Ordinary local function calls and results retain their existing request shape.
  • What do you want reviewers to focus on? Please review the provider-owned metadata shape, the explicit fail-closed behavior for unrecoverable legacy server-tool results, the bounded pause-turn continuation semantics, and the end-to-end replay coverage.

Related Issue

Fixes #9478
Fixes #9479

#9513 is closed and behaviorally folded into this PR. Its pending server_tool_use fix, transcript-reload coverage, reused-call-ID regression, streamed-input regression, and MCP/code-execution delta guards are included here, while this PR additionally preserves completed native web-search/web-fetch results, encrypted and caller metadata, explicit legacy failure behavior, file-history replay, and Anthropic pause_turn continuation. Shivam Sharma is credited as a co-author on the fold-in commit.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: investigation, implementation, tests, documentation, and this PR description.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 05ba43d363f5
Model: gpt-5.6-sol

Overview

The PR introduces an adapter-owned metadata envelope that survives framework serialization, validates replay types, preserves native server-result roles, and fails closed for unrecoverable legacy results. The end-to-end tests provide strong coverage for non-streaming and persisted web-search history. Two residual gaps remain: streaming drops server-tool arguments before native replay, and the new serializer mistakes any provider-neutral hosted call for an Anthropic server tool.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
2 verified findings remained after source verification (1 high, 1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/anthropic/agent_framework_anthropic/_chat_client.py

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated

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.

🟡 Changes recommended

Streaming still discards server-tool argument deltas, leaving replayed calls without their original queries or URLs.

1 open issue
What changed in this PR

This PR addresses #9478 by preserving Anthropic server-tool identity and structured results in the Python adapter, extending the pending-call approach in #9513 to persisted history replay.

Changes:

  • Preserve provider metadata and reconstruct native server-tool calls and results.
  • Reject legacy server-tool results whose replay metadata is missing.
  • Document adapter-owned metadata and add continuation, persistence, and integration coverage.
File Description
python/​packages/​anthropic/​tests/​test_anthropic_client.py Adds mixed-tool continuation and persisted-history replay coverage.
python/​packages/​anthropic/​README.md Documents adapter-owned server-tool replay metadata.
python/​packages/​anthropic/​agent_framework_anthropic/​_chat_client.py Preserves native server-tool metadata, serialization, and assistant roles.

Review effort: Balanced

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py
@eavanvalkenburg Eduard van Valkenburg (eavanvalkenburg) changed the title Python: Preserve Anthropic server-tool history replay Python: Fix Anthropic server-tool continuation and replay Oct 27, 2026
Co-authored-by: Shivam Sharma <192242476+ShivamSharma43@users.noreply.github.com>

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.

🟡 Changes recommended

Streamed continuations lose message boundaries, which can corrupt final structured output and merge distinct tool-call occurrences.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated

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.

🔵 Needs a closer look

Automatic continuation can still omit redacted thinking or replay streamed MCP calls with incomplete arguments.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Prevent streaming MCP replay with discarded tool arguments

python/​packages/​anthropic/​agent_framework_anthropic/​_chat_client.py:114

MCP calls are not faithfully replayable in streaming mode yet: their opening mcp_tool_use can have input={}, and the parser deliberately discards subsequent input_json_delta events (also asserted by the new MCP delta regression). Whitelisting the resulting mcp_server_tool_call allows a paused turn to continue with empty arguments instead of the original input. Exclude MCP calls from automatic continuation until their streamed arguments are retained, and cover this paused-stream case.

Medium severity Prevent replay when redacted thinking blocks are discarded

python/​packages/​anthropic/​agent_framework_anthropic/​_chat_client.py:794

The replayability check only sees parsed Content, but the parser silently drops native redacted_thinking blocks. A paused response containing redacted thinking plus supported content therefore passes this guard in both response modes and is resent without its required reasoning block. Track discarded native blocks and return pause_turn without continuing, or preserve them faithfully; add redacted-thinking regressions for both modes.

1 resolved since last review

Review effort: Balanced

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated

@jpalvarezl Jose Alvarez (jpalvarezl) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Following up inline on the two replay gaps from the latest Copilot summary so their resolution is easier to track.

Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 27, 2026
Merged via the queue into microsoft:main with commit 4a272d2 Oct 28, 2026
50 checks passed
@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the investigate-anthropic-history branch October 28, 2026 06:35

This branch was previously deployed

1 inactive deployment
github-app-auth — 083de13d Deployed Oct 27, 2026 by eavanvalkenburg via add_label #26311
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

3 participants