Function Calling

Function calling (also known as tool use) allows the model to invoke external functions you define. Instead of generating a text-only response, the model can output a structured function call with arguments, which your code executes and returns the result for the model to incorporate into its final answer.

This enables the model to interact with APIs, databases, calculators, and any external system.

How It Works

  1. You define one or more tools (functions) in the request, each with a name, description, and JSON Schema for parameters.

  2. The model decides whether to call a tool based on the user's message.

  3. If the model calls a tool, it returns a response with finish_reason: "tool_calls" containing the function name and arguments.

  4. Your code executes the function and sends the result back.

  5. The model generates a final response using the function result.

User message  -->  Model  -->  tool_calls response
                                    |
                          Your code executes function
                                    |
                          Tool result sent back  -->  Model  -->  Final response

Defining Tools

Tools are defined in the tools array of the request. Each tool has a type of "function" and a function object containing the name, description, and parameter schema.

{
  "model": "dos-ai",
  "messages": [
    { "role": "user", "content": "What is the weather in Hanoi?" }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get the current weather for a given city.",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string",
              "description": "The city name, e.g. 'Hanoi' or 'Ho Chi Minh City'."
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"],
              "description": "Temperature unit. Defaults to celsius."
            }
          },
          "required": ["city"]
        }
      }
    }
  ]
}

Best Practices for Tool Definitions

  • Write clear descriptions. The model uses the description to decide when to call the function. Be specific about what the function does and when it should be used.

  • Use JSON Schema constraints. Use enum, required, minimum, maximum, and pattern to constrain parameters. This helps the model generate valid arguments.

  • Keep parameter names intuitive. Use descriptive names like city rather than c or param1.

Handling Tool Calls

When the model decides to call a tool, the response looks like this:

Key points:

  • content may be null when the model makes a tool call.

  • arguments is a JSON string that you need to parse.

  • id on each tool call is required when sending the result back.

  • finish_reason is "tool_calls" instead of "stop".

Sending Tool Results

After executing the function, send the result back by appending both the assistant's tool call message and a tool role message to the conversation:

The model then generates a natural language response incorporating the tool result:

Multiple Tool Calls

The model can call multiple tools in a single response. This is known as parallel tool calling.

When responding, include a separate tool message for each call, matched by tool_call_id:

Controlling Tool Use

You can control whether and how the model uses tools with the tool_choice parameter:

Value
Behavior

"auto"

The model decides whether to call a tool (default).

"none"

The model will not call any tools, even if they are defined.

"required"

The model must call at least one tool.

{"type": "function", "function": {"name": "get_weather"}}

Force the model to call a specific function.

Complete Example

Python

JavaScript

cURL

Error Handling

Common issues with function calling:

Issue
Cause
Solution

Model ignores tools

Description is too vague

Write a clearer, more specific function description.

Invalid arguments

Schema is too loose

Add required, enum, and constraints to the schema.

Model hallucinates functions

Too many tools defined

Reduce the number of tools, or use tool_choice to constrain.

JSON parse error on arguments

Model output was malformed

Wrap JSON.parse in a try/catch and retry the request.

Next Steps

Last updated