UniterRouter
API ReferenceOpenAI Compatible

Function Calling

OpenAI Function Calling API - Enable GPT-4, GPT-3.5 to call external tools and APIs. Build AI agents with tool use, structured data extraction, and automated workflows.

Function Calling

Function calling allows models to generate structured arguments for predefined functions, enabling integration with external tools and APIs.

Supported Models: GPT-4o, GPT-4 Turbo, GPT-3.5 Turbo, Claude 3.5 Sonnet/Haiku, Gemini 2.0/1.5, and most modern LLMs.

How It Works

  1. Define functions with JSON Schema
  2. Send functions with your request
  3. Model returns function call with arguments
  4. Execute function and return result
  5. Model generates final response

Define Functions

{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get the current weather for a 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"]
        }
      }
    }
  ]
}

Example Request

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api.uniterouter.ai/v1",
    api_key="your_api_key",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name, e.g. Tokyo"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]

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

# Check if model wants to call a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    function_name = tool_call.function.name
    arguments = json.loads(tool_call.function.arguments)
    
    # Execute your function
    result = get_weather(arguments["location"])
    
    # Send result back
    messages.append(response.choices[0].message)
    messages.append({
        "role": "tool",
        "tool_call_id": tool_call.id,
        "content": json.dumps(result)
    })
    
    # Get final response
    final_response = client.chat.completions.create(
        model="gpt-4o",
        messages=messages,
        tools=tools
    )
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.uniterouter.ai/v1",
  apiKey: process.env.UNITEROUTER_API_KEY,
});

const tools: OpenAI.Chat.ChatCompletionTool[] = [
  {
    type: "function",
    function: {
      name: "get_weather",
      description: "Get the current weather for a location",
      parameters: {
        type: "object",
        properties: {
          location: {
            type: "string",
            description: "City name, e.g. Tokyo",
          },
        },
        required: ["location"],
      },
    },
  },
];

const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
  { role: "user", content: "What's the weather in Tokyo?" },
];

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages,
  tools,
  tool_choice: "auto",
});

if (response.choices[0].message.tool_calls) {
  const toolCall = response.choices[0].message.tool_calls[0];
  const args = JSON.parse(toolCall.function.arguments);
  
  // Execute function and get result
  const result = await getWeather(args.location);
  
  // Continue conversation with result
  messages.push(response.choices[0].message);
  messages.push({
    role: "tool",
    tool_call_id: toolCall.id,
    content: JSON.stringify(result),
  });
  
  const finalResponse = await client.chat.completions.create({
    model: "gpt-4o",
    messages,
    tools,
  });
}
curl https://api.uniterouter.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $UNITEROUTER_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "What is the weather in Tokyo?"}],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "Get current weather",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {"type": "string"}
            },
            "required": ["location"]
          }
        }
      }
    ],
    "tool_choice": "auto"
  }'

Tool Response Format

When the model calls a function:

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_abc123",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"location\": \"Tokyo\"}"
          }
        }
      ]
    },
    "finish_reason": "tool_calls"
  }]
}

Tool Choice Options

Control when the model uses tools:

# Let model decide
tool_choice="auto"

# Force a specific function
tool_choice={"type": "function", "function": {"name": "get_weather"}}

# Disable function calling
tool_choice="none"

# Require some function call
tool_choice="required"

Parallel Function Calls

Models can call multiple functions simultaneously:

tools = [
    {"type": "function", "function": {"name": "get_weather", ...}},
    {"type": "function", "function": {"name": "get_time", ...}}
]

# User: "What's the weather and time in Tokyo and New York?"
# Model may return multiple tool_calls

Handle parallel calls:

if response.choices[0].message.tool_calls:
    messages.append(response.choices[0].message)
    
    for tool_call in response.choices[0].message.tool_calls:
        result = execute_function(tool_call.function.name, 
                                  tool_call.function.arguments)
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result)
        })

Strict Mode

Enable strict schema validation (OpenAI models):

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "strict": True,  # Enforce schema
        "parameters": {...}
    }
}]

Best Practices

  1. Clear descriptions - Write detailed function and parameter descriptions
  2. Validate arguments - Always validate before executing
  3. Handle errors - Return error messages the model can understand
  4. Limit tools - Use only necessary functions (typically < 20)
  5. Use enums - Constrain values where possible

How is this guide?