Function Calling REST
Copyright 2025 Google LLC.
This notebook provides quick code examples that show you how to get started with function calling using curl.
You can run this in Google Colab, or you can copy/paste the curl commands into your terminal.
To run this notebook, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you are running in a different environment, you can store your key in an environment variable. See Authentication to learn more.
How function calling works
Function calling lets developers create a description of a function in their code, then pass that description to a language model in a request. The response from the model includes the name of a function that matches the description and the arguments to call it with. Function calling lets you use functions as tools in generative AI applications, and you can define more than one function within a single request. Function calling returns JSON with the name of a function and the arguments to use in your code.
Functions are described using function declarations. After you pass a list of function declarations in a query to a language model, the model returns an object in an OpenAPI compatible schema format that includes the names of functions and their arguments and tries to answer the user query with one of the returned functions. The language model understands the purpose of a function by analyzing its function declaration. The model doesn't actually call the function. Instead, a developer uses the OpenAPI compatible schema object in the response to call the function that the model returns.
When you implement function calling, you create one or more function
declarations, then add the function declarations to a tools object that's
passed to the model. Each function declaration contains information about one
function that includes the following:
- Function name
- Function parameters in an OpenAPI compatible schema format. A select subset (add a link) is supported. When using curl, the schema is specified using JSON.
- Function description (optional). For the best results, it is recommended that you include a description.
This notebook includes curl examples that make REST calls with the
GenerativeModel class and its methods.
Supported models
All the Gemini models support function calling.
Function calling cURL samples
When you use cURL, the function and parameter information is included in the
tools element. Each function declaration in the tools element contains the
function name, its parameters specified using the
OpenAPI compatible schema, and
a function description. The following samples demonstrate how to use curl
commands with function calling:
Single-turn curl sample
Single-turn is when you call the language model one time. With function calling, a single-turn use case might be when you provide the model a natural language query and a list of functions. In this case, the model uses the function declaration, which includes the function name, parameters, and description, to predict which function to call and the arguments to call it with.
The following curl sample is an example of passing in a description of a
function that returns information about where a movie is playing. Several
function declarations are included in the request, such as find_movies and
find_theaters.
{
"candidates": [
{
"content": {
"parts": [
{
"functionCall": {
"name": "find_theaters",
"args": {
"movie": "Barbie",
"location": "Mountain View, CA"
}
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
],
"promptFeedback": {
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
}
Multi-turn curl examples
You can implement a multi-turn function calling scenario by doing the following:
- Get a function call response by calling the language model. This is the first turn.
- Call the language model using the function call response from the first turn and the function response you get from calling that function. This is the second turn.
The response from the second turn either summarizes the results to answer your query in the first turn, or contains a second function call you can use to get more information for your query.
This topic includes two multi-turn curl examples:
- Curl example that uses a function response from a previous turn
- Curl example that calls a language model multiple times
Curl example that uses a response from a previous turn
The following curl sample calls the function and arguments returned by the previous single-turn example to get a response. The method and parameters returned by the single-turn example are in this JSON.
"functionCall": {
"name": "find_theaters",
"args": {
"movie": "Barbie",
"location": "Mountain View, CA"
}
}
{
"candidates": [
{
"content": {
"parts": [
{
"text": "OK. I found two theaters in Mountain View that are showing the Barbie movie: AMC Mountain View 16 and Regal Edwards 14."
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
]
}
Curl example that calls a language model multiple times
The following curl example calls the language model multiple times to call a function. Each time the model calls the function, it can use a different function to answer a different user query in the request.
{
"candidates": [
{
"content": {
"parts": [
{
"functionCall": {
"name": "find_movies",
"args": {
"location": "Mountain View, CA",
"description": "comedy"
}
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
}
]
}
],
"promptFeedback": {
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
}