I need acurlexample of the Gemini API for tool calling which shows how a tool response…
- User
I need acurlexample of the Gemini API for tool calling which shows how a tool response is sent back to the model
- Assistant
I need acurlexample of the Gemini API for tool calling which shows how a tool response is sent back to the model
Overview
Below is a two-stepcurlworkflow that demonstrates how to use the Gemini API’s RESTgenerateContentendpoint for function (tool) calling. You’ll first send a request with your function declaration and user prompt, then, after executing the function in your application, send the function’s result back to the model so it can generate a final human-readable response.
First, define your function in the request payload and ask the model to call it when appropriate.
Endpoint
Use the public endpoint for Gemini:
────────────────────────────────────────────────────────────────────
bashCopy
whereMODEL_IDmight begemini-2.0-flashor another supported modelGoogle AI for Developers.
cURL Example
────────────────────────────────────────────────────────────────────
bashCopycurl -X POST
-H "Content-Type: application/json"
-d '{
"tools": [
{
"function_declarations": [
{
"name": "set_light_values",
"description": "Sets the brightness and color temperature of a light.",
"parameters": {
"type": "object",
"properties": {
"brightness": {
"type": "integer",
"description": "Light level from 0 to 100."
},
"color_temp": {
"type": "string",
"enum": ["daylight", "cool", "warm"],
"description": "Color temperature: daylight, cool, or warm."
}
},
"required": ["brightness", "color_temp"]
}
}
]
}
],
"contents": [
{
"role": "user",
"parts": [
{ "text": "Turn the lights down to a romantic level." }
]
}
],
"tool_config": {
"function_calling_config": { "mode": "AUTO" }
}
}'
Notes:
Thetoolsarray holds your function declarations in OpenAPI-style JSON schemasGoogle Cloud.
tool_ction_calling_config.modeset to"AUTO"lets the model decide when to call your function; you can also force calls with mode"ANY"Google Cloud.
Thetoolsarray holds your function declarations in OpenAPI-style JSON schemasGoogle Cloud.
tool_ction_calling_config.modeset to"AUTO"lets the model decide when to call your function; you can also force calls with mode"ANY"Google Cloud.
The response you receive will include afunctionCallobject indicating which function to invoke and with what arguments. For example:
────────────────────────────────────────────────────────────────────
jsonCopy{ "candidates": [ { "content": { "parts": [ { "functionCall": { "name": "set_light_values", "args": { "brightness": 25, "color_temp": "warm" } } } ] } } ] }
Here, the model suggests callingset_light_values(brightness=25, color_temp="warm")Google AI for Developers. Execute this function in your code and capture its result.
Once you’ve executedset_light_valuesin your application and obtained a result (e.g.,{"brightness":25,"colorTemperature":"warm"}), send a follow-up request that includes:
The original user message.
The model’s function call message.
Your function’s response as a special message.
The original user message.
The model’s function call message.
Your function’s response as a special message.
cURL Example
────────────────────────────────────────────────────────────────────
bashCopycurl -X POST
-H "Content-Type: application/json"
-d '{
"tools": [ /* same function_declarations as above */ ],
"contents": [
{
"role": "user",
"parts": [{ "text": "Turn the lights down to a romantic level." }]
},
{
"role": "assistant",
"parts": [
{
"functionCall": {
"name": "set_light_values",
"args": { "brightness": 25, "color_temp": "warm" }
}
}
]
},
{
"role": "user",
"parts": [
{
"functionResponse": {
"name": "set_light_values",
"response": {
"brightness": 25,
"colorTemperature": "warm"
}
}
}
]
}
]
}'
In this payload:
The second element re-plays the model’s function call (role: "assistant"with afunctionCall)Google AI for Developers.
The third element carries your function’s actual output back into the conversation (functionResponse) so the model can craft a final answerGoogle AI for DevelopersGoogle AI for Developers.
The second element re-plays the model’s function call (role: "assistant"with afunctionCall)Google AI for Developers.
The third element carries your function’s actual output back into the conversation (functionResponse) so the model can craft a final answerGoogle AI for DevelopersGoogle AI for Developers.
With this two-step pattern—first submitting the function declarations and prompt, then sending back your function’s result—you enable structured, tool-backed interactions with the Gemini API via simplecurlcommands.
Not yet included in a published release.