Gemini 允许在单次交互中组合使用 内置工具(例如 google_search)和 函数调用
(也称为 自定义工具),方法是保留并公开
工具调用的上下文历史记录。内置工具和自定义工具组合支持复杂的代理工作流,例如,模型可以在调用您的特定业务逻辑之前,根据实时网络数据进行自我定位。
以下示例展示了如何使用 google_search 和自定义函数 getWeather 启用内置工具和自定义工具组合:
Python
# This will only work for SDK newer than 2.0.0
from google import genai
client = genai.Client()
getWeather = {
"type": "function",
"name": "getWeather",
"description": "Gets the weather for a requested city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city and state, e.g. Utqiaġvik, Alaska",
},
},
"required": ["city"],
},
}
# The Interactions API manages context automatically across tool calls.
# The model will first use Google Search, then call getWeather.
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="What is the northernmost city in the United States? What's the weather like there today?",
tools=[
{"type": "google_search"},
getWeather,
],
)
# Process steps: the interaction contains search results and a function call
for step in interaction.steps:
if step.type == "function_call":
print(f"Function call: {step.name} with args: {step.arguments}")
# In a real application, you would execute the function here
# and provide the result back to the model.