How to use function calling with Gemini API
Quick answer
Use the
OpenAI SDK v1 to call the Gemini API with function_call parameters in chat.completions.create. Define your functions schema and pass it in the request to receive structured function call responses from the gemini-2.5-pro model.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable.
pip install openai>=1.0 Step by step
This example shows how to define a function schema and invoke the Gemini API with function calling enabled to get structured outputs.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[{"role": "user", "content": "What is the weather like in New York?"}],
functions=functions,
function_call="auto" # Let the model decide to call the function
)
print("Model response:")
print(response.choices[0].message.content)
print("Function call details:")
print(response.choices[0].message.function_call) output
Model response:
{"location": "New York", "unit": "fahrenheit"}
Function call details:
{'name': 'get_current_weather', 'arguments': '{"location": "New York", "unit": "fahrenheit"}'} Common variations
- Use
function_call="none"to disable function calling. - Specify
function_call={"name": "function_name"}to force a specific function call. - Use other Gemini models like
gemini-2.0-flashfor faster responses. - Implement async calls with
asyncioandawaitin Python 3.8+.
Troubleshooting
- If you get a
400 Bad Request, verify your function schema is valid JSON Schema. - If the function call is missing, ensure
functionsandfunction_callparameters are correctly passed. - Check your API key and environment variable
OPENAI_API_KEYare set properly.
Key Takeaways
- Use the OpenAI SDK v1 with
functionsandfunction_callparameters to enable Gemini API function calling. - Define your function schema using JSON Schema to get structured, machine-readable outputs.
- You can let the model decide which function to call or force a specific function call.
- Always validate your function schema and environment setup to avoid request errors.