Overview
Function calling works in two steps:- Request: Send a request with tool definitions. The model may request to call one or more tools.
- Execute & Respond: Execute the requested functions and send the results back to the model.
Tool Definition
A tool is defined using a dictionary with the following structure:FunctionDefinition
| Property | Type | Description |
|---|---|---|
name | str | The name of the function (must be unique, a-z, A-Z, 0-9, _, -) |
description | str | None | Description of what the function does. Highly recommended - helps the model understand when to use it |
parameters | dict | None | JSON Schema object describing the function parameters |
Parameters Schema
Theparameters field uses JSON Schema format:
Tool Choice
Thetool_choice parameter controls when and which tools the model should call:
| Value | Type | Description |
|---|---|---|
"auto" | str | Let the model decide whether to call tools (default) |
"none" | str | Don’t call any tools, even if provided |
{"type": "function", "function": {"name": "function_name"}} | dict | Force the model to call a specific function |
Tool Call Object Structure
When the model requests a tool call, you receive aToolCall object in the response:
| Property | Type | Description |
|---|---|---|
id | str | Unique identifier for this tool call |
type | str | Type of tool call (typically "function") |
function | dict | Function call details |
function["name"] | str | Name of the function to call |
function["arguments"] | str | JSON string containing the function arguments |
Parsing Arguments
Complete Example
Here’s a complete end-to-end example with error handling:Streaming with Tools
Thestream() method also supports tools. For details about streaming, see the Stream Method documentation.