rai-sdk
rai-sdk is a Rust SDK for building backend AI workflows across OpenAI, Anthropic, OpenRouter, and any OpenAI-compatible endpoint such as Ollama, vLLM, or LM Studio. 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. Dropping a stream aborts the upstream provider request. - Proxyable streams:
stream_wire_events()yields serializable events so a server can re-emit a generation to its own clients over SSE, andStreamAccumulatorreassembles them on the far side. - Local and self-hosted models: point a client at any OpenAI-compatible endpoint — Ollama, vLLM, LM Studio — with no API key required, and get a typed error rather than an opaque HTTP failure when the endpoint cannot do tools or structured output.
- 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 Completions, and OpenAI-compatible endpoints (Ollama, vLLM, LM Studio)anthropic— Anthropic Messagesopenrouter— OpenRouter (aggregates many vendors)
The OpenAI-compatible provider has no feature of its own: it is the same wire format, reusing the openai module's request builder and stream parser, so a build that only talks to local models enables openai and nothing else.
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.
OpenAI-compatible endpoints are the exception: they have no environment variables and are always configured per client, because one process routinely talks to several. OPENAI_BASE_URL keeps its existing meaning and still redirects only the real OpenAI provider.
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").
Local and Self-Hosted Models
Ollama, vLLM, LM Studio, llama.cpp's server, and most inference gateways serve POST {base_url}/chat/completions in OpenAI's format. Name the endpoint on the client and the rest of the SDK is unchanged — same request builder, same streaming, same structured output.
use ;
async
For anything other than Ollama's default address, name the endpoint yourself. An API key is optional: with none configured, no Authorization header is sent at all.
use ;
The endpoint is per client, so those two coexist in one process with their own credentials and capabilities.
Capability degradation
"OpenAI-compatible" describes a wire format, not a feature set: a small local model may not call tools, and a runtime may not honor response_format. Those failures arrive as Error::CapabilityUnsupported, distinct from the generic HTTP and request errors, so falling back is a match arm rather than a string search.
use ;
async
Capabilities are declared, never probed: auto-detection would cost a round trip on every client build and still be wrong per 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
Proxying a Stream Over SSE
When your server holds the provider credentials and streams results on to a desktop or browser client, the events have to cross a wire. stream_wire_events() yields WireStreamEvents, which serialize to a tagged JSON object — one SSE data: payload each.
client ──POST──▶ your server ──rai-sdk──▶ provider
◀──SSE─── WireStreamEvent ◀────────┘
use StreamExt;
use ;
async
On the receiving side, StreamAccumulator is the client-side counterpart of stream_accumulated():
use ;
cargo run --example sse_proxy runs the whole loop — axum handler, SSE re-emission, client-side reassembly — in one process.
Wire format
Unlike the other streaming methods, stream_wire_events() items are not Results. Once the stream is open every outcome is an event, tagged with a "type" discriminant:
"type" |
Meaning |
|---|---|
message_start |
First event of every stream; names the protocol version, model, and provider. |
text_delta |
Append this text to the output so far. |
tool_call_start / tool_call_delta / tool_call_end |
A tool call, first incrementally and then assembled. |
tool_result |
The output of executing a tool call. Only a proxy that runs tools itself emits this. |
usage |
Token counts, emitted once just before the terminal event. |
message_stop |
Terminal event of a successful stream. |
turn_complete |
An assembled ConversationTurn, for history. |
error |
Terminal event of a failed stream. |
A mid-stream provider failure arrives as error rather than as a truncated response, so a client can tell "the provider refused" from "the network died" — the latter being a stream that ends with no terminal event at all. StreamAccumulator::finish() enforces that distinction.
The "type" strings and each event's field names are a compatibility surface. A server and a client can be built from different rai-sdk versions, so renaming or removing one is a breaking change and will be called out in the changelog; adding a variant is not. WireStreamEvent and WireErrorKind are both #[non_exhaustive], and an unrecognized error kind deserializes into WireErrorKind::Other, so match with a catch-all arm. WIRE_PROTOCOL_VERSION names the current revision of the framing and rides on every message_start.
Cancelling a Stream
Dropping a stream aborts the upstream provider request. Every streaming method is driven entirely by its consumer — the provider's response body is polled from inside the returned stream, never from a detached background task — so dropping the stream closes the connection and the provider stops generating. No orphaned generation keeps burning tokens.
That holds when the surrounding task is cancelled rather than the stream explicitly dropped, which is what a tokio::time::timeout or a web-framework client disconnect looks like. Note that a cancelled generation reports no usage, so metering cannot rely on the final usage event alone.
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.
- Dropping a stream aborts the upstream provider request, so a cancelled generation reports no usage.
- Provider availability is based on enabled Cargo features and configured credentials. An OpenAI-compatible endpoint is available once its base URL is set, with or without a key.
- OpenAI-compatible endpoint capabilities are declared, not detected. The default assumes full compatibility.
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.