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 theTool struct:
FunctionDefinition
| Property | Type | Description |
|---|---|---|
name | String | The name of the function (must be unique, a-z, A-Z, 0-9, _, -) |
description | Option<String> | Description of what the function does. Highly recommended - helps the model understand when to use it |
parameters | JsonSchema | JSON Schema object describing the function parameters |
Parameters Schema
Theparameters field uses JSON Schema format via the JsonSchema struct:
Tool Choice
Thetool_choice parameter controls when and which tools the model should call. In Rust, this is set using serde_json::Value:
| Value | Type | Description |
|---|---|---|
"auto" | serde_json::Value | Let the model decide whether to call tools (default) |
"none" | serde_json::Value | Don’t call any tools, even if provided |
{"type": "function", "function": {"name": "function_name"}} | serde_json::Value | 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 | String | Unique identifier for this tool call |
call_type | String | Type of tool call (typically "function") |
function | FunctionCall | Function call details |
function.name | String | Name of the function to call |
function.arguments | String | 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.