@@ -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
26672758async def test_concurrent_streams_keep_approval_requests_request_local (
0 commit comments