llm_tool library

Annotate Dart functions with @LlmTool() and get LLM tool definitions: JSON schema, argument validation and type-safe dispatch.

Add llm_tool_generator and build_runner as dev dependencies to generate a ToolDefinition for every LlmTool function.

Classes

LlmTool
Marks a top-level function, or a method of an LlmToolset class, as a tool an LLM can call.
LlmToolset
Marks a class whose @LlmTool methods are tools, e.g. tools that need a service, a repository or other state.
Param
Describes one parameter of an LlmTool function to the LLM.
ToolDefinition<T>
A tool an LLM can call: its name, description, JSON schema and code.
ToolResult
What happened when a tool was invoked, ready to send back to the model: a value on success, or an error written for the model to read.

Extensions

ToolDefinitionFormats on ToolDefinition
A tool in each provider's JSON format, to pass to that provider's SDK (e.g. its Tool.fromJson) or REST API. No SDK is needed.
ToolListFormats on Iterable<ToolDefinition>
The same formats for a list of tools, e.g. the generated allTools, and running a model's call by the tool name it sent.

Functions

toolSchemaSnapshot(Iterable<ToolDefinition> tools) → String
Everything the LLM sees of tools, as stable, readable JSON, for a snapshot test that fails when a schema changes:
validateArguments(Map<String, Object?> schema, Map<String, Object?> args) → List<String>
Checks args against a JSON Schema schema. Returns a list of problems; an empty list means the arguments are valid.
withoutAdditionalProperties(Map<String, Object?> schema) → Map<String, Object?>
A copy of schema without additionalProperties, at every level.

Typedefs

ToolConfirmation = FutureOr<bool> Function(ToolDefinition tool, Map<String, Object?> args)
Asks the user whether tool may run with args. Return true to run it.

Exceptions / Errors

ToolArgumentException
Thrown when the AI sends arguments that don't match a tool's schema.