How to use function calling with OpenAI API
Quick answer
Use the
functions parameter in client.chat.completions.create to define callable functions with JSON schemas. The model returns structured data invoking these functions, enabling precise control over output format and content.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
Define your function schema and call the OpenAI chat completion endpoint with the functions parameter. The model will respond with a function_call message containing arguments matching your schema.
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"]
}
}
]
messages = [
{"role": "user", "content": "What is the weather like in Boston?"}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
functions=functions,
function_call="auto" # or specify function name
)
message = response.choices[0].message
if message.get("function_call"):
function_name = message["function_call"]["name"]
arguments = message["function_call"]["arguments"]
print(f"Function to call: {function_name}")
print(f"Arguments: {arguments}")
else:
print(message.content) output
Function to call: get_current_weather
Arguments: {"location": "Boston"} Common variations
- Use
function_call="none"to disable function calling and get normal chat completions. - Specify
function_call="get_current_weather"to force calling a specific function. - Use async calls with
asyncioandawaitif your environment supports it. - Switch models like
gpt-4o-minifor faster, cheaper calls.
Troubleshooting
- If you get a
400 Bad Request, verify your function JSON schema is valid and matches OpenAI's specification. - If the model does not return a
function_call, check thatfunction_callparameter is set to"auto"or the function name. - Ensure your API key is correctly set in
os.environ["OPENAI_API_KEY"].
Key Takeaways
- Use the
functionsparameter to define callable functions with JSON schemas for structured outputs. - Set
function_callto control when and which function the model should invoke. - Parse the
function_callfield in the response to extract function name and arguments. - Validate your function schema to avoid request errors.
- You can combine function calling with normal chat messages for flexible workflows.