Python: Fix server_tool_use input_json_delta handling and improve Anthropic samples - #5050
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits intoApr 2, 2026
Conversation
…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).
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
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_deltaparsing forserver_tool_use(in addition tomcp_tool_use) to prevent generatingContent.from_function_call(name="")that can cause Anthropic 400s on subsequent turns. - Update Anthropic Claude samples to document required environment variables.
- Add
.envloading (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. |
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>
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:
If you need me to access, download, or install something from one of these locations, you can either:
|
Eduard van Valkenburg (eavanvalkenburg)
approved these changes
Apr 2, 2026
Eduard van Valkenburg (eavanvalkenburg)
enabled auto-merge
April 2, 2026 09:33
SergeyMenshykh
approved these changes
Apr 2, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_ignoredto verify that after aserver_tool_usecontent block, subsequentinput_json_deltaevents produce noContent.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