From 04cbba5de4a2a74c04106f955d5baaf382416cb3 Mon Sep 17 00:00:00 2001 From: pratikwayase Date: Wed, 11 Nov 2026 11:30:22 +0530 Subject: [PATCH] fix:ResponsesHostServer duplicates function-call --- .../_responses.py | 12 +- .../foundry_hosting/tests/test_responses.py | 199 ++++++++++++++++++ 2 files changed, 205 insertions(+), 6 deletions(-) diff --git a/python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py b/python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py index 02b6993d50e..1ee036dd11e 100644 --- a/python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py +++ b/python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py @@ -2797,9 +2797,9 @@ async def handle( for event in self._open_function_call(content): yield event args_str = _json_safe_to_str(content.arguments) - self._accumulated.append(args_str) - if self._fc_builder is not None: - yield self._fc_builder.emit_arguments_delta(args_str) + delta = self._consume_text_update(args_str, content.merge_mode) + if delta and self._fc_builder is not None: + yield self._fc_builder.emit_arguments_delta(delta) elif content.type == "function_result": for event in self._close(): @@ -2825,9 +2825,9 @@ async def handle( for event in self._open_mcp_call(content): yield event args_str = _json_safe_to_str(content.arguments) - self._accumulated.append(args_str) - if self._mcp_builder is not None: - yield self._mcp_builder.emit_arguments_delta(args_str) + delta = self._consume_text_update(args_str, content.merge_mode) + if delta and self._mcp_builder is not None: + yield self._mcp_builder.emit_arguments_delta(delta) elif ( content.type == "mcp_server_tool_result" diff --git a/python/packages/foundry_hosting/tests/test_responses.py b/python/packages/foundry_hosting/tests/test_responses.py index b605b27a625..1b24defdb31 100644 --- a/python/packages/foundry_hosting/tests/test_responses.py +++ b/python/packages/foundry_hosting/tests/test_responses.py @@ -5512,6 +5512,205 @@ async def test_function_call_streaming(self) -> None: assert len(args_done) == 1 assert args_done[0]["data"]["arguments"] == '{"q": "hello"}' + async def test_function_call_replace_snapshot_not_appended(self) -> None: + """A ``replace`` snapshot (function_call_arguments.done) must not be appended after streamed fragments.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[Content.from_function_call("call_1", "get_weather", arguments="{", merge_mode="append")], + role="assistant", + ), + AgentResponseUpdate( + contents=[ + Content.from_function_call( + "call_1", "get_weather", arguments='"location":"Seattle"}', merge_mode="append" + ) + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[ + Content.from_function_call( + "call_1", "get_weather", arguments='{"location":"Seattle"}', merge_mode="replace" + ) + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[Content.from_function_result("call_1", result="The weather in Seattle is sunny.")], + role="assistant", + ), + ] + ) + server = _make_server(agent) + resp = await _post(server, stream=True) + + assert resp.status_code == 200 + events = _parse_sse_events(resp.text) + types = _sse_event_types(events) + + # Two fragment deltas only; the replace snapshot equals the accumulated + # fragments, so it must NOT be re-emitted as a third delta. + assert types.count("response.function_call_arguments.delta") == 2 + + args_done = [e for e in events if e["event"] == "response.function_call_arguments.done"] + assert len(args_done) == 1 + assert args_done[0]["data"]["arguments"] == '{"location":"Seattle"}' + + completed = events[-1]["data"]["response"] + fc_item = next(item for item in completed["output"] if item["type"] == "function_call") + assert fc_item["arguments"] == '{"location":"Seattle"}' + assert fc_item["status"] == "completed" + + async def test_function_call_replace_snapshot_extends_fragments(self) -> None: + """When the replace snapshot carries more than the streamed fragments, only the unsent suffix streams.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[ + Content.from_function_call("call_2", "get_weather", + arguments='{"loc', merge_mode="append") + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[ + Content.from_function_call( + "call_2", "get_weather", arguments='{"location":"Seattle"}', merge_mode="replace" + ) + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[Content.from_function_result("call_2", result="The weather in Seattle is sunny.")], + role="assistant", + ), + ] + ) + server = _make_server(agent) + resp = await _post(server, stream=True) + + assert resp.status_code == 200 + events = _parse_sse_events(resp.text) + + args_deltas = [e for e in events if e["event"] == "response.function_call_arguments.delta"] + assert [e["data"]["delta"] for e in args_deltas] == ['{"loc', 'ation":"Seattle"}'] + + args_done = [e for e in events if e["event"] == "response.function_call_arguments.done"] + assert len(args_done) == 1 + assert args_done[0]["data"]["arguments"] == '{"location":"Seattle"}' + + async def test_function_call_replace_snapshot_without_fragments(self) -> None: + """A replace snapshot with no prior fragments streams the whole snapshot once as the delta.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[ + Content.from_function_call( + "call_3", "get_weather", arguments='{"location":"Seattle"}', merge_mode="replace" + ) + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[Content.from_function_result("call_3", result="Sunny.")], + role="assistant", + ), + ] + ) + server = _make_server(agent) + resp = await _post(server, stream=True) + + assert resp.status_code == 200 + events = _parse_sse_events(resp.text) + + args_deltas = [e for e in events if e["event"] == "response.function_call_arguments.delta"] + assert [e["data"]["delta"] for e in args_deltas] == ['{"location":"Seattle"}'] + + args_done = [e for e in events if e["event"] == "response.function_call_arguments.done"] + assert len(args_done) == 1 + assert args_done[0]["data"]["arguments"] == '{"location":"Seattle"}' + + async def test_function_call_divergent_snapshot_fails_stream(self) -> None: + """A replace snapshot that is not an extension of the fragments fails the stream, like text does.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[ + Content.from_function_call("call_4", "get_weather", arguments='{"a":1', merge_mode="append") + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[ + Content.from_function_call("call_4", "get_weather", arguments='{"b":2}', merge_mode="replace") + ], + role="assistant", + ), + ] + ) + + resp = await _post(_make_server(agent), stream=True) + + events = _parse_sse_events(resp.text) + assert events[-1]["event"] == "response.failed" + assert "divergent replacement text snapshot" in events[-1]["data"]["response"]["error"]["message"] + + async def test_function_call_without_arguments_emits_no_empty_delta(self) -> None: + """A call with no arguments closes with empty arguments and emits no empty delta event.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[Content.from_function_call("call_5", "ping")], + role="assistant", + ), + AgentResponseUpdate( + contents=[Content.from_function_result("call_5", result="pong")], + role="assistant", + ), + ] + ) + server = _make_server(agent) + resp = await _post(server, stream=True) + + assert resp.status_code == 200 + events = _parse_sse_events(resp.text) + + assert "response.function_call_arguments.delta" not in _sse_event_types(events) + args_done = [e for e in events if e["event"] == "response.function_call_arguments.done"] + assert len(args_done) == 1 + assert args_done[0]["data"]["arguments"] == "" + + async def test_non_streaming_function_call_replace_snapshot_not_appended(self) -> None: + """Non-streaming requests collect the same streamed updates; arguments stay the single snapshot.""" + agent = _make_agent( + stream_updates=[ + AgentResponseUpdate( + contents=[Content.from_function_call("call_6", "get_weather", arguments="{", merge_mode="append")], + role="assistant", + ), + AgentResponseUpdate( + contents=[ + Content.from_function_call( + "call_6", "get_weather", arguments='{"location":"Seattle"}', merge_mode="replace" + ) + ], + role="assistant", + ), + AgentResponseUpdate( + contents=[Content.from_function_result("call_6", result="Sunny.")], + role="assistant", + ), + ] + ) + server = _make_server(agent) + resp = await _post(server, stream=False) + + assert resp.status_code == 200 + fc_items = [item for item in resp.json()["output"] if item["type"] == "function_call"] + assert len(fc_items) == 1 + assert fc_items[0]["arguments"] == '{"location":"Seattle"}' + async def test_function_result_omits_internal_exception(self) -> None: agent = _make_agent( stream_updates=[