How to use function calling in OpenAI API
Quick answer
Use the
functions parameter in client.chat.completions.create to define callable functions and enable the model to invoke them. The response includes a function_call object specifying which function to call and with what arguments.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable.
- Run
pip install openaito install the SDK. - Set your API key in your environment:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows).
pip install openai Step by step
Define your functions schema and call the OpenAI chat completion endpoint with the functions parameter. The model will respond with a function_call indicating which function to invoke and its arguments.
import os
from openai import OpenAI
import json
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"]
}
}
]
messages = [
{"role": "user", "content": "What is the weather like in Boston?"}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
functions=functions,
function_call="auto" # let the model decide to call or not
)
message = response.choices[0].message
if message.get("function_call"):
function_name = message["function_call"]["name"]
arguments = json.loads(message["function_call"]["arguments"])
print(f"Function to call: {function_name}")
print(f"Arguments: {arguments}")
else:
print("Model response:", message["content"]) output
Function to call: get_current_weather
Arguments: {'location': 'Boston'} Common variations
You can specify function_call as "none" to disable function calling or provide a specific function name to force call. Async usage is supported with async methods. Different models like gpt-4o or gpt-4o-mini support function calling similarly.
import asyncio
async def async_function_call():
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me the weather in Seattle."}],
functions=functions,
function_call="auto"
)
message = response.choices[0].message
print(message)
# Run async example
# asyncio.run(async_function_call()) Troubleshooting
- If you get a
400 Bad Request, verify yourfunctionsJSON schema is valid. - If the model never calls functions, ensure
function_callis set to"auto"or the specific function name. - Check your API key environment variable is set correctly to avoid authentication errors.
Key Takeaways
- Use the
functionsparameter to define callable functions with JSON schema. - Set
function_callto "auto" to let the model decide when to call functions. - Parse the
function_callfield in the response to get function name and arguments. - Async calls and different models support function calling similarly.
- Validate your function schema and environment variables to avoid common errors.