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.
  • A handler function that executes the tool logic.

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, required ToolHandler handler})
Creates a new ToolDefinition.
const

Properties

description String
Human-readable description of what the tool does.
final
handler ToolHandler
The function that executes the tool logic.
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