Skip to main content

Crate rai_sdk

Crate rai_sdk 

Source
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

§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 builds aws-lc-rs, which requires cmake and a C compiler.
  • native-tls uses 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§

JsonSchema
A type which can be described as a JSON Schema document.

Derive Macros§

JsonSchema
Derive macro for JsonSchema trait.