Skip to main content

Client

Struct Client 

Source
pub struct Client<ModelState = ModelMissing> { /* private fields */ }
Expand description

Unified AI client for OpenAI, Anthropic, and OpenRouter.

A client owns provider credentials and HTTP clients, an optional default model, default generation and retry settings, and any tools shared by every request. Build one once and reuse it: individual requests are cheap, but constructing a client initializes a client per configured provider.

Use ClientBuilder for the common path, or Client::new when you already have a Config.

§Typestate

The ModelState parameter records whether a default model is present. ClientBuilder::model moves the builder into the model-ready state, and only a model-ready client hands out request builders that can call RequestBuilder::generate without naming a model. A client built without a default model is still fully usable — every request just has to call RequestBuilder::model first. Either way, a request missing a model is a compile error rather than a runtime one.

§Examples

use rai_sdk::{ClientBuilder, Model};

let client = ClientBuilder::new()
    .from_env()
    .model(Model::gpt4o_mini())
    .build()?;

// Reuse the same client for many requests.
for prompt in ["Define a trait.", "Define a lifetime."] {
    let response = client.request().prompt(prompt).generate().await?;
    println!("{}", response.text());
}

Implementations§

Source§

impl Client<ModelMissing>

Source

pub fn new(config: Config) -> Result<Self>

Create a client from an explicit Config, with no default model.

Every request from this client must select a model with RequestBuilder::model. Use ClientBuilder instead if you want a default model or client-level tools.

A provider whose API key is missing is simply left uninitialized rather than failing here; using it later returns Error::ProviderNotConfigured.

§Errors

Returns an error if a configured provider’s HTTP client cannot be constructed, for example because the request timeout is invalid.

§Examples
use rai_sdk::{Client, Config, Model};

let client = Client::new(Config::from_env())?;

let response = client
    .request()
    .model(Model::gpt4o_mini())
    .prompt("Hello")
    .generate()
    .await?;
Source

pub fn builder() -> ClientBuilder<ModelMissing>

Create a builder for configuring a client with defaults.

Equivalent to ClientBuilder::new.

Source

pub fn request( &self, ) -> RequestBuilder<'_, PromptMissing, ModelMissing, ModelMissing>

Start a request.

This client has no default model, so the returned builder requires RequestBuilder::model before it will expose generate and friends.

Source§

impl<ModelState> Client<ModelState>

Source

pub async fn generate_stream( &self, model: Model, prompt: &Prompt, config: &GenerationConfig, ) -> Result<Pin<Box<dyn Stream<Item = Result<ProviderStreamEvent>> + Send>>>

Stream a completion for an explicit model and prompt.

Prefer RequestBuilder::stream, which applies the client’s defaults, retry policy, and per-request tool overrides. This lower-level entry point is useful when you are driving the model and prompt yourself.

§Errors
Source

pub fn is_provider_available(&self, provider: ProviderKind) -> bool

Whether a provider is usable: its feature is enabled and it has credentials.

Use this to branch at runtime instead of discovering a missing key through a failed request.

§Examples
use rai_sdk::{ClientBuilder, Model, ProviderKind};

let client = ClientBuilder::new().from_env().build()?;

let model = if client.is_provider_available(ProviderKind::Anthropic) {
    Model::claude_sonnet_46()
} else {
    Model::gpt4o_mini()
};
Source

pub fn config(&self) -> &Config

The configuration this client was built with.

Note that the returned Config contains API keys; do not log it.

Source§

impl Client<ModelReady>

Source

pub fn request( &self, ) -> RequestBuilder<'_, PromptMissing, ModelReady, ModelReady>

Start a request that inherits this client’s default model.

Because the model is already known, the returned builder only needs a prompt before you can call RequestBuilder::generate. Override the model per request with RequestBuilder::model.

Trait Implementations§

Source§

impl<ModelState> Debug for Client<ModelState>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<ModelState = ModelMissing> !RefUnwindSafe for Client<ModelState>

§

impl<ModelState = ModelMissing> !UnwindSafe for Client<ModelState>

§

impl<ModelState> Freeze for Client<ModelState>

§

impl<ModelState> Send for Client<ModelState>
where ModelState: Send,

§

impl<ModelState> Sync for Client<ModelState>
where ModelState: Sync,

§

impl<ModelState> Unpin for Client<ModelState>
where ModelState: Unpin,

§

impl<ModelState> UnsafeUnpin for Client<ModelState>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more