Skip to main content

OpenAiClientBuilder

Struct OpenAiClientBuilder 

Source
pub struct OpenAiClientBuilder { /* private fields */ }
Expand description

Builder for OpenAiClient.

Created via OpenAiClientBuilder::default or OpenAiClient::builder. All fields have sensible defaults except api_key, which must be set before build.

Implementations§

Source§

impl OpenAiClientBuilder

Source

pub fn with_api_key(self, key: impl Into<String>) -> Self

Set the API key for authentication.

Required — build returns an error if this is not set. The key is sent as the Authorization: Bearer <key> header on every request.

Source

pub fn with_base_url(self, url: impl Into<String>) -> Self

Set the base URL for API requests.

Defaults to https://api.openai.com/v1. Override when targeting an OpenAI-compatible endpoint (e.g. https://api.deepseek.com/v1, http://localhost:11434/v1 for Ollama, or a vLLM server).

Source

pub fn with_model(self, model: impl Into<String>) -> Self

Set the default model identifier.

The model string is sent as the model field on every request. Can be changed at runtime via OpenAiClient::set_model (e.g. when the FallbackManager trips).

Source

pub fn with_timeout(self, timeout: Duration) -> Self

Set the total request timeout (connect + response + body).

Defaults to 120 seconds. Ignored when a client was supplied via with_http_client.

Source

pub fn with_connect_timeout(self, timeout: Duration) -> Self

Set the TCP connection establishment timeout.

Defaults to 10 seconds. Ignored when a client was supplied via with_http_client.

Source

pub fn with_http_client(self, client: Client) -> Self

Inject a pre-built, shared reqwest::Client.

When set, the client’s connection pool is shared with every other provider built from the same handle, and the pool/TCP knobs are ignored. Configure timeouts on the injected client, not here.

Source

pub fn with_stream_usage(self, enabled: bool) -> Self

Control whether streaming requests include stream_options.include_usage.

Defaults to true — real OpenAI, DeepSeek, and Grok support it and send a final usage chunk. Pass false for OpenAI-compatible servers that reject the parameter with a validation error (older Ollama, some self-hosted vLLM/LM Studio deployments). When disabled, streamed turns report usage: None instead of real token counts.

Ignored on non-streaming requests.

Source

pub fn with_pool_max_idle_per_host(self, n: usize) -> Self

Set the maximum idle connections kept alive per host.

Defaults to reqwest’s built-in default (unlimited). Ignored when a client was supplied via with_http_client.

Source

pub fn with_pool_idle_timeout(self, d: Duration) -> Self

Set how long an idle connection stays in the pool before being closed.

Defaults to reqwest’s built-in default (90s). Ignored when a client was supplied via with_http_client.

Source

pub fn with_tcp_keepalive(self, d: Duration) -> Self

Set the OS-level TCP keepalive interval.

Defaults to disabled (reqwest default). Ignored when a client was supplied via with_http_client.

Source

pub fn with_tcp_nodelay(self, enabled: bool) -> Self

Control whether TCP_NODELAY is set on connections.

Defaults to true — SSE streaming benefits from disabling Nagle’s algorithm. Pass false to re-enable it. Ignored when a client was supplied via with_http_client.

Source

pub fn build(self) -> Result<OpenAiClient, ApiError>

Build the client.

§Errors

Returns ApiError if no API key was set.

Trait Implementations§

Source§

impl Default for OpenAiClientBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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