Skip to main content

Module tools

Module tools 

Source
Expand description

Tools: the Tool trait and its stream, ToolContext, ToolError, FunctionTool, the ToolRegistry, structured output, input schemas and the executors.

§Converting errors

A tool body fails with a ToolError. ? converts any std::error::Error into one, and the interrupt ToolContext::interrupt returns as well. A BoxError, and error types without a std::error::Error impl such as anyhow::Error, do not convert with ?; wrap them with ToolError::from_boxed (or ToolError::msg for any Display). A #[tool] function may instead return Result<T, E> for any E: Into<BoxError>, and the macro converts the error.

use strands::{BoxError, Tool, ToolError, tool};

fn lookup(sku: &str) -> Result<u32, BoxError> {
    if sku == "sku-1" { Ok(3) } else { Err(format!("unknown sku {sku}").into()) }
}

/// Look up stock for a product, plus a reserve.
#[tool]
async fn check_stock(sku: String, reserve: String) -> Result<u32, ToolError> {
    let reserve: u32 = reserve.parse()?;
    let stock = lookup(&sku).map_err(ToolError::from_boxed)?;
    Ok(stock + reserve)
}

/// Look up stock for a product.
#[tool]
async fn check_stock_boxed(sku: String) -> Result<u32, BoxError> {
    lookup(&sku)
}

assert_eq!(CheckStock.name(), "check_stock");
assert_eq!(CheckStockBoxed.name(), "check_stock_boxed");

Re-exports§

pub use executors::ConcurrentToolExecutor;
pub use executors::SequentialToolExecutor;
pub use executors::ToolExecutorStrategy;

Modules§

executors
Tool executors: how the tool calls of one model turn run.

Structs§

FunctionTool
A tool that wraps a callback and shapes its return value into a result (FunctionTool).
FunctionToolBuilder
Builds a FunctionTool; finish with callback or stream_callback.
SchemaValidator
Validates tool input against a JSON Schema.
StructuredOutputTool
Validates the model’s structured output against a JSON Schema (StructuredOutputTool).
ToolContext
What a tool receives when it runs (ToolContext).
ToolError
The error a tool body returns, much like anyhow::Error.
ToolRegistry
The tools an agent can call, by name (ToolRegistry).
ToolStream
The stream a Tool returns (ToolStreamGenerator): progress events, then one outcome.
ToolStreamEvent
Progress data a tool yields while it runs (ToolStreamEvent).

Enums§

FunctionToolItem
What a streaming FunctionTool callback yields (an async generator’s yield and return).
ToolOutcome
How a tool run ended.
ToolSource
Something that provides tools to an agent.
ToolStreamItem
One item of a ToolStream.

Constants§

LOAD_TOOLS_TOOL_NAME
Name of the tool a model calls to load the input schemas of deferred tools (LOAD_TOOLS_TOOL_NAME).
STRUCTURED_OUTPUT_TOOL_NAME
The name of the structured output tool.

Traits§

Tool
A tool the model can call (Tool).

Functions§

create_error_result
An error tool result whose content is Error: <message> and which keeps the original error.
is_valid_tool_name
Whether name is a tool name providers accept: 1 to 64 characters, only ASCII letters, digits, _ and -.
normalize_schema
Normalizes a schemars schema for model providers.
schema_for
The normalized input schema of T, generated with schemars.