Switch language

Function Calling

Let models invoke external tools and APIs through function definitions.

Function Calling enables AI models to interact with external tools and APIs. You define functions the model can call, and it returns structured arguments for you to execute.

How it works

Define your tools with JSON Schema parameters in the tools array.
Send the request — the model may return a tool_calls response instead of text.
Execute the function locally with the returned arguments.
Send the function result back to the model for a final answer.

Complete example

1. Initialize the client

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api-inference.bitdeer.ai/v1",
    api_key="YOUR_API_KEY",
)

2. Define the function

def get_weather(location: str) -> str:
    """Simulate fetching weather data."""
    return json.dumps({"location": location, "temperature": "22°C", "condition": "sunny"})

3. Send the request with tools

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. 'San Francisco, CA'",
                    }
                },
                "required": ["location"],
            },
        },
    }
]

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

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=messages,
    tools=tools,
)

tool_call = response.choices[0].message.tool_calls[0]
print(tool_call.function.name)       # "get_weather"
print(tool_call.function.arguments)  # '{"location": "Tokyo"}'

4. Execute and send result back

messages.append(response.choices[0].message)

function_args = json.loads(tool_call.function.arguments)
result = get_weather(**function_args)

messages.append({
    "tool_call_id": tool_call.id,
    "role": "tool",
    "content": result,
})

final_response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=messages,
)

print(final_response.choices[0].message.content)
# "The weather in Tokyo is currently 22°C and sunny."

Tool schema reference

{
  "type": "function",
  "function": {
    "name": "function_name",
    "description": "What the function does",
    "parameters": {
      "type": "object",
      "properties": { ... },
      "required": ["param1"]
    },
    "strict": false
  }
}
FieldTypeDescription
namestringFunction name (a-z, A-Z, 0-9, underscores/dashes, max 64 chars)
descriptionstringHelps the model decide when to call this function
parametersobjectJSON Schema describing the function's parameters
strictbooleanWhen true, the model strictly follows the schema

Last updated on

On this page

Function Calling · Bitdeer AI