Skip to main content

Overview

Function Calling allows models to call functions you define, enabling interaction with external systems. The model does not execute functions directly — it returns the function name and arguments, and your code executes them and returns the results to the model.

Complete Example

import json
from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://crazyrouter.com/v1")

# 1. Define tools
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather information for a specified city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "City name, e.g.: New York"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature unit"
                    }
                },
                "required": ["city"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "search_products",
            "description": "Search for products",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string", "description": "Search keyword"},
                    "max_price": {"type": "number", "description": "Maximum price"}
                },
                "required": ["query"]
            }
        }
    }
]

# 2. Mock function implementations
def get_weather(city, unit="celsius"):
    return {"city": city, "temperature": 22, "unit": unit, "condition": "Sunny"}

def search_products(query, max_price=None):
    return [{"name": f"{query} Product A", "price": 99}, {"name": f"{query} Product B", "price": 199}]

# 3. Conversation loop
messages = [{"role": "user", "content": "What's the weather like in New York today? Also search for umbrellas"}]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

message = response.choices[0].message

# 4. Handle tool calls
if message.tool_calls:
    messages.append(message)

    for tool_call in message.tool_calls:
        func_name = tool_call.function.name
        args = json.loads(tool_call.function.arguments)

        if func_name == "get_weather":
            result = get_weather(**args)
        elif func_name == "search_products":
            result = search_products(**args)

        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result, ensure_ascii=False)
        })

    # 5. Get final response
    final = client.chat.completions.create(
        model="gpt-4o",
        messages=messages,
        tools=tools
    )
    print(final.choices[0].message.content)

Force a Specific Function Call

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "get_weather"}}
)

Supported Models

Function Calling is supported by the following models:
  • gpt-4o, gpt-4o-mini, gpt-4-turbo
  • claude-sonnet-4-20250514, claude-opus-4-20250514
  • gemini-2.5-pro, gemini-2.5-flash
Different models have varying levels of Function Calling support. GPT-4o supports parallel function calls, while some models may only support single calls.