Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai-compatible/llms.txt

Use this file to discover all available pages before exploring further.

ChatToolConverter serializes the tool definitions and tool-choice policy that accompany a chat request. It is called by ChatRequestBuilder::build() when the request carries one or more tools, and its two static methods handle the tools array and the tool_choice field independently. You never need to instantiate this class directly.

Namespace

AiSdk\OpenAICompatible\Converters\ChatToolConverter

Methods

convert()

public static function convert(array $tools): array<int, array<string, mixed>>
Converts an array of AiSdk\Tool objects into the OpenAI tools array. Each tool is wrapped in the {"type": "function", "function": {...}} envelope expected by the API.
tools
array<int, Tool>
required
An array of AiSdk\Tool objects to include in the request. Passing an empty array produces an empty output array.
Returns array<int, array<string, mixed>> — each element has the shape:
{
  "type": "function",
  "function": {
    "name": "<tool name>",
    "description": "<tool description>",
    "parameters": { /* JSON Schema from inputSchemaForProvider() */ }
  }
}
The parameters value is sourced from Tool::inputSchemaForProvider(), which returns the JSON Schema object that describes the tool’s accepted inputs.

choice()

public static function choice(?ToolChoice $choice): string|array<string, mixed>
Converts a portable AiSdk\ToolChoice object (or null) into the value for the OpenAI tool_choice request field.
choice
ToolChoice|null
The desired tool-choice policy. When null, the converter defaults to 'auto', letting the model decide whether to call a tool.
Returns a string for standard policies or an array when a specific tool is forced:
$choice valueReturned value
null'auto'
ToolChoice::TOOL (specific function){"type": "function", "function": {"name": "<toolName>"}}
Any other type (e.g. 'none', 'required')$choice->type as a plain string

Example

use AiSdk\Tool;
use AiSdk\ToolChoice;
use AiSdk\OpenAICompatible\Converters\ChatToolConverter;

// --- Serialize tool definitions ---

$tools = [
    new Tool(
        name: 'get_weather',
        description: 'Returns the current weather for a city.',
        // inputSchemaForProvider() returns the JSON Schema array
    ),
];

$wireTools = ChatToolConverter::convert($tools);

// $wireTools =>
// [
//   [
//     'type'     => 'function',
//     'function' => [
//       'name'        => 'get_weather',
//       'description' => 'Returns the current weather for a city.',
//       'parameters'  => [ /* JSON Schema */ ],
//     ],
//   ],
// ]

// --- Serialize tool choice: auto (null) ---

ChatToolConverter::choice(null);
// => 'auto'

// --- Serialize tool choice: force a specific tool ---

ChatToolConverter::choice(ToolChoice::tool('get_weather'));
// => ['type' => 'function', 'function' => ['name' => 'get_weather']]

// --- Serialize tool choice: none or required ---

ChatToolConverter::choice(ToolChoice::none());
// => 'none'

ChatToolConverter::choice(ToolChoice::required());
// => 'required'
ChatRequestBuilder only adds the tools and tool_choice keys to the request body when $request->tools is non-empty. If no tools are passed, neither key appears in the serialized payload, which avoids sending an empty tools: [] that some providers reject.

Build docs developers (and LLMs) love