Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(formatter): send failed gemini tool results under the error key (#3947)
Yanze (David) Wudb780a4c341c
diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.pyindex 7aaac8d6a..f1d05bd92 100644--- a/src/agentscope/formatter/_gemini_formatter.py+++ b/src/agentscope/formatter/_gemini_formatter.py@@ -20,6 +20,7 @@ from ..message import (DataBlock,ToolCallBlock,ToolResultBlock,+ ToolResultState,URLSource,Base64Source,)@@ -279,13 +280,20 @@ class GeminiChatFormatter(_GeminiFormatterBase):multimodal_data,) = self.convert_tool_result_to_string(block.output)+ # Gemini reads a failed call from the "error" key of the+ # response, and a successful one from "output".+ response_key = (+ "error"+ if block.state == ToolResultState.ERROR+ else "output"+ )tool_result_parts.append({"function_response": {"id": block.id,"name": block.name,"response": {- "output": textual_output,+ response_key: textual_output,},},},diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.pyindex b2a9b05a6..3169bc106 100644--- a/tests/formatter_gemini_test.py+++ b/tests/formatter_gemini_test.py@@ -1232,6 +1232,89 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):][0]["function_call"]self.assertIsInstance(function_call["args"], dict)+ async def test_chat_formatter_failed_tool_result_uses_error_key(+ self,+ ) -> None:+ """A failed tool call reaches Gemini under the ``error`` key of the+ function response, a successful one under ``output``."""+ res = await GeminiChatFormatter().format(+ [+ AssistantMsg(+ name="assistant",+ content=[+ ToolCallBlock(+ id="call_1",+ name="get_weather",+ input='{"city": "Paris"}',+ ),+ ToolCallBlock(+ id="call_2",+ name="get_weather",+ input='{"city": "Atlantis"}',+ ),+ ToolResultBlock(+ id="call_1",+ name="get_weather",+ output=[TextBlock(text="sunny")],+ state=ToolResultState.SUCCESS,+ ),+ ToolResultBlock(+ id="call_2",+ name="get_weather",+ output=[TextBlock(text="Unknown city: Atlantis")],+ state=ToolResultState.ERROR,+ ),+ ],+ ),+ ],+ )++ self.assertListEqual(+ res,+ [+ {+ "role": "model",+ "parts": [+ {+ "function_call": {+ "id": "call_1",+ "name": "get_weather",+ "args": {"city": "Paris"},+ },+ },+ {+ "function_call": {+ "id": "call_2",+ "name": "get_weather",+ "args": {"city": "Atlantis"},+ },+ },+ ],+ },+ {+ "role": "user",+ "parts": [+ {+ "function_response": {+ "id": "call_1",+ "name": "get_weather",+ "response": {"output": "sunny"},+ },+ },+ {+ "function_response": {+ "id": "call_2",+ "name": "get_weather",+ "response": {+ "error": "Unknown city: Atlantis",+ },+ },+ },+ ],+ },+ ],+ )+class GeminiMultiAgentSystemMessageTest(IsolatedAsyncioTestCase):"""A system message must never emit a ``text: None`` part."""