Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
rai-sdk
rai-sdk is a Rust SDK for building backend AI workflows across OpenAI, Anthropic, and OpenRouter. It provides typed model selection, typestate request builders, structured output validation, streaming, retry/backoff, multimodal prompts, and automatic tool execution loops.
- API reference: https://docs.rs/rai-sdk
- Guide: https://rmagatti.github.io/rai-sdk/
Project status: early and pre-1.0. The crate is usable today, but the public API may change in breaking ways before
1.0. Pin an exact version if you need stability.
Features
- Typed providers and models: use
Model::gpt4o_mini(),Model::claude_sonnet_46(),Model::openrouter_auto(), or custom provider model IDs. - Typestate request builders:
.generate()is only available after a prompt and model are available at compile time. - Structured output: derive
JsonSchemaand call.generate_structured::<T>()or.generate_structured_once::<T>(). - Tool calling: register typed async tools;
generate()executes tool calls and feeds results back to the model until a final answer is produced. - Streaming: consume provider stream events directly, high-level stream events, or use
stream_accumulated()to stream internally and return a full response. - Retry/backoff: transient
RateLimit,Timeout, and HTTP errors are retried with configurable exponential backoff and jitter. - Multimodal prompts: send text, image, audio, video, and file content blocks. Provider support varies.
Installation
Or add it to your Cargo.toml directly, along with the crates the examples below use:
[]
= "0.1"
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
= "1"
= "0.3"
The minimum supported Rust version is 1.86.
Feature flags
Providers (all enabled by default):
openai— OpenAI Chat Completionsanthropic— Anthropic Messagesopenrouter— OpenRouter (aggregates many vendors)
TLS backend (at least one required when a provider is enabled):
rustls-tls(default) — no system OpenSSL needed, but buildsaws-lc-rs, which requires cmake and a C compilernative-tls— uses the platform TLS stack and avoids buildingaws-lc-rs/cmake (Linux needs OpenSSL development files)
Since the TLS backend is part of the default feature set, turning defaults off means naming one explicitly:
[]
= { = "0.1", = false, = ["anthropic", "rustls-tls"] }
Building in a minimal container without cmake? Use native-tls instead:
[]
= { = "0.1", = false, = ["anthropic", "native-tls"] }
Omitting both while enabling a provider fails the build with an explanatory
message. A providerless --no-default-features build remains valid. If Cargo
feature unification enables both TLS features, rai-sdk uses rustls; use the
default-features = false form above to avoid compiling it.
Configuration
Use environment variables:
Optional provider settings:
Optional retry settings:
You can also configure everything in code with ClientBuilder and RetryConfig.
Basic Chat
use ;
async
OpenRouter
use ;
async
Use curated OpenRouter constructors like Model::openrouter_gpt5(), Model::openrouter_deepseek_r1(), and Model::openrouter_qwen3_coder(), or pass any provider model ID with Model::openrouter_custom("vendor/model").
Structured Output
generate_structured() validates the model response against a generated JSON Schema and deserializes it into your Rust type.
use ;
use ;
async
Use generate_structured_once() when configured tools should be ignored and you want a single provider response.
Tool Calling
Tools are typed handlers. generate() automatically runs tool calls, appends tool results, and asks the model to continue until it returns a final response.
use ;
use ;
use json;
async
async
Use .generate_once() if you want the raw provider response with tool calls but do not want the SDK to execute registered tools.
Streaming
For a complete response assembled from the streaming transport:
use ;
async
For raw stream events:
use StreamExt;
use ;
async
Multimodal Prompt
use ;
async
OpenAI and OpenRouter currently serialize image content. Other block types are represented in the common prompt model, but provider-specific support may be incomplete.
Retry Configuration
use Duration;
use ;
async
Disable retries globally with ClientBuilder::new().no_retry() or per request with .request().no_retry().
Examples
Run bundled examples from this repository:
Notes
generate()auto-executes registered tools.generate_once()does not.generate_structured()may use tools before producing typed output.generate_structured_once()ignores configured tools.- Streaming with registered tools is intentionally rejected by the raw streaming API.
- Provider availability is based on enabled Cargo features and configured credentials.
Documentation
- API reference on docs.rs — every public type and method.
- Guide — task-oriented chapters on configuration, providers, structured output, tool calling, streaming, and retries.
Contributing
Contributions are welcome. See CONTRIBUTING.md for local setup, the commands CI runs, and the testing policy — the test suite is fully offline and must never require API credentials.
Please also read our Code of Conduct. To report a security issue, follow SECURITY.md rather than opening a public issue.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.