@@ -4786,3 +4786,161 @@ def a(x: int) -> int:
47864786
47874787
47884788# endregion
4789+
4790+
4791+ async def test_declaration_only_tool_streaming_no_argument_duplication (
4792+ chat_client_base : SupportsChatGetResponse ,
4793+ ):
4794+ """Streaming a declaration-only tool must not duplicate arguments in the final response (fix #6973)."""
4795+ from agent_framework import FunctionTool
4796+
4797+ declaration_func = FunctionTool (
4798+ name = "get_weather" ,
4799+ func = None ,
4800+ description = "Get the weather" ,
4801+ input_model = {"type" : "object" , "properties" : {"location" : {"type" : "string" }}, "required" : ["location" ]},
4802+ )
4803+ assert declaration_func .declaration_only is True
4804+
4805+ # Stream arrives in two chunks, arguments split across them
4806+ chat_client_base .streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
4807+ [
4808+ ChatResponseUpdate (
4809+ contents = [Content .from_function_call (call_id = "call1" , name = "get_weather" , arguments = '{"location":' )],
4810+ role = "assistant" ,
4811+ ),
4812+ ChatResponseUpdate (
4813+ contents = [Content .from_function_call (call_id = "call1" , name = "get_weather" , arguments = '"Seattle"}' )],
4814+ role = "assistant" ,
4815+ ),
4816+ ],
4817+ ]
4818+
4819+ updates = []
4820+ async for update in chat_client_base .get_response ( # type: ignore[call-overload] # pyrefly: ignore[no-matching-overload] # ty: ignore[no-matching-overload]
4821+ "What's the weather in Seattle?" , # type: ignore[arg-type]
4822+ options = {"tool_choice" : "auto" , "tools" : [declaration_func ]}, # type: ignore[arg-type]
4823+ stream = True , # type: ignore[arg-type]
4824+ ):
4825+ updates .append (update )
4826+
4827+ # Collect arguments from all streamed function_call updates
4828+ all_args = ""
4829+ for update in updates :
4830+ for content in update .contents :
4831+ if content .type == "function_call" and content .call_id == "call1" :
4832+ all_args += content .arguments or ""
4833+
4834+ # Arguments must appear exactly once - not duplicated
4835+ assert all_args == '{"location":"Seattle"}' , f"Expected single copy of arguments, got: { all_args !r} "
4836+
4837+
4838+ async def test_declaration_only_tool_streaming_single_chunk_no_duplication (
4839+ chat_client_base : SupportsChatGetResponse ,
4840+ ):
4841+ """Single-chunk streaming of a declaration-only tool must not duplicate arguments (fix #6973)."""
4842+ from agent_framework import FunctionTool
4843+
4844+ declaration_func = FunctionTool (
4845+ name = "get_weather" ,
4846+ func = None ,
4847+ description = "Get the weather" ,
4848+ input_model = {"type" : "object" , "properties" : {"location" : {"type" : "string" }}, "required" : ["location" ]},
4849+ )
4850+
4851+ chat_client_base .streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
4852+ [
4853+ ChatResponseUpdate (
4854+ contents = [
4855+ Content .from_function_call (call_id = "call1" , name = "get_weather" , arguments = '{"location":"Seattle"}' )
4856+ ],
4857+ role = "assistant" ,
4858+ ),
4859+ ],
4860+ ]
4861+
4862+ final_response = await chat_client_base .get_response ( # type: ignore[call-overload] # pyrefly: ignore[no-matching-overload] # ty: ignore[no-matching-overload]
4863+ "What's the weather in Seattle?" , # type: ignore[arg-type]
4864+ options = {"tool_choice" : "auto" , "tools" : [declaration_func ]}, # type: ignore[arg-type]
4865+ stream = True , # type: ignore[arg-type]
4866+ ).get_final_response ()
4867+
4868+ function_calls = [
4869+ content
4870+ for msg in final_response .messages
4871+ for content in msg .contents
4872+ if content .type == "function_call" and content .call_id == "call1"
4873+ ]
4874+ assert len (function_calls ) == 1 , f"Expected exactly 1 function_call, got { len (function_calls )} "
4875+ assert function_calls [0 ].arguments == '{"location":"Seattle"}' , (
4876+ f"Arguments duplicated: { function_calls [0 ].arguments !r} "
4877+ )
4878+
4879+
4880+ async def test_declaration_only_tool_streaming_preserves_user_input_request_metadata (
4881+ chat_client_base : SupportsChatGetResponse ,
4882+ ):
4883+ """Streaming a declaration-only tool must retain user_input_request/id metadata (fix #6973).
4884+
4885+ The inner stream yields raw argument chunks without the control metadata; the finalized
4886+ call gains ``user_input_request=True`` and ``id`` during post-processing. Streaming
4887+ consumers (e.g. AgentExecutor) rely on that signal to emit request_info and pause, so it
4888+ must reach the stream even though the duplicated arguments are suppressed.
4889+ """
4890+ from agent_framework import FunctionTool
4891+
4892+ declaration_func = FunctionTool (
4893+ name = "get_weather" ,
4894+ func = None ,
4895+ description = "Get the weather" ,
4896+ input_model = {"type" : "object" , "properties" : {"location" : {"type" : "string" }}, "required" : ["location" ]},
4897+ )
4898+ assert declaration_func .declaration_only is True
4899+
4900+ chat_client_base .streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
4901+ [
4902+ ChatResponseUpdate (
4903+ contents = [Content .from_function_call (call_id = "call1" , name = "get_weather" , arguments = '{"location":' )],
4904+ role = "assistant" ,
4905+ ),
4906+ ChatResponseUpdate (
4907+ contents = [Content .from_function_call (call_id = "call1" , name = "get_weather" , arguments = '"Seattle"}' )],
4908+ role = "assistant" ,
4909+ ),
4910+ ],
4911+ ]
4912+
4913+ stream = chat_client_base .get_response ( # type: ignore[call-overload] # pyrefly: ignore[no-matching-overload] # ty: ignore[no-matching-overload]
4914+ "What's the weather in Seattle?" , # type: ignore[arg-type]
4915+ options = {"tool_choice" : "auto" , "tools" : [declaration_func ]}, # type: ignore[arg-type]
4916+ stream = True , # type: ignore[arg-type]
4917+ )
4918+
4919+ updates = []
4920+ async for update in stream :
4921+ updates .append (update )
4922+
4923+ # At least one streamed update must carry the user_input_request signal with the call id,
4924+ # so consumers can pause the workflow / emit request_info for the client-side tool.
4925+ signalled = [
4926+ content
4927+ for update in updates
4928+ for content in update .contents
4929+ if content .type == "function_call" and content .call_id == "call1" and content .user_input_request
4930+ ]
4931+ assert signalled , "Expected a streamed update carrying user_input_request metadata for the declaration-only call"
4932+ assert any (getattr (content , "id" , None ) == "call1" for content in signalled ), (
4933+ "Expected the finalized declaration-only call to retain its id"
4934+ )
4935+
4936+ # The finalized aggregated response must also retain the signal without duplicated arguments.
4937+ final_response = await stream .get_final_response ()
4938+ requests = [
4939+ content
4940+ for msg in final_response .messages
4941+ for content in msg .contents
4942+ if content .type == "function_call" and content .call_id == "call1" and content .user_input_request
4943+ ]
4944+ assert len (requests ) == 1 , f"Expected exactly one user input request, got { len (requests )} "
4945+ assert requests [0 ].user_input_request is True
4946+ assert requests [0 ].arguments == '{"location":"Seattle"}' , f"Arguments duplicated or lost: { requests [0 ].arguments !r} "
0 commit comments