Skip to content

Commit 5a09876

Browse files
Python: Keep streamed Anthropic server tool call input
A streamed server_tool_use block starts with input={} and its arguments arrive through input_json_delta events, which were dropped for server tools. A pending server tool call was therefore replayed with input={} after the local tool result, and the deferred search lost its query. Argument deltas of server tool calls parsed as hosted function calls are now kept as hosted, informational-only fragments with the call's call_id, so they merge into the call without making it a local function call. MCP and code execution deltas are still skipped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a03b285 commit 5a09876

2 files changed

Lines changed: 129 additions & 31 deletions

File tree

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class _AnthropicRequestState:
284284

285285
active_call_id: str | None = None
286286
active_call_content_type: str | None = None
287+
active_call_is_hosted_function: bool = False
287288
tool_name_aliases: dict[str, str] = field(default_factory=dict[str, str])
288289

289290

@@ -1454,6 +1455,7 @@ def _parse_contents_from_anthropic(
14541455
case "tool_use" | "mcp_tool_use" | "server_tool_use":
14551456
request_state.active_call_id = content_block.id
14561457
request_state.active_call_content_type = content_block.type
1458+
request_state.active_call_is_hosted_function = False
14571459
if content_block.type == "mcp_tool_use":
14581460
contents.append(
14591461
Content.from_mcp_server_tool_call(
@@ -1480,6 +1482,7 @@ def _parse_contents_from_anthropic(
14801482
)
14811483
else:
14821484
resolved_tool_name = request_state.tool_name_aliases.get(content_block.name, content_block.name)
1485+
request_state.active_call_is_hosted_function = content_block.type == "server_tool_use"
14831486
contents.append(
14841487
Content.from_function_call(
14851488
call_id=content_block.id,
@@ -1725,16 +1728,20 @@ def _parse_contents_from_anthropic(
17251728
)
17261729
)
17271730
case "input_json_delta":
1728-
# Skip argument deltas for MCP and server tools — execution is handled server-side.
1729-
if request_state.active_call_content_type in ("mcp_tool_use", "server_tool_use"):
1730-
pass
1731-
else:
1731+
# MCP and code execution server tool calls are parsed into their own content types, so
1732+
# their argument deltas are skipped. Argument deltas of other server tool calls (such as
1733+
# web search) are kept as hosted, informational-only fragments that merge into the call,
1734+
# so a pending call is replayed with its full input.
1735+
hosted_call = request_state.active_call_is_hosted_function
1736+
if hosted_call or request_state.active_call_content_type not in ("mcp_tool_use", "server_tool_use"):
17321737
call_id = request_state.active_call_id or ""
17331738
contents.append(
17341739
Content.from_function_call(
17351740
call_id=call_id,
17361741
name="",
17371742
arguments=content_block.partial_json,
1743+
informational_only=hosted_call,
1744+
hosted=hosted_call,
17381745
merge_mode="append" if streaming else None,
17391746
raw_representation=content_block,
17401747
)

‎python/packages/anthropic/tests/test_anthropic_client.py‎

Lines changed: 118 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,14 +2001,13 @@ def test_parse_contents_from_anthropic_input_json_delta_no_duplicate_name(
20012001
assert result[0].arguments == '"San Francisco"}'
20022002

20032003

2004-
def test_parse_contents_server_tool_use_input_json_delta_ignored(
2004+
def test_parse_contents_server_tool_use_input_json_delta_is_hosted_and_informational(
20052005
mock_anthropic_client: MagicMock,
20062006
) -> None:
2007-
"""Regression test: input_json_delta events are ignored after a server_tool_use block.
2007+
"""input_json_delta events after a server_tool_use block stay hosted, informational-only fragments.
20082008
2009-
Server-managed tools have their execution handled server-side, so streaming
2010-
input_json_delta events must not produce Content.from_function_call(name='')
2011-
entries that would cause Anthropic API 400 errors on subsequent turns.
2009+
The fragments carry the server tool call's ``call_id`` and an empty name, so they merge into the
2010+
call and give it the streamed input without making it a local function call.
20122011
"""
20132012
client = create_test_anthropic_client(mock_anthropic_client)
20142013
request_state = _AnthropicRequestState()
@@ -2020,31 +2019,58 @@ def test_parse_contents_server_tool_use_input_json_delta_ignored(
20202019
server_tool_content.name = "web_search"
20212020
server_tool_content.input = {}
20222021

2023-
result = client._parse_contents_from_anthropic([server_tool_content], request_state)
2022+
result = client._parse_contents_from_anthropic([server_tool_content], request_state, streaming=True)
20242023
# server_tool_use falls through to informational-only function_call (not mcp_tool_use / code_execution)
20252024
assert len(result) == 1
20262025
assert result[0].type == "function_call"
20272026
assert result[0].informational_only is True
20282027
assert result[0].hosted is True
20292028
assert request_state.active_call_content_type == "server_tool_use"
2029+
call = result[0]
2030+
2031+
for partial_json in ('{"query": "latest', ' news"}'):
2032+
delta_content = MagicMock()
2033+
delta_content.type = "input_json_delta"
2034+
delta_content.partial_json = partial_json
2035+
2036+
result = client._parse_contents_from_anthropic([delta_content], request_state, streaming=True)
2037+
assert len(result) == 1
2038+
assert result[0].type == "function_call"
2039+
assert result[0].call_id == "srvtool_abc"
2040+
assert result[0].name == ""
2041+
assert result[0].arguments == partial_json
2042+
assert result[0].informational_only is True
2043+
assert result[0].hosted is True
2044+
call += result[0]
2045+
2046+
assert call.name == "web_search"
2047+
assert call.informational_only is True
2048+
assert call.hosted is True
2049+
assert call.parse_arguments() == {"query": "latest news"}
2050+
2051+
2052+
def test_parse_contents_mcp_and_code_execution_input_json_delta_ignored(
2053+
mock_anthropic_client: MagicMock,
2054+
) -> None:
2055+
"""input_json_delta events after MCP and code execution tool blocks produce no content."""
2056+
client = create_test_anthropic_client(mock_anthropic_client)
20302057

2031-
# input_json_delta events after server_tool_use must be silently ignored
2032-
delta_content = MagicMock()
2033-
delta_content.type = "input_json_delta"
2034-
delta_content.partial_json = '{"query": "latest news"}'
2035-
2036-
result = client._parse_contents_from_anthropic([delta_content], request_state)
2037-
assert result == [], "input_json_delta after server_tool_use should produce no content, but got: %r" % result
2058+
for block_type, name in (("mcp_tool_use", "search_docs"), ("server_tool_use", "code_execution")):
2059+
request_state = _AnthropicRequestState()
2060+
tool_content = MagicMock()
2061+
tool_content.type = block_type
2062+
tool_content.id = "call_abc"
2063+
tool_content.name = name
2064+
tool_content.server_name = "docs"
2065+
tool_content.input = {}
2066+
assert client._parse_contents_from_anthropic([tool_content], request_state, streaming=True)
20382067

2039-
# A second delta must also be ignored
2040-
delta_content_2 = MagicMock()
2041-
delta_content_2.type = "input_json_delta"
2042-
delta_content_2.partial_json = '{"extra": true}'
2068+
delta_content = MagicMock()
2069+
delta_content.type = "input_json_delta"
2070+
delta_content.partial_json = '{"query": "latest news"}'
20432071

2044-
result = client._parse_contents_from_anthropic([delta_content_2], request_state)
2045-
assert result == [], (
2046-
"subsequent input_json_delta after server_tool_use should also be ignored, but got: %r" % result
2047-
)
2072+
result = client._parse_contents_from_anthropic([delta_content], request_state, streaming=True)
2073+
assert result == [], f"input_json_delta after {block_type} {name} should produce no content: {result!r}"
20482074

20492075

20502076
# Stream Processing Tests
@@ -2435,6 +2461,67 @@ async def create(**kwargs: Any) -> Any:
24352461
assert response.text == "done"
24362462

24372463

2464+
async def test_streaming_parallel_server_tool_use_replays_streamed_input(
2465+
mock_anthropic_client: MagicMock,
2466+
) -> None:
2467+
"""A streamed pending server tool call is replayed with the input that arrived through argument deltas."""
2468+
executions = 0
2469+
2470+
@tool(name="local_value", approval_mode="never_require")
2471+
def local_value() -> str:
2472+
nonlocal executions
2473+
executions += 1
2474+
return "local-ok"
2475+
2476+
async def parallel_tool_events() -> Any:
2477+
yield _message_start_event("tool-response")
2478+
yield _tool_start_event(block_type="tool_use", call_id="toolu_local", name="local_value", index=0)
2479+
yield _content_block_stop_event(index=0)
2480+
yield _tool_start_event(block_type="server_tool_use", call_id="srvtoolu_search", name="web_search", index=1)
2481+
yield _argument_delta_event('{"query": "agent framework')
2482+
yield _argument_delta_event(' release"}')
2483+
yield _content_block_stop_event(index=1)
2484+
yield _message_delta_event("tool_use")
2485+
yield _message_stop_event()
2486+
2487+
async def final_events() -> Any:
2488+
yield _message_start_event("final-response")
2489+
yield _message_delta_event("end_turn")
2490+
yield _message_stop_event()
2491+
2492+
requests: list[dict[str, Any]] = []
2493+
2494+
async def create(**kwargs: Any) -> Any:
2495+
requests.append(kwargs)
2496+
return parallel_tool_events() if len(requests) == 1 else final_events()
2497+
2498+
client = create_test_anthropic_client(mock_anthropic_client)
2499+
mock_anthropic_client.beta.messages.create.side_effect = create
2500+
stream = client.get_response(
2501+
[Message(role="user", contents=["Call local_value and search the web."])],
2502+
options={"tools": [client.get_web_search_tool(), local_value], "max_tokens": 64},
2503+
stream=True,
2504+
)
2505+
_ = [update async for update in stream]
2506+
await stream.get_final_response()
2507+
2508+
assert executions == 1
2509+
assert len(requests) == 2
2510+
replayed_blocks = [
2511+
block
2512+
for message in requests[1]["messages"]
2513+
if isinstance(message["content"], list)
2514+
for block in message["content"]
2515+
if block["type"] in ("tool_use", "server_tool_use", "tool_result")
2516+
]
2517+
assert [(block["type"], block.get("id") or block.get("tool_use_id")) for block in replayed_blocks] == [
2518+
("tool_use", "toolu_local"),
2519+
("server_tool_use", "srvtoolu_search"),
2520+
("tool_result", "toolu_local"),
2521+
]
2522+
assert replayed_blocks[1]["input"] == {"query": "agent framework release"}
2523+
2524+
24382525
@pytest.mark.parametrize(
24392526
("close_block", "stop_reason", "include_message_stop"),
24402527
[
@@ -2656,12 +2743,16 @@ async def consume_primary() -> tuple[list[ChatResponseUpdate], ChatResponse]:
26562743
assert primary_calls[0].call_id == "shared-call"
26572744
assert primary_calls[0].parse_arguments() == {"command": "pwd"}
26582745

2659-
hosted_calls = [content for content in hosted_contents if content.type == "function_call"]
2660-
assert len(hosted_calls) == 1
2661-
assert hosted_calls[0].name == "web_search"
2662-
assert hosted_calls[0].call_id == "shared-call"
2663-
assert hosted_calls[0].arguments == {}
2664-
assert hosted_calls[0].informational_only is True
2746+
assert not any(content.hosted or content.informational_only for content in primary_fragments)
2747+
2748+
hosted_fragments = [content for content in hosted_contents if content.type == "function_call"]
2749+
assert [
2750+
(content.name, content.call_id, content.arguments, content.hosted, content.informational_only)
2751+
for content in hosted_fragments
2752+
] == [
2753+
("web_search", "shared-call", {}, True, True),
2754+
("", "shared-call", '{"query":"framework"}', True, True),
2755+
]
26652756

26662757

26672758
async def test_concurrent_streams_keep_approval_requests_request_local(

0 commit comments

Comments
 (0)