BadRequestError when submit request to google_search function
What happened?
Describe the bug in the latest version of autogen with autogenstudio. when requst the default question "What is the capital of France?" in deep research team in autogenstudio. got this error:
openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': "Unable to submit request because `google_search` functionDeclaration `parameters.country` schema didn't specify the schema type field. Learn more: https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling", 'status': 'INVALID_ARGUMENT'}}]
To Reproduce change the default model to gemini-2.0-flash, run the team in playground.
Which packages was the bug in?
Python Extensions (autogen-ext), Python AgentChat (autogen-agentchat>=0.4.0)
AutoGen library version.
0.6.4
Other library version.
No response
Model used
gemini-2.0-flash
Model provider
Google Gemini
Other model provider
No response
Python version
3.12
.NET version
None
Operating system
MacOS
The same issue affects the bing_search function as well:
openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': "Unable to submit request because `bing_search` functionDeclaration `parameters.country` schema didn't specify the schema type field. Learn more: https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling", 'status': 'INVALID_ARGUMENT'}}]
To reproduce Run the deep research team in AutoGen Studio using gemini-2.0-flash as the model and bing_search instead of the default google_search function.
@DingPengfei @federicovilla55
This seems to be an issue with using Gemini models with our OpenAI endpoint. This is likely becuase the country field has an Optional type that is not being parsed correctly.
- One thing you can do immediately is to remove the country field from the function definition and body.
- Investigate how types are parsed and handled for Optional function args vs what the Gemini api expects.
Can you try that and share results?
Happy to collaborate on on a PR/fix
@victordibia
By removing the optional fields (content_max_length, country) from the google_search function, the openai.BadRequestError no longer occurs. Similarly, replacing the optional types with a compatible standard JSON type (such as using str instead of Optional[str] for the country parameter) also prevents the error.
The issue is due to Gemini/Vertex AI requiring the function declaration to follow OpenAPI schema, as described in Vertex AI docs.
To reproduce the issue without using the AutoGen Studio UI, the following AutoGen AgentChat script can be used:
from autogenstudio.gallery.tools.google_search import google_search
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_core.tools import FunctionTool
from autogen_agentchat.ui import Console
import asyncio
import os
google_search_tool = FunctionTool(
google_search, description="Search Google for information, returns results with a snippet and body content"
)
async def main():
model_client = OpenAIChatCompletionClient(model="gemini-2.0-flash", api_key=os.getenv("GEMINI_API_KEY"))
search_agent = AssistantAgent(
name="Google_Search_Agent",
model_client=model_client,
tools=[google_search_tool],
description="Search Google for information.",
system_message="You are a helpful AI assistant. Solve tasks using your tools.",
)
stream = search_agent.run_stream(task="What is the capital of France? (Return only the capital city name.)")
await Console(stream)
await model_client.close()
asyncio.run(main())
Printing the parameters passed to the google_search tool in the above script, we get:
{'type': 'object', 'properties': {'query': {'description': 'query', 'title': 'Query', 'type': 'string'}, 'num_results': {'default': 3, 'description': 'num_results', 'title': 'Num Results', 'type': 'integer'}, 'include_snippets': {'default': True, 'description': 'include_snippets', 'title': 'Include Snippets', 'type': 'boolean'}, 'include_content': {'default': True, 'description': 'include_content', 'title': 'Include Content', 'type': 'boolean'}, 'content_max_length': {'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'default': 10000, 'description': 'content_max_length', 'title': 'Content Max Length'}, 'language': {'default': 'en', 'description': 'language', 'title': 'Language', 'type': 'string'}, 'country': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'description': 'country', 'title': 'Country'}, 'safe_search': {'default': True, 'description': 'safe_search', 'title': 'Safe Search', 'type': 'boolean'}}, 'required': ['query'], 'additionalProperties': False}
OpenAPI does not allow type: ["string", "null"] like standard JSON Schema does (docs reference), instead to make a parameter optional it should be used "nullable": true, such as country': {'default': None, 'description': 'country', 'title': 'Country', 'type': 'string', 'nullable': True}.
With this change, the issue no longer occurs too.