> For the complete documentation index, see [llms.txt](https://docs.tensorx.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorx.ai/api-reference/function-calling.md).

# Function Calling

Let AI models call your functions to take actions and retrieve information.

***

## Overview

Function calling (also called "tools") allows models to:

* Request specific data from your systems
* Trigger actions in your application
* Access real-time information
* Perform calculations

The model doesn't execute functions directly—it returns a structured request for your code to handle.

***

## Supported Models

| Model                         | Function Calling |
| ----------------------------- | ---------------- |
| `z-ai/glm-5.1`                | ✅                |
| `minimax/minimax-m2.5`        | ✅                |
| `moonshotai/kimi-k2.5`        | ✅                |
| `deepseek/deepseek-chat-v3.1` | ✅                |
| `deepseek/deepseek-r1-0528`   | ✅                |
| `minimax/minimax-m2`          | ✅                |
| `qwen/qwen3-235b-a22b-2507`   | ✅                |
| `openai/gpt-oss-120b`         | ✅                |

***

## Basic Example

Here's a weather function the model can call:

{% tabs %}
{% tab title="Python" %}

```python
from openai import OpenAI
import json

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

# Define your 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. London, UK"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature unit"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

# Make the request
response = client.chat.completions.create(
    model="deepseek/deepseek-chat-v3.1",
    messages=[
        {"role": "user", "content": "What's the weather in Tokyo?"}
    ],
    tools=tools
)

# Check if model wants to call a function
message = response.choices[0].message
if message.tool_calls:
    tool_call = message.tool_calls[0]
    print(f"Function: {tool_call.function.name}")
    print(f"Arguments: {tool_call.function.arguments}")
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://api.tensorx.ai/v1'
});

const 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. London, UK'
          },
          unit: {
            type: 'string',
            enum: ['celsius', 'fahrenheit'],
            description: 'Temperature unit'
          }
        },
        required: ['location']
      }
    }
  }
];

const response = await client.chat.completions.create({
  model: 'deepseek/deepseek-chat-v3.1',
  messages: [
    { role: 'user', content: "What's the weather in Tokyo?" }
  ],
  tools
});

const message = response.choices[0].message;
if (message.tool_calls) {
  const toolCall = message.tool_calls[0];
  console.log(`Function: ${toolCall.function.name}`);
  console.log(`Arguments: ${toolCall.function.arguments}`);
}
```

{% endtab %}

{% tab title="curl" %}

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "deepseek/deepseek-chat-v3.1",
    "messages": [
      {"role": "user", "content": "What'\''s the weather in Tokyo?"}
    ],
    "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. London, UK"
              }
            },
            "required": ["location"]
          }
        }
      }
    ]
  }'
```

{% endtab %}
{% endtabs %}

***

## Response Format

When the model decides to call a function:

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

Key fields:

* `tool_calls` - Array of functions the model wants to call
* `id` - Unique ID for this tool call (use when sending results back)
* `arguments` - JSON string of function parameters
* `finish_reason: "tool_calls"` - Indicates model is waiting for results

***

## Complete Flow

A full function calling conversation:

```python
import json

# Step 1: Send user message with tools
response = client.chat.completions.create(
    model="deepseek/deepseek-chat-v3.1",
    messages=[
        {"role": "user", "content": "What's the weather in Paris?"}
    ],
    tools=tools
)

message = response.choices[0].message

# Step 2: Check if model wants to call functions
if message.tool_calls:
    # Step 3: Execute the function(s)
    tool_call = message.tool_calls[0]
    function_name = tool_call.function.name
    arguments = json.loads(tool_call.function.arguments)
    
    # Your actual function implementation
    if function_name == "get_weather":
        result = get_weather(arguments["location"])  # Your code
    
    # Step 4: Send the result back
    response = client.chat.completions.create(
        model="deepseek/deepseek-chat-v3.1",
        messages=[
            {"role": "user", "content": "What's the weather in Paris?"},
            message,  # Include the assistant's tool_calls message
            {
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": json.dumps(result)
            }
        ],
        tools=tools
    )
    
    # Step 5: Get the final response
    print(response.choices[0].message.content)
```

***

## Tool Choice Options

Control when the model uses functions:

```python
# Let model decide (default)
tool_choice="auto"

# Never call functions
tool_choice="none"

# Must call a function
tool_choice="required"

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

***

## Parallel Tool Calls

Models can request multiple functions at once:

```json
{
  "tool_calls": [
    {"id": "call_1", "function": {"name": "get_weather", "arguments": "{\"location\":\"Paris\"}"}},
    {"id": "call_2", "function": {"name": "get_weather", "arguments": "{\"location\":\"London\"}"}}
  ]
}
```

Handle all calls and return all results:

```python
messages = [original_user_message, assistant_message]

for tool_call in 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)
    })

response = client.chat.completions.create(
    model="deepseek/deepseek-chat-v3.1",
    messages=messages,
    tools=tools
)
```

***

## Common Tool Examples

### Web Search

```python
{
    "type": "function",
    "function": {
        "name": "web_search",
        "description": "Search the web for current information",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query"
                }
            },
            "required": ["query"]
        }
    }
}
```

### Calculator

```python
{
    "type": "function",
    "function": {
        "name": "calculate",
        "description": "Perform mathematical calculations",
        "parameters": {
            "type": "object",
            "properties": {
                "expression": {
                    "type": "string",
                    "description": "Math expression, e.g. '2+2', 'sqrt(16)', '15% of 200'"
                }
            },
            "required": ["expression"]
        }
    }
}
```

### Database Query

```python
{
    "type": "function",
    "function": {
        "name": "query_database",
        "description": "Query the product database",
        "parameters": {
            "type": "object",
            "properties": {
                "table": {
                    "type": "string",
                    "enum": ["products", "orders", "customers"]
                },
                "filters": {
                    "type": "object",
                    "description": "Key-value pairs to filter by"
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum results to return"
                }
            },
            "required": ["table"]
        }
    }
}
```

***

## Best Practices

### Writing Good Descriptions

The `description` field is crucial—it tells the model when to use the function:

```python
# ❌ Bad: Too vague
"description": "Gets data"

# ✅ Good: Clear and specific
"description": "Get the current weather conditions including temperature, humidity, and forecast for a specific city"
```

### Parameter Descriptions

Help the model provide correct arguments:

```python
"location": {
    "type": "string",
    "description": "City and country, e.g. 'Paris, France' or 'New York, USA'"
}
```

### Error Handling

Return errors as tool results so the model can respond appropriately:

```python
try:
    result = get_weather(location)
except Exception as e:
    result = {"error": str(e)}

# The model will see the error and can tell the user
```

***

## See Also

* [Chat Completions](/api-reference/chat-completions.md) - Full API reference
* [Streaming](/api-reference/streaming.md) - Stream function call responses
* [Models](/api-reference/models.md) - Models that support function calling


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorx.ai/api-reference/function-calling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
