Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(model): stream an Anthropic tool call without arguments as {} (#3008)
Mahmoud Zahere64cae413e48
diff --git a/src/agentscope/model/_anthropic/_model.py b/src/agentscope/model/_anthropic/_model.pyindex 28093009e..96f37f579 100644--- a/src/agentscope/model/_anthropic/_model.py+++ b/src/agentscope/model/_anthropic/_model.py@@ -439,6 +439,8 @@ class AnthropicChatModel(ChatModelBase):block_id_mapping: dict[int, str] = {}# The mapping from index to tool call idtool_call_mapping: dict = OrderedDict()+ # The indexes of the tool calls that received argument text+ tool_call_with_input: set[int] = set()async with response as stream:async for event in stream:@@ -537,12 +539,28 @@ class AnthropicChatModel(ChatModelBase):and block_index in tool_call_mapping):block_id, name = tool_call_mapping[block_index]+ if delta.partial_json:+ tool_call_with_input.add(block_index)delta_res.append_tool_call(block_id=block_id,name=name,input=delta.partial_json or "",)+ # A call without arguments streams no JSON text, so close it+ # with the empty object the non-streaming response carries+ elif (+ event.type == "content_block_stop"+ and event.index in tool_call_mapping+ and event.index not in tool_call_with_input+ ):+ block_id, name = tool_call_mapping[event.index]+ delta_res.append_tool_call(+ block_id=block_id,+ name=name,+ input="{}",+ )+elif event.type == "message_delta":if event.usage and usage:usage.output_tokens = event.usage.output_tokensdiff --git a/tests/model_anthropic_test.py b/tests/model_anthropic_test.pyindex 3e6e06e37..1f6f32d5c 100644--- a/tests/model_anthropic_test.py+++ b/tests/model_anthropic_test.py@@ -1013,6 +1013,70 @@ class TestAnthropicStream(IsolatedAsyncioTestCase):[{"role": "assistant", "content": content}],)+ async def test_stream_tool_call_without_arguments(self) -> None:+ """A tool call without arguments streams as ``{}``, as without+ streaming."""+ completion = anthropic_types.Message.model_validate(+ {+ "id": "msg-no-args",+ "type": "message",+ "role": "assistant",+ "model": self.model.model,+ "content": [+ {+ "type": "tool_use",+ "id": "toolu_1",+ "name": "list_tasks",+ "input": {},+ },+ ],+ "stop_reason": "tool_use",+ "stop_sequence": None,+ "usage": {"input_tokens": 10, "output_tokens": 20},+ },+ )+ events = _completion_events(completion)+ tool_delta = next(_ for _ in events if _.type == "content_block_delta")+ non_stream_model = _make_model(stream=False)+ non_stream_model.client = self.mock_client++ cases = {+ "no_delta": [_ for _ in events if _ is not tool_delta],+ "empty_delta": [+ (+ _.model_copy(+ update={+ "delta": _.delta.model_copy(+ update={"partial_json": ""},+ ),+ },+ )+ if _ is tool_delta+ else _+ )+ for _ in events+ ],+ }+ for name, case_events in cases.items():+ with self.subTest(name=name):+ self.mock_client.messages.create = AsyncMock(+ side_effect=[+ _MockAsyncEventStream(case_events),+ completion,+ ],+ )++ final = [r async for r in await self.model([])][-1]+ non_stream = await non_stream_model([])++ self.assertListEqual(+ [+ (_.id, _.name, _.input)+ for _ in (final.content[0], non_stream.content[0])+ ],+ [("toolu_1", "list_tasks", "{}")] * 2,+ )+# ---------------------------------------------------------------------------# _format_tools tests