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§
- Function
Tool - A tool that wraps a callback and shapes its return value into a result (
FunctionTool). - Function
Tool Builder - Builds a
FunctionTool; finish withcallbackorstream_callback. - Schema
Validator - Validates tool input against a JSON Schema.
- Structured
Output Tool - Validates the model’s structured output against a JSON Schema (
StructuredOutputTool). - Tool
Context - What a tool receives when it runs (
ToolContext). - Tool
Error - The error a tool body returns, much like
anyhow::Error. - Tool
Registry - The tools an agent can call, by name (
ToolRegistry). - Tool
Stream - The stream a
Toolreturns (ToolStreamGenerator): progress events, then one outcome. - Tool
Stream Event - Progress data a tool yields while it runs (
ToolStreamEvent).
Enums§
- Function
Tool Item - What a streaming
FunctionToolcallback yields (an async generator’syieldandreturn). - Tool
Outcome - How a tool run ended.
- Tool
Source - Something that provides tools to an agent.
- Tool
Stream Item - 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
nameis 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.