ToolDefinition class

Defines a tool that the LLM can invoke.

A tool definition includes:

  • A unique name that the model uses to reference the tool.
  • A description explaining what the tool does (helps the model decide when to use it).
  • A list of parameters defining the expected input schema.
  • An optional handler function that executes the tool logic.

ChatSession.sendWithTools runs the handler of each tool the model calls. Leave it out for a tool the app runs itself, such as one that needs user approval or is executed by a remote client.

Example:

final weatherTool = ToolDefinition(
  name: 'get_weather',
  description: 'Get the current weather for a location',
  parameters: [
    ToolParam.string('location', description: 'City name', required: true),
    ToolParam.enumType('unit', values: ['celsius', 'fahrenheit']),
  ],
  handler: (params) async {
    final location = params.getRequiredString('location');
    final unit = params.getString('unit') ?? 'celsius';
    // Fetch weather...
    return 'Weather in $location: 22°$unit';
  },
);

Constructors

ToolDefinition({required String name, required String description, required List<ToolParam> parameters, ToolHandler? handler})
Creates a new ToolDefinition.
const
ToolDefinition.fromJsonSchema({required String name, required String description, required Map<String, dynamic> schema, ToolHandler? handler})
Creates a tool with an owned JSON Schema rather than parameter helpers.
factory

Properties

description → String
Human-readable description of what the tool does.
final
handler → ToolHandler?
The function that executes the tool logic, or null when the app runs the tool itself.
final
hashCode → int
The hash code for this object.
no setterinherited
name → String
Unique name for the tool (e.g., "get_weather").
final
parameters → List<ToolParam>
List of parameter definitions for the tool's input.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

invoke(Map<String, dynamic> args) → Future<Object?>
Invokes the tool's handler with the given args.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson() → Map<String, dynamic>
Converts the tool definition to an OpenAI-compatible JSON object.
toJsonSchema() → Map<String, dynamic>
Converts the tool's parameters to a JSON Schema object.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited