@@ -49,15 +49,14 @@ def _format_tools(self, tools: List[Union[Callable, Dict]], tool_choice: str = N
4949 """
5050 Format the tools from a generic BaseModel to the Gemini format.
5151 """
52- tools_formatted = format_tools (tools , mode = Mode .GEMINI_TOOLS )
53- tool_choice = self ._check_tool_choice (tools_formatted , tool_choice )
52+ tools_formatted , tool_choice = format_tools (tools , tool_choice , mode = Mode .GEMINI_TOOLS )
5453
5554 tools_formatted = [
5655 Tool (function_declarations = [tool ]) for tool in tools_formatted
5756 ]
5857
5958 # If the tool_choice is a named tool, then apply correct formatting
60- if tool_choice in [tool [ "title" ] for tool in tools ]:
59+ if tool_choice in [tool . function_declarations [ 0 ]. name for tool in tools_formatted ]:
6160 tool_choice = ToolConfig (
6261 function_calling_config = FunctionCallingConfig (
6362 mode = "ANY" , allowed_function_names = [tool_choice ]
@@ -66,7 +65,7 @@ def _format_tools(self, tools: List[Union[Callable, Dict]], tool_choice: str = N
6665 elif tool_choice == "required" :
6766 tool_choice = ToolConfig (
6867 function_calling_config = FunctionCallingConfig (
69- mode = "ANY" , allowed_function_names = [tool [ "title" ] for tool in tools ]
68+ mode = "ANY" , allowed_function_names = [tool . function_declarations [ 0 ]. name for tool in tools_formatted ]
7069 )
7170 )
7271 elif tool_choice == "auto" :
@@ -232,18 +231,20 @@ def _prepare_messages(
232231 """
233232 def format_content (message : Message ):
234233 parts = []
235- tool_calls = []
236- tool_results = []
237234 if isinstance (message .content , str ):
238235 parts = [Part (text = message .content )]
239236 else :
240237 for content in message .content :
241238 if content .type == "text" :
242239 parts .append (Part (text = content .text ))
243240 elif content .type == "tool_use" :
244- tool_calls .append (Part (function_call = FunctionCall (id = content .tool_use .id , name = content .tool_use .name , args = content .tool_use .args )))
241+ parts .append (Part (function_call = FunctionCall (id = content .tool_use .id , name = content .tool_use .name , args = content .tool_use .args )))
245242 elif content .type == "tool_result" :
246- tool_results .append (Part (function_response = FunctionResponse (id = content .tool_result .id , name = content .tool_result .name , response = content .tool_result .result )))
243+ result = content .tool_result .result
244+ if isinstance (result , str ):
245+ result = {"content" : result }
246+ parts .append (Part (function_response = FunctionResponse (id = content .tool_result .id , name = content .tool_result .name , response = result )))
247+
247248 return parts
248249
249250 messages = [Message (** m ) if isinstance (m , Dict ) else m for m in messages ]
0 commit comments