Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(model): handle Gemini function calls without an id (#1883)
Yufeng Heca95e3ea4269
diff --git a/src/agentscope/model/_gemini/_model.py b/src/agentscope/model/_gemini/_model.pyindex ce84e678f..66fde201e 100644--- a/src/agentscope/model/_gemini/_model.py+++ b/src/agentscope/model/_gemini/_model.py@@ -347,7 +347,7 @@ class GeminiChatModel(ChatModelBase):part.thought_signature,).decode("utf-8")else:- call_id = part.function_call.id+ call_id = part.function_call.id or uuid.uuid4().hexinput_str = json.dumps(keyword_args,ensure_ascii=False,@@ -431,7 +431,7 @@ class GeminiChatModel(ChatModelBase):part.thought_signature,).decode("utf-8")else:- call_id = part.function_call.id+ call_id = part.function_call.id or uuid.uuid4().hexcontent_blocks.append(ToolCallBlock(id=call_id,diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.pyindex 0faebf275..f587d5ee0 100644--- a/tests/model_gemini_test.py+++ b/tests/model_gemini_test.py@@ -170,6 +170,46 @@ class TestGeminiNonStream(IsolatedAsyncioTestCase):),)+ @patch("google.genai.Client")+ async def test_tool_call_response_without_id(+ self,+ mock_client_cls: MagicMock,+ ) -> None:+ """A function call with no id gets a generated id, not a crash."""+ parts = [+ _make_part(+ function_call={+ "name": "get_weather",+ "args": {"city": "Tokyo"},+ "id": None,+ },+ ),+ ]+ mock_client_cls.return_value.aio.models.generate_content = AsyncMock(+ return_value=_mock_completion(parts),+ )++ result = await self.model([])++ self.assertEqual(+ (result.is_last, result.content),+ (+ True,+ [+ ToolCallBlock.model_construct(+ id=A,+ name="get_weather",+ input=json.dumps(+ {"city": "Tokyo"},+ ensure_ascii=False,+ ),+ ),+ ],+ ),+ )+ self.assertIsInstance(result.content[0].id, str)+ self.assertTrue(result.content[0].id)+@patch("google.genai.Client")async def test_thinking_response(self,@@ -309,6 +349,53 @@ class TestGeminiStream(IsolatedAsyncioTestCase):],)+ @patch("google.genai.Client")+ async def test_stream_tool_calls_without_id(+ self,+ mock_client_cls: MagicMock,+ ) -> None:+ """Two id-less function calls in one chunk get distinct ids."""+ chunks = [+ _make_stream_chunk(+ [+ _make_part(+ function_call={+ "name": "search",+ "args": {"q": "a"},+ "id": None,+ },+ ),+ _make_part(+ function_call={+ "name": "search",+ "args": {"q": "b"},+ "id": None,+ },+ ),+ ],+ ),+ ]+ mock_client_cls.return_value.aio.models.generate_content_stream = (+ AsyncMock(return_value=_MockAsyncStream(chunks))+ )++ gen = await self.model([])+ responses = [r async for r in gen]++ final = responses[-1]+ self.assertTrue(final.is_last)+ self.assertEqual(len(final.content), 2)+ ids = [block.id for block in final.content]+ self.assertTrue(all(isinstance(i, str) and i for i in ids))+ self.assertNotEqual(ids[0], ids[1])+ self.assertEqual(+ [block.input for block in final.content],+ [+ json.dumps({"q": "a"}, ensure_ascii=False),+ json.dumps({"q": "b"}, ensure_ascii=False),+ ],+ )+# ---------------------------------------------------------------------------# _format_tools tests