Skip to content

Python: Preserve Anthropic server-tool history replay - #9718

Open
Eduard van Valkenburg (eavanvalkenburg) wants to merge 2 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-anthropic-history
Open

Eduard van Valkenburg (eavanvalkenburg) wants to merge 2 commits into
microsoft:mainfrom
eavanvalkenburg:investigate-anthropic-history

Conversation

@eavanvalkenburg

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.

This change preserves the minimum plain Anthropic metadata required to reconstruct native server-tool calls and results across live continuation, streaming finalization, and persisted history reloads.

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. 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. The package documentation describes the adapter-owned metadata, and tests cover mixed local/server tools, pause-turn replay, serialized history, fresh-agent replay, and a live Anthropic integration path.
  • What is the impact of these changes? Pending Anthropic server tools remain server-owned when local tool results are returned, and completed server-tool transcripts now survive JSON or MessagePack history round-trips without being converted into client tool traffic. 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, and the end-to-end history replay coverage.

Related Issue

Fixes #9478

#9513 addresses the pending mixed-tool call by preserving unresolved server_tool_use blocks, but intentionally leaves completed server-tool calls/results in the existing generic tool_use/tool_result representation because the structured result was not retained. This PR additionally preserves and replays completed native web-search/web-fetch result blocks through persisted history, handles encrypted and caller metadata, fails explicitly for unrecoverable legacy results, and adds full fresh-agent history replay coverage.

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

This branch was previously deployed

1 inactive deployment
github-app-auth — 4dec8f5f Deployed Oct 26, 2026 by eavanvalkenburg via add_label #26204
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

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Anthropic parallel hosted web search and local tool calls lose server_tool_use identity

2 participants