Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(gemini): fix GeminiChatModel compatibility with Gemini-3-pro (#1206)
DavdGao6579dff68b47
diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.pyindex 814838b04..2c297fc65 100644--- a/src/agentscope/formatter/_gemini_formatter.py+++ b/src/agentscope/formatter/_gemini_formatter.py@@ -200,10 +200,11 @@ class GeminiChatFormatter(TruncatedFormatterBase):parts.append({"function_call": {- "id": block["id"],+ "id": None,"name": block["name"],"args": block["input"],},+ "thought_signature": block.get("id", None),},)diff --git a/src/agentscope/model/_gemini_model.py b/src/agentscope/model/_gemini_model.pyindex 635232aa0..6dfeddc8d 100644--- a/src/agentscope/model/_gemini_model.py+++ b/src/agentscope/model/_gemini_model.py@@ -1,10 +1,11 @@# -*- coding: utf-8 -*-# mypy: disable-error-code="dict-item""""The Google Gemini model in agentscope."""+import base64import copy-import jsonimport warningsfrom datetime import datetime+import jsonfrom typing import (AsyncGenerator,Any,@@ -333,39 +334,54 @@ class GeminiChatModel(ChatModelBase):text = ""thinking = ""+ tool_calls: list[ToolUseBlock] = []metadata: dict | None = Noneasync for chunk in response:- content_block: list = []-- # Thinking partsif (chunk.candidatesand chunk.candidates[0].contentand chunk.candidates[0].content.parts):for part in chunk.candidates[0].content.parts:- if part.thought and part.text:- thinking += part.text+ if part.text:+ if part.thought:+ thinking += part.text+ else:+ text += part.text++ if part.function_call:+ keyword_args = part.function_call.args or {}+ # .. note:: Gemini API always returns None for+ # function_call.id, so we use thought_signature+ # as the unique identifier for tool+ # calls when available. That maybe+ # infeasible someday, but Gemini+ # requires the thought_signature for some+ # llms like gemini-3-pro++ if part.thought_signature:+ call_id = base64.b64encode(+ part.thought_signature,+ ).decode("utf-8")+ else:+ call_id = part.function_call.id++ tool_calls.append(+ ToolUseBlock(+ type="tool_use",+ id=call_id,+ name=part.function_call.name,+ input=keyword_args,+ raw_input=json.dumps(+ keyword_args,+ ensure_ascii=False,+ ),+ ),+ )# Text parts- if chunk.text:- text += chunk.text- if structured_model:- metadata = _json_loads_with_repair(text)-- # Function calls- tool_calls = []- if chunk.function_calls:- for function_call in chunk.function_calls:- tool_calls.append(- ToolUseBlock(- type="tool_use",- id=function_call.id,- name=function_call.name,- input=function_call.args or {},- raw_input=json.dumps(function_call.args or {}),- ),- )+ if text and structured_model:+ metadata = _json_loads_with_repair(text)usage = Noneif chunk.usage_metadata:@@ -376,8 +392,11 @@ class GeminiChatModel(ChatModelBase):time=(datetime.now() - start_datetime).total_seconds(),)+ # The content blocks for the current chunk+ content_blocks: list = []+if thinking:- content_block.append(+ content_blocks.append(ThinkingBlock(type="thinking",thinking=thinking,@@ -385,21 +404,18 @@ class GeminiChatModel(ChatModelBase):)if text:- content_block.append(+ content_blocks.append(TextBlock(type="text",text=text,),)- content_block.extend(tool_calls)-- parsed_chunk = ChatResponse(- content=content_block,+ yield ChatResponse(+ content=content_blocks + tool_calls,usage=usage,metadata=metadata,)- yield parsed_chunkdef _parse_gemini_generation_response(self,@@ -429,6 +445,7 @@ class GeminiChatModel(ChatModelBase):"""content_blocks: List[TextBlock | ToolUseBlock | ThinkingBlock] = []metadata: dict | None = None+ tool_calls: list = []if (response.candidates@@ -436,35 +453,54 @@ class GeminiChatModel(ChatModelBase):and response.candidates[0].content.parts):for part in response.candidates[0].content.parts:- if part.thought and part.text:- content_blocks.append(- ThinkingBlock(- type="thinking",- thinking=part.text,+ if part.text:+ if part.thought:+ content_blocks.append(+ ThinkingBlock(+ type="thinking",+ thinking=part.text,+ ),+ )+ else:+ content_blocks.append(+ TextBlock(+ type="text",+ text=part.text,+ ),+ )++ if part.function_call:+ keyword_args = part.function_call.args or {}+ # .. note:: Gemini API always returns None for+ # function_call.id, so we use thought_signature+ # as the unique identifier for tool+ # calls when available. That maybe infeasible+ # someday, but Gemini requires the thought_signature+ # for some llms like gemini-3-pro++ if part.thought_signature:+ call_id = base64.b64encode(+ part.thought_signature,+ ).decode("utf-8")+ else:+ call_id = part.function_call.id++ tool_calls.append(+ ToolUseBlock(+ type="tool_use",+ id=call_id,+ name=part.function_call.name,+ input=keyword_args,+ raw_input=json.dumps(+ keyword_args,+ ensure_ascii=False,+ ),),)- if response.text:- content_blocks.append(- TextBlock(- type="text",- text=response.text,- ),- )- if structured_model:- metadata = _json_loads_with_repair(response.text)-- if response.function_calls:- for tool_call in response.function_calls:- content_blocks.append(- ToolUseBlock(- type="tool_use",- id=tool_call.id,- name=tool_call.name,- input=tool_call.args or {},- raw_input=json.dumps(tool_call.args or {}),- ),- )+ # For the structured output case+ if response.text and structured_model:+ metadata = _json_loads_with_repair(response.text)if response.usage_metadata:usage = ChatUsage(@@ -478,7 +514,7 @@ class GeminiChatModel(ChatModelBase):usage = Nonereturn ChatResponse(- content=content_blocks,+ content=content_blocks + tool_calls,usage=usage,metadata=metadata,)diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.pyindex cf0a27e72..bb4d49aae 100644--- a/tests/formatter_gemini_test.py+++ b/tests/formatter_gemini_test.py@@ -271,12 +271,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},@@ -353,12 +354,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},@@ -405,12 +407,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},@@ -490,12 +493,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},@@ -532,12 +536,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "2",+ "id": None,"name": "get_capital","args": {"country": "South Korea",},},+ "thought_signature": "2",},],},@@ -700,12 +705,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},@@ -922,12 +928,13 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):"parts": [{"function_call": {- "id": "1",+ "id": None,"name": "get_capital","args": {"country": "Japan",},},+ "thought_signature": "1",},],},diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.pyindex f62d63ffd..3ebcce594 100644--- a/tests/model_gemini_test.py+++ b/tests/model_gemini_test.py@@ -25,7 +25,30 @@ class GeminiResponseMock:self.usage_metadata = (self._create_usage_mock(usage_metadata) if usage_metadata else None)- self.candidates = candidates or []++ if candidates:+ # Use provided candidates structure+ self.candidates = candidates+ else:+ # Build default candidate structure+ part = Mock()+ part.text = text+ part.thought = False+ part.function_call = None++ first_candidate = Mock()+ first_candidate.content = Mock()+ first_candidate.content.parts = [part]++ for function_call in function_calls or []:+ part = Mock()+ part.text = None+ part.thought = False+ part.function_call = function_call+ part.thought_signature = None+ first_candidate.content.parts.append(part)++ self.candidates = [first_candidate]def _create_usage_mock(self, usage_data: dict) -> Mock:usage_mock = Mock()@@ -49,6 +72,8 @@ class GeminiPartMock:def __init__(self, text: str = "", thought: bool = False):self.text = textself.thought = thought+ self.function_call = None+ self.thought_signature = Noneclass GeminiCandidateMock:@@ -127,6 +152,7 @@ class TestGeminiChatModel(IsolatedAsyncioTestCase):call_args = mock_client.aio.models.generate_content.call_args[1]self.assertEqual(call_args["model"], "gemini-2.5-flash")self.assertEqual(call_args["contents"], messages)+ self.assertIn("config", call_args)self.assertIsInstance(result, ChatResponse)expected_content = [TextBlock(type="text", text="Hello! How can I help you?"),@@ -232,7 +258,11 @@ class TestGeminiChatModel(IsolatedAsyncioTestCase):text="Let me analyze this step by step...",thought=True,)- candidate = GeminiCandidateMock(parts=[thinking_part])+ text_part = GeminiPartMock(+ text="Here's my analysis",+ thought=False,+ )+ candidate = GeminiCandidateMock(parts=[thinking_part, text_part])mock_response = self._create_mock_response_with_thinking("Here's my analysis",candidates=[candidate],