Skip to content

Python: Fix server_tool_use input_json_delta handling and improve Anthropic samples - #5050

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
mainfrom
fix/anthropic-server-tool-use-and-samples
Apr 2, 2026
Merged

Python: Fix server_tool_use input_json_delta handling and improve Anthropic samples#5050
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
mainfrom
fix/anthropic-server-tool-use-and-samples

Conversation

@chetantoshniwal

@chetantoshniwal chetantoshniwal commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Anthropic Sample fix and regression test coverage for the server_tool_use streaming edge case.

Description

  • Fix: Skip input_json_delta for server_tool_use content blocks in AnthropicClient streaming. Server-managed tools (e.g., skills with code interpreter) were producing Content.from_function_call(name='') entries that caused Anthropic API 400 errors on subsequent turns.

  • Test: Added regression test test_parse_contents_server_tool_use_input_json_delta_ignored to verify that after a server_tool_use content block, subsequent input_json_delta events produce no Content.from_function_call(name='') entries, preventing the fix from silently regressing.

  • Samples: Add dotenv loading and environment variable documentation to Anthropic Claude samples (MCP, permissions, session, shell, tools, URL, skills).

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.

…amples

- Fix: Skip input_json_delta for server_tool_use content blocks in AnthropicClient streaming. Server-managed tools (e.g., skills with code interpreter) were producing Content.from_function_call(name='') entries that caused Anthropic API 400 errors on subsequent turns.

- Samples: Add dotenv loading and environment variable documentation to Anthropic Claude samples (MCP, permissions, session, shell, tools, URL, skills).
Copilot AI review requested due to automatic review settings April 2, 2026 03:42
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Apr 2, 2026
@github-actions github-actions Bot changed the title Fix server_tool_use input_json_delta handling and improve Anthropic samples Python: Fix server_tool_use input_json_delta handling and improve Anthropic samples Apr 2, 2026
@markwallace-microsoft

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py4373591%446, 449, 530, 623, 625, 768, 795–796, 874, 904–905, 950, 966–967, 974–976, 980–982, 986–989, 1103, 1113, 1165, 1286, 1313–1314, 1331, 1344, 1357, 1382–1383
TOTAL26994318788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5341 20 💤 0 ❌ 0 🔥 1m 22s ⏱️

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

This PR addresses an Anthropic streaming edge case in the Python Anthropic chat client (avoiding invalid tool-call content during server-managed tool execution) and updates the Anthropic Claude Python samples to make environment setup clearer and more consistent.

Changes:

  • Skip input_json_delta parsing for server_tool_use (in addition to mcp_tool_use) to prevent generating Content.from_function_call(name="") that can cause Anthropic 400s on subsequent turns.
  • Update Anthropic Claude samples to document required environment variables.
  • Add .env loading (python-dotenv) to several Anthropic Claude samples for easier local execution.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
python/packages/anthropic/agent_framework_anthropic/_chat_client.py Prevents emitting invalid function-call delta content for server-managed tools during streaming.
python/samples/02-agents/providers/anthropic/anthropic_skills.py Documents required Anthropic environment variables for the skills sample.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_url.py Adds .env loading and documents ANTHROPIC_API_KEY.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_tools.py Adds .env loading and documents ANTHROPIC_API_KEY.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_shell.py Adds .env loading and documents ANTHROPIC_API_KEY.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_session.py Adds .env loading and documents ANTHROPIC_API_KEY.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_multiple_permissions.py Adds .env loading and documents ANTHROPIC_API_KEY.
python/samples/02-agents/providers/anthropic/anthropic_claude_with_mcp.py Documents ANTHROPIC_API_KEY for the MCP sample.

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

Copilot AI commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Merged via the queue into main with commit 47d8291 Apr 2, 2026
31 checks passed
Ben Thomas (alliscode) pushed a commit to alliscode/agent-framework that referenced this pull request Apr 3, 2026
…hropic samples (microsoft#5050)

* Fix server_tool_use input_json_delta handling and improve Anthropic samples

- Fix: Skip input_json_delta for server_tool_use content blocks in AnthropicClient streaming. Server-managed tools (e.g., skills with code interpreter) were producing Content.from_function_call(name='') entries that caused Anthropic API 400 errors on subsequent turns.

- Samples: Add dotenv loading and environment variable documentation to Anthropic Claude samples (MCP, permissions, session, shell, tools, URL, skills).

* Add regression test for server_tool_use + input_json_delta skip behavior

Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/7c68dcb2-b577-4e36-b423-664b8fe3ac1d

Co-authored-by: chetantoshniwal <255221507+chetantoshniwal@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: chetantoshniwal <255221507+chetantoshniwal@users.noreply.github.com>
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.

6 participants