Concept Intermediate · 3 min read

What is function calling in OpenAI

Quick answer
Function calling in OpenAI is a feature that allows chat models like gpt-4o to invoke predefined functions by returning structured JSON data. This enables seamless integration of AI with external APIs or code by specifying function signatures and receiving precise function call arguments.
Function calling in OpenAI is a feature that enables chat models to call predefined functions by returning structured data, facilitating automated and reliable API interactions.

How it works

Function calling works by letting you define functions with names, descriptions, and parameter schemas that the model can "call" during chat completion. Instead of returning plain text, the model returns a JSON object specifying the function name and arguments. This is like giving the AI a phonebook of functions it can dial, and it chooses which to call based on the conversation context.

When the model decides to call a function, your application receives the structured call data and can execute the corresponding function locally or remotely, then feed the result back to the model if needed.

Concrete example

Here is a Python example using the OpenAI SDK v1+ to define a function and handle a function call response:

python
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": "City and state, e.g. San Francisco, CA"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location"]
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather like in New York?"}],
    functions=functions,
    function_call="auto"  # let model decide to call function
)

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": "New York", "unit": "fahrenheit"}

When to use it

Use function calling when you want your AI to interact with external APIs, databases, or backend logic in a structured and reliable way. It is ideal for tasks like fetching real-time data, performing calculations, or triggering workflows based on user input.

Avoid function calling if your use case only requires free-form text generation or when you do not need structured outputs or integration with external systems.

Key terms

TermDefinition
Function callingA feature where the model returns a JSON object to invoke predefined functions.
Function schemaA JSON schema defining function name, description, and parameters for the model.
Function callThe JSON response from the model specifying which function to call and with what arguments.
ParametersThe structured inputs required by a function, defined in the schema.

Key Takeaways

  • Function calling enables chat models to return structured JSON to invoke external functions.
  • Define functions with JSON schemas to guide the model on available callable actions.
  • Use function calling to integrate AI with APIs and backend logic for reliable automation.
Verified 2026-04 · gpt-4o
Verify ↗