Function calling

Function calling lets the model automatically select and invoke your predefined tool functions based on the user's needs, enabling capabilities such as data lookups, API calls, and task execution.

Basic concepts#

The complete function-calling flow:

  • Define tools — describe the available functions and parameters in the request
  • Model decision — the model decides whether a tool needs to be called
  • Return the call — the model returns the function name and arguments
  • Execute the function — you run the function and get the result
  • Continue the conversation — send the result back to the model to generate the final reply

OpenAI protocol#

Python

Code
from openai import OpenAI
import json
 
client = OpenAI(
    base_url="https://as.apinoma.com/v1",
    api_key="<your APINOMA_API_KEY>"
)
 
# 1. Define tools
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get real-time weather information for a specified city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "city name,such as Beijing or Shanghai"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "Temperature unit"
                }
            },
            "required": ["city"]
        }
    }
}]
 
# 2. Send the request
messages = [{"role": "user", "content": "What is the weather like in Beijing today?"}]
 
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)
 
message = response.choices[0].message
 
# 3. Handle the tool calls
if message.tool_calls:
    for tool_call in message.tool_calls:
        args = json.loads(tool_call.function.arguments)

        # 4. Execute your function
        result = get_weather(args["city"])  # your own implementation

        # 5. Send the result back to the model
        messages.append(message)
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result)
        })
 
    # Get the final reply
    final = client.chat.completions.create(
        model="openai/gpt-4o",
        messages=messages,
        tools=tools
    )
    print(final.choices[0].message.content)

TypeScript

Anthropic protocol#

Anthropic uses the `tools` parameter, with a slightly different format:

Code
import anthropic
 
client = anthropic.Anthropic(
    base_url="https://as.apinoma.com/anthropic",
    api_key="<your APINOMA_API_KEY>"
)
 
response = client.messages.create(
    model="anthropic/claude-sonnet-4.6",
    max_tokens=1024,
    tools=[{
        "name": "get_weather",
        "description": "Get real-time weather information for a specified city",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"}
            },
            "required": ["city"]
        }
    }],
    messages=[{"role": "user", "content": "What is the weather like in Beijing today?"}]
)
 
# Handle the tool_use content block
for block in response.content:
    if block.type == "tool_use":
        print(f"Calling tool: {block.name}, Parameter: {block.input}")

Parallel function calling#

The model can return multiple tool calls in a single response, and you should execute them in parallel:

Code
# The The model may request multiple tool calls at once
if message.tool_calls:
    # Execute all tool calls in parallel
    import asyncio
 
    async def execute_tools(tool_calls):
        tasks = []
        for tc in tool_calls:
            args = json.loads(tc.function.arguments)
            tasks.append(execute_function(tc.function.name, args))
        return await asyncio.gather(*tasks)

tool_choice parameter#

Value

Description

`"auto"`

The model decides whether to call a tool (default)

`"none"`

Disallow tool calls

`"required"`

Force a tool call

`{"type": "function", "function": {"name": "xxx"}}`

Force a call to the specified tool

Supported models#

The following models support function calling:

  • OpenAI: `gpt-4o`, `gpt-4o-mini`, `o1`, `o3-mini`
  • Anthropic: `claude-opus-4`, `claude-sonnet-4`, `claude-3-5-haiku`
  • Google: `gemini-pro-compatible-pro-preview`, `gemini-pro-compatible-flash-lite-preview`, `gemini-3-pro-preview`
  • Domestic: `deepseek-chat`, `qwen-max`, `glm-4`

Last updated on June 23, 2026