Skip to content
  • There are no suggestions because the search field is empty.

Function Calling

Function calling enables models to connect with external tools and applications. Function calling is used to empower AI assistants with different capabilities and build deep integrations between applications and models.

Examples for Function Calling

  • Enable assistants to fetch data: aan AI assistant retrieves the latest customer data from an internal system when a user asks, What are my recent orders? before generating a response.
  • Enabling assistants to take actions: an AI assistant needs to schedule meetings based on user preferences and calendar availability.
  • Enabling assistants to perform computation: a math tutor assistant needs to perform a math computation.
  • Building rich workflows: a data extraction pipeline that fetches raw text, then converts it to structured data and saves it in a database.
  • Modifying your applications' UI: you can use function calls that update the UI based on user input, for example, rendering a pin on a map.

Learn more about the usage of function calling

Apply Function Calling

In Compass, Function Calling is supported with the Chat Completions API.

In this section, the example uses function such as get_current_weather and tells the model that it can call this function to retrieve weather information based on the input location. If the user asks a weather-related question, the model cannot respond due to a lack of information or direct access to weather data. Instead, it detects the available functions and generates the appropriate arguments for the function call. Hence, the model will build input parameters based on the user’s question and indicate the function to call. Once you call the function as per the model suggestion, the model responds to the user’s question regarding the weather.

To start using the Function Calling:

  1. Define the function using the tool parameters. See the API reference documentation.
  2. Based on the model response, call the particular functions.
  3. Send the information for each function call and function response to the model.

Using Function Calling with the Chat Completion API

from openai import OpenAI
import json
client = OpenAI(
    base_url = "https://api.core42.ai/v1",
    default_headers={"api-key": "<API_KEY>"},
    api_key="XXX"
)
# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})
def run_conversation():
    # Step 1: send the conversation and available functions to the model
    messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
    tools = [
        {
            "type": "function",
            "function": {
                "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="gpt-4o",
        messages=messages,
        tools=tools,
        tool_choice="auto",  # auto is default, but we'll be explicit
    )
    response_message = response.choices[0].message
    tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
    if tool_calls:
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_current_weather": get_current_weather,
        }  # only one function in this example, but you can have multiple
        messages.append(response_message)  # extend conversation with assistant's reply
        # Step 4: send the info for each function call and function response to the model
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(
                location=function_args.get("location"),
                unit=function_args.get("unit"),
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        second_response = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
        )  # get a new response from the model where it can see the function response
        return second_response

print(run_conversation())

Using Function Calling with the Response API

The Response API supports both single and parallel Function Calling. Below are the examples showing how to use Function Calling with the Response API.

Single Tool/Function

The models supporting single function calling are GPT-4o, GPT-4o mini, GPT-4.1, o3, o3-mini, and o4-mini.

The example below shows how to send a POST request to the Responses API, defining a single tool named get-weather using the gpt-4o model.

Sample Request

curl --location 'https://api.core42.ai/v1/responses?api-version=preview' \
--header 'Content-Type: application/json' \
--header 'api-key: api-key' \
--data '{ 
    "model": "gpt-4o",
    "tools": [  
        {  
            "type": "function",  
            "name": "get_weather",  
            "description": "Get the weather for a location",  
            "parameters": {  
                "type": "object",  
                "properties": {  
                    "location": {"type": "string"}
                },  
                "required": ["location"] 
            }
        }  
    ],
    "input": "what is weather in San Fransico?"
}'

The response confirms that the model recognized the need to call the function instead of generating a text answer directly.

Sample Response

{
    "id": "resp_090d0fcd6d671e4500690b7f67c7908190a50bd9b1680ff623",
    "object": "response",
    "created_at": 1762361191,
    "status": "completed",
    "background": false,
    "content_filters": null,
    "error": null,
    "incomplete_details": null,
    "instructions": null,
    "max_output_tokens": null,
    "max_tool_calls": null,
    "model": "gpt-4o",
    "output": [
        {
            "id": "fc_090d0fcd6d671e4500690b7f6831048190b3d0cc50eb8f03a7",
            "type": "function_call",
            "status": "completed",
            "arguments": "{\"location\":\"San Francisco\"}",
            "call_id": "call_UH9mNcr7m7VKaYbdjyZibKgc",
            "name": "get_weather"
        }
    ],
    "parallel_tool_calls": true,
    "previous_response_id": null,
    "prompt_cache_key": null,
    "reasoning": {
        "effort": null,
        "summary": null
    },
    "safety_identifier": null,
    "service_tier": "default",
    "store": true,
    "temperature": 1.0,
    "text": {
        "format": {
            "type": "text"
        },
        "verbosity": "medium"
    },
    "tool_choice": "auto",
    "tools": [
        {
            "type": "function",
            "description": "Get the weather for a location",
            "name": "get_weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string"
                    }
                },
                "required": [
                    "location"
                ],
                "additionalProperties": false
            },
            "strict": true
        }
    ],
    "top_logprobs": 0,
    "top_p": 1.0,
    "truncation": "disabled",
    "usage": {
        "input_tokens": 46,
        "input_tokens_details": {
            "cached_tokens": 0
        },
        "output_tokens": 16,
        "output_tokens_details": {
            "reasoning_tokens": 0
        },
        "total_tokens": 62
    },
    "user": null,
    "metadata": {}
}

Parallel Function Calling with Multiple Functions

Parallel function calls allows the model to trigger multiple functions in a single response. This reduces the number of API requests and improves the performance.

For example, a time-and-weather assistant can request:

  • current weather in two cities

  • current time in two cities

in one single API request.

The models supporting parallel function calling are o3, o3-mini, o4-mini, GPT-4o, GPT-4o mini, and GPT-4.1.

Request

This request demonstrates how to define multiple tools (functions) within a single call to the Responses API (using model gpt-4o).
The model can intelligently decide which function(s) to call based on the question — in this case, asking for both the weather and the current time in multiple cities.

{
    "model": "gpt-4o",
    "tools":
    [
        {
            "name": "get_current_weather",
            "type": "function",
            "description": "Get the current weather in a given location",
            "parameters":
            {
                "type": "object",
                "properties":
                {
                    "location":
                    {
                        "type": "string",
                        "description": "The city name, e.g. San Francisco"
                    },
                    "unit":
                    {
                        "type": "string",
                        "enum":
                        [
                            "celsius",
                            "fahrenheit"
                        ]
                    }
                },
                "required":
                [
                    "location"
                ]
            }
        },
        {
            "name": "get_current_time",
            "type": "function",
            "description": "Get the current time in a given location",
            "parameters":
            {
                "type": "object",
                "properties":
                {
                    "location":
                    {
                        "type": "string",
                        "description": "The city name, e.g. San Francisco"
                    }
                },
                "required":
                [
                    "location"
                ]
            }
        }
    ],
    "input":
    [
        {
            "role": "user",
            "content": "What's the weather and current time in Paris and London?"
        }
    ]
}

Response:

This response shows how the Responses API can invoke multiple tools (functions) within a single response.
The model was prompted in a way that required both weather and time information for two cities: Paris and London.

{
    "id": "resp_0ee47e3631a858a200690b834a952881968a8c67da0d77f854",
    "object": "response",
    "created_at": 1762362186,
    "status": "completed",
    "background": false,
    "content_filters": null,
    "error": null,
    "incomplete_details": null,
    "instructions": null,
    "max_output_tokens": null,
    "max_tool_calls": null,
    "model": "gpt-4o",
    "output": [
        {
            "id": "fc_0ee47e3631a858a200690b834b4dfc8196baef3a57ca94bd00",
            "type": "function_call", 
            "status": "completed",
            "arguments": "{\"location\":\"Paris\",\"unit\":\"celsius\"}",
            "call_id": "call_ZeoyNAeUnAqwdYn8CHpPVmTN",
            "name": "get_current_weather"
        },
        {
            "id": "fc_0ee47e3631a858a200690b834b59b88196bba56b37a2e73c1d",
            "type": "function_call",
            "status": "completed",
            "arguments": "{\"location\":\"Paris\"}",
            "call_id": "call_XYWULm96yXK1Bz3NsWBUogo6",
            "name": "get_current_time"
        },
        {
            "id": "fc_0ee47e3631a858a200690b834b79488196916460359e394f23",
            "type": "function_call",
            "status": "completed",
            "arguments": "{\"location\":\"London\",\"unit\":\"celsius\"}",
            "call_id": "call_gsOKYs7WXUVKFQLuqZUxvxo5",
            "name": "get_current_weather"
        },
        {
            "id": "fc_0ee47e3631a858a200690b834b8c388196916b595023ddb1bf",
            "type": "function_call",
            "status": "completed",
            "arguments": "{\"location\":\"London\"}",
            "call_id": "call_cktwSBTiUfOfcU7unaQd53L7",
            "name": "get_current_time"
        }
    ],
    "parallel_tool_calls": true,
    "previous_response_id": null,
    "prompt_cache_key": null,
    "reasoning": {
        "effort": null,
        "summary": null
    },
    "safety_identifier": null,
    "service_tier": "default",
    "store": true,
    "temperature": 1.0,
    "text": {
        "format": {
            "type": "text"
        },
        "verbosity": "medium"
    },
    "tool_choice": "auto",
    "tools": [
        {
            "type": "function",
            "description": "Get the current weather in a given location",
            "name": "get_current_weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city name, e.g. San Francisco"
                    },
                    "unit": {
                        "type": "string",
                        "enum": [
                            "celsius",
                            "fahrenheit"
                        ]
                    }
                },
                "required": [
                    "location",
                    "unit"
                ],
                "additionalProperties": false
            },
            "strict": true
        },
        {
            "type": "function",
            "description": "Get the current time in a given location",
            "name": "get_current_time",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city name, e.g. San Francisco"
                    }
                },
                "required": [
                    "location"
                ],
                "additionalProperties": false
            },
            "strict": true
        }
    ],
    "top_logprobs": 0,
    "top_p": 1.0,
    "truncation": "disabled",
    "usage": {
        "input_tokens": 27,
        "input_tokens_details": {
            "cached_tokens": 0
        },
        "output_tokens": 84,
        "output_tokens_details": {
            "reasoning_tokens": 0
        },
        "total_tokens": 111
    },
    "user": null,
    "metadata": {}
}