Skip to content

Commit 10aa390

Browse files
committed
fix: avoid duplicating arguments in streamed declaration-only tool calls (#6973)
When streaming a declaration-only function call, the _stream() loop yields function_call chunks from the inner stream, then _process_function_requests re-emits the finalized call from function_call_results. _process_update merges same-call_id function_call contents via +=, so re-emitting the full arguments concatenated them twice: {"location":"Seattle"}{"location":"Seattle"}. The finalized call gains its control metadata (user_input_request=True, id) only during post-processing, so it must still reach the stream: AgentExecutor relies on that signal to emit request_info and pause for client-side tools. Fix: strip only the already-streamed arguments from function_call items in the streamed update while preserving their metadata, instead of dropping the items entirely. Also preserve id and user_input_request when merging function_call contents in Content.__add__ so the aggregated response retains the signal. Tests: split-chunk and single-chunk no-duplication, plus metadata preservation. Fixes #6973
1 parent 85c00fc commit 10aa390

3 files changed

Lines changed: 177 additions & 1 deletion

File tree

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
import contextvars
7+
import copy
78
import inspect
89
import json
910
import logging
@@ -2400,7 +2401,20 @@ async def _process_function_requests(
24002401
had_errors=had_errors,
24012402
max_errors=max_errors,
24022403
)
2403-
result["function_call_results"] = list(function_call_results)
2404+
# The inner stream already yielded the declaration-only function_call chunks as they
2405+
# arrived, so re-emitting their arguments would make _process_update (which merges via +=)
2406+
# concatenate the argument string twice. Strip the arguments from any function_call items
2407+
# while retaining their control metadata (id, user_input_request) so streaming consumers
2408+
# (e.g. AgentExecutor) still receive the request-info/pause signal without duplicated arguments.
2409+
streamed_results: list[Content] = []
2410+
for r in function_call_results:
2411+
if r.type == "function_call":
2412+
metadata_only = copy.copy(r)
2413+
metadata_only.arguments = None
2414+
streamed_results.append(metadata_only)
2415+
else:
2416+
streamed_results.append(r)
2417+
result["function_call_results"] = streamed_results
24042418
result["function_call_count"] = sum(1 for r in function_call_results if r.type == "function_result")
24052419
# If middleware requested termination, change action to return
24062420
if should_terminate:

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,10 @@ def _add_function_call_content(self, other: Content) -> Content:
15481548
call_id=self_call_id,
15491549
name=getattr(self, "name", None) or getattr(other, "name", None),
15501550
arguments=arguments,
1551+
id=getattr(self, "id", None) or getattr(other, "id", None),
1552+
user_input_request=(
1553+
getattr(self, "user_input_request", None) or getattr(other, "user_input_request", None)
1554+
),
15511555
exception=getattr(self, "exception", None) or getattr(other, "exception", None),
15521556
informational_only=getattr(self, "informational_only", False)
15531557
or getattr(other, "informational_only", False),

‎python/packages/core/tests/core/test_function_invocation_logic.py‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)