Skip to content

Build and run agents you can see, understand and trust.

fix(realtime): release completed tool-call names in dashscope (#2969)

ANIRUDDHA ADAK73ef4c6a16aa
diff --git a/src/agentscope/realtime/_dashscope/_model.py b/src/agentscope/realtime/_dashscope/_model.py
index b342e08a9..800965bd8 100644
--- a/src/agentscope/realtime/_dashscope/_model.py
+++ b/src/agentscope/realtime/_dashscope/_model.py
@@ -331,12 +331,15 @@ class DashScopeRealtimeModel(RealtimeModelBase):
at_ms=data.get("audio_end_ms", 0),
)
- case "response.output_item.added" | "response.output_item.done":
+ case "response.output_item.added":
item = data.get("item", {})
if item.get("type") == "function_call" and item.get("name"):
self._tool_names[item["call_id"]] = item["name"]
return None
+ case "response.output_item.done":
+ return None
+
case "response.function_call_arguments.delta":
call_id = data.get("call_id", "")
self._tool_args[call_id] = self._tool_args.get(
@@ -353,7 +356,9 @@ class DashScopeRealtimeModel(RealtimeModelBase):
call_id = data.get("call_id", "")
accumulated = self._tool_args.pop(call_id, "")
arguments = data.get("arguments") or accumulated
- name = data.get("name") or self._tool_names.pop(call_id, "")
+ name = self._tool_names.pop(call_id, "")
+ if data.get("name"):
+ name = data["name"]
return me.ToolCallEvent(
item_id=self._item_id,
tool_call=ToolCallBlock(
diff --git a/tests/realtime_dashscope_test.py b/tests/realtime_dashscope_test.py
index 4f7bf6d03..29c7ed3cf 100644
--- a/tests/realtime_dashscope_test.py
+++ b/tests/realtime_dashscope_test.py
@@ -219,6 +219,83 @@ class DashScopeParseTest(unittest.TestCase):
{"type": "response.created", "response": {"id": "r1"}},
)
+ def test_completed_tool_call_releases_its_name(self) -> None:
+ """A finished tool call must not leave a name mapping behind."""
+ self.model._parse(
+ {
+ "type": "response.output_item.added",
+ "item": {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "get_weather",
+ },
+ },
+ )
+ self.assertIn("call_1", self.model._tool_names)
+
+ event = self.model._parse(
+ {
+ "type": "response.function_call_arguments.done",
+ "call_id": "call_1",
+ "name": "get_weather",
+ "arguments": '{"city":"Beijing"}',
+ },
+ )
+ self.model._parse(
+ {
+ "type": "response.output_item.done",
+ "item": {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "get_weather",
+ "arguments": '{"city":"Beijing"}',
+ },
+ },
+ )
+
+ event_data = event.model_dump()
+ event_data["tool_call"].pop("created_at")
+ self.assertEqual(
+ event_data,
+ {
+ "item_id": "r1",
+ "tool_call": {
+ "type": "tool_call",
+ "id": "call_1",
+ "name": "get_weather",
+ "input": '{"city":"Beijing"}',
+ "state": "pending",
+ "suggested_rules": [],
+ "finished_at": None,
+ },
+ },
+ )
+ self.assertEqual(self.model._tool_names, {})
+
+ def test_name_fallback_still_works_without_a_name_on_done(self) -> None:
+ """A ``done`` frame without ``name`` still resolves via the cache."""
+ self.model._parse(
+ {
+ "type": "response.output_item.added",
+ "item": {
+ "type": "function_call",
+ "call_id": "call_2",
+ "name": "get_time",
+ },
+ },
+ )
+
+ event = self.model._parse(
+ {
+ "type": "response.function_call_arguments.done",
+ "call_id": "call_2",
+ "arguments": '{"tz":"UTC"}',
+ },
+ )
+
+ self.assertEqual(event.tool_call.name, "get_time")
+ self.assertEqual(self.model._tool_names, {})
+
def test_reply_frames(self) -> None:
"""Every reply-side frame maps to one model event."""
frames = [