Expand description
A unified Rust SDK for backend AI workflows across OpenAI, Anthropic, and OpenRouter.
rai-sdk wraps three provider APIs behind one typed client so switching
models does not mean rewriting request-building, streaming, or tool-calling
code.
§Capabilities
- Typed providers and models — construct models with
Model::gpt4o_mini,Model::claude_sonnet_46, orModel::openrouter_auto, or pass any provider model ID directly. - Typestate request builders —
RequestBuilder::generateonly exists once a prompt and a model are present, so incomplete requests fail to compile rather than at runtime. - Structured output — derive
JsonSchemaand callRequestBuilder::generate_structuredto validate the response against a generated schema and deserialize it into your own type. - Tool calling — register typed async tools with
Tool;RequestBuilder::generateruns the tool loop, feeding results back until the model produces a final answer. - Streaming — consume raw provider events, or use
RequestBuilder::stream_accumulatedto stream internally and return a completeResponse. - Proxyable streams —
RequestBuilder::stream_wire_eventsyields serializableWireStreamEvents so a server can re-emit a generation to its own clients over SSE, andStreamAccumulatorreassembles them on the far end. See thewiremodule. - Retries — transient rate-limit, timeout, and HTTP failures are retried
with configurable exponential backoff and jitter via
RetryConfig. - Multimodal prompts — build prompts from text, image, audio, video, and
file
ContentBlocks. Provider support varies. - Local and self-hosted models — point a client at any endpoint speaking
the OpenAI Chat Completions format with
ClientBuilder::openai_compatible_base_url(orClientBuilder::ollama) and name models withModel::openai_compatible. See theprovider::openai_compatiblemodule, which theopenaifeature gates.
§Quickstart
use rai_sdk::{ClientBuilder, Model};
let client = ClientBuilder::new()
.from_env()
.model(Model::gpt4o_mini())
.build()?;
let response = client
.request()
.prompt("Explain Rust ownership in two sentences.")
.generate()
.await?;
println!("{}", response.text());§Configuration
ClientBuilder::from_env reads OPENAI_API_KEY, ANTHROPIC_API_KEY, and
OPENROUTER_API_KEY, along with optional base-URL, timeout, and retry
overrides. Everything can also be set explicitly on ClientBuilder or
Config, and explicit values take precedence over the environment.
§Cargo features
The openai, anthropic, and openrouter features are all enabled by
default and gate the corresponding provider support. Disable the defaults to
compile against only the providers you use.
openai additionally gates the OpenAI-compatible provider, which reuses
that module’s request builder and stream parser rather than duplicating
them. A build that talks only to local models therefore enables openai
and nothing else.
Enabling a provider also requires at least one TLS backend:
rustls-tls(default) needs no system OpenSSL, but buildsaws-lc-rs, which requires cmake and a C compiler.native-tlsuses the platform TLS stack instead, avoiding that build requirement.
Because a TLS backend is part of the default feature set, disabling default features means re-enabling one explicitly:
rai-sdk = { version = "0.1", default-features = false, features = ["anthropic", "native-tls"] }Cargo features are additive, so dependency feature unification can enable
both backends. That configuration is supported and uses rustls; select only
native-tls as shown above to avoid compiling aws-lc-rs.
§Further reading
The guide covers each capability in task-oriented chapters. Its examples are compile-checked against this crate, so they stay in sync with the API you see here.
Re-exports§
pub use client::Client;pub use client::ClientBuilder;pub use client::RequestBuilder;pub use config::Config;pub use config::EndpointCapabilities;pub use error::Capability;pub use error::Error;pub use error::ProviderKind;pub use error::Result;pub use error::ToolArgumentIssue;pub use generation::GenerationConfig;pub use message::ContentBlock;pub use message::ImageSource;pub use message::Message;pub use message::Prompt;pub use message::Response;pub use message::Role;pub use message::StreamChunk;pub use message::StructuredOutput;pub use message::ToolCall;pub use message::ToolDefinition;pub use message::Usage;pub use model::AnthropicModel;pub use model::Model;pub use model::OpenAICompatibleModel;pub use model::OpenAIModel;pub use model::OpenRouterModel;pub use retry::RetryConfig;pub use tool::Tool;pub use tool::ToolContext;pub use wire::StreamAccumulator;pub use wire::WIRE_PROTOCOL_VERSION;pub use wire::WireError;pub use wire::WireErrorKind;pub use wire::WireStreamEvent;pub use schemars;
Modules§
- client
- The client and request builders that drive generation.
- config
- Client-level configuration: credentials, endpoints, timeouts, retries.
- error
- Error types shared by every provider.
- generation
- Per-request generation settings.
- message
- Prompts, messages, and responses.
- model
- Typed model catalogs for every supported provider.
- provider
- Provider implementations and the low-level streaming event they emit.
- retry
- Retry policy for transient provider failures.
- tool
- Typed tools that the model can call during generation.
- wire
- Serializable stream events for proxying a generation across a network hop.
Traits§
- Json
Schema - A type which can be described as a JSON Schema document.
Derive Macros§
- Json
Schema - Derive macro for
JsonSchematrait.