AI tools
rig tools whose arguments are validated by vld, with the schema prepared for the model provider.

rig tools declare parameters() (a JSON Schema) and get serde-deserialized arguments. vld_web::rig builds parameters() from a vld type and validates the arguments with it; a failure is ToolExecutionError::invalid_args whose model-visible text is llm_feedback().

Install

Cargo.toml
[dependencies]
vld = "0.8"
vld-web = { version = "0.8", features = ["rig"] }
rig-core = "0.47"

The feature enables vld/llm. rig-core 0.47 needs Rust 1.97.1.

Example

use vld::llm::Provider;
use vld_web::rig::{VldTool, VldToolHandler};

vld::schema! {
    #[derive(Debug)]
    pub struct AddArgs {
        pub a: i64 => vld::number().int(),
        pub b: i64 => vld::number().int().min(0),
    }
}

struct Add;

impl VldToolHandler for Add {
    const NAME: &'static str = "add";
    type Args = AddArgs;
    type Output = i64;
    type Error = std::convert::Infallible;

    fn description(&self) -> String {
        "Add two numbers".into()
    }

    async fn call(&self, args: AddArgs) -> Result<i64, Self::Error> {
        Ok(args.a + args.b)
    }
}

let tool = VldTool::new(Add)?;                                   // plain JSON Schema
let strict = VldTool::for_provider(Add, Provider::OpenAiStrict)?; // OpenAI strict function calling

VldTool<H> implements rig's Tool (Args = serde_json::Value, Error = ToolExecutionError); register it like any other tool. for_provider fails with LlmSchemaError when the provider would reject the schema (for example a recursive type for Anthropic), see the provider table.

Arguments {"a": 2.5, "b": -1} give the model:

a: expected integer, got 2.5
b: expected number >= 0, got -1

For a hand-written Tool, vld_web::rig::parse_args::<T>(&args, Some(&schema)) does the same parsing and error mapping.