Skip to content

Commit 7def61f

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 (line ~2800), then later re-yields the same function_call items from function_call_results (line ~2847). The _process_update callback concatenates streaming arguments via +=, so duplicate yields cause the arguments to be concatenated twice: {"location":"Seattle"}{"location":"Seattle"} Filter out function_call type items from the streamed update, since they were already emitted by the inner stream. Only yield function_result and approval items, which are newly generated by _process_function_requests. Tests: both split-chunk and single-chunk streaming of declaration-only tools. Fixes #6973
1 parent e57f046 commit 7def61f

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2400,8 +2400,17 @@ async def _process_function_requests(
24002400
had_errors=had_errors,
24012401
max_errors=max_errors,
24022402
)
2403-
result["function_call_results"] = list(function_call_results)
2403+
# Exclude function_call items from the streamed update: the inner stream already yielded
2404+
# them as they arrived, so re-yielding them would cause argument strings to be concatenated
2405+
# twice in _process_update (which uses +=) for declaration-only tools.
2406+
filtered_results = [r for r in function_call_results if r.type != "function_call"]
2407+
result["function_call_results"] = filtered_results
24042408
result["function_call_count"] = sum(1 for r in function_call_results if r.type == "function_result")
2409+
# If the filtered results are empty, suppress the update to avoid emitting a confusing empty
2410+
# ChatResponseUpdate event. Clear the update_role so the yield check (if role := result.get("update_role"))
2411+
# will skip this empty update.
2412+
if not filtered_results:
2413+
result["update_role"] = None
24052414
# If middleware requested termination, change action to return
24062415
if should_terminate:
24072416
result["action"] = "return"

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4786,3 +4786,90 @@ 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=[Content.from_function_call(call_id="call1", name="get_weather", arguments='{"location":"Seattle"}')],
4855+
role="assistant",
4856+
),
4857+
],
4858+
]
4859+
4860+
final_response = await chat_client_base.get_response( # type: ignore[call-overload] # pyrefly: ignore[no-matching-overload] # ty: ignore[no-matching-overload]
4861+
"What's the weather in Seattle?", # type: ignore[arg-type]
4862+
options={"tool_choice": "auto", "tools": [declaration_func]}, # type: ignore[arg-type]
4863+
stream=True, # type: ignore[arg-type]
4864+
).get_final_response()
4865+
4866+
function_calls = [
4867+
content
4868+
for msg in final_response.messages
4869+
for content in msg.contents
4870+
if content.type == "function_call" and content.call_id == "call1"
4871+
]
4872+
assert len(function_calls) == 1, f"Expected exactly 1 function_call, got {len(function_calls)}"
4873+
assert function_calls[0].arguments == '{"location":"Seattle"}', (
4874+
f"Arguments duplicated: {function_calls[0].arguments!r}"
4875+
)

0 commit comments

Comments
 (0)