pub struct OpenAI { /* private fields */ }Expand description
The main OpenAI client.
See OpenAI API docs for the full API reference.
Use with_options() to create a cheap clone with
per-request customization (extra headers, query params, timeout):
use openai_oxide::RequestOptions;
use std::time::Duration;
let custom = client.with_options(
RequestOptions::new()
.header("X-Custom", "value")
.timeout(Duration::from_secs(30))
);Implementations§
Source§impl OpenAI
impl OpenAI
Sourcepub fn with_config<C: Config + 'static>(config: C) -> Self
pub fn with_config<C: Config + 'static>(config: C) -> Self
Create a client from a full config.
Sourcepub fn with_options(&self, options: RequestOptions) -> Self
pub fn with_options(&self, options: RequestOptions) -> Self
Create a cheap clone of this client with additional request options.
The returned client shares the same HTTP connection pool (reqwest::Client
uses Arc internally) but applies the merged options to every request.
use openai_oxide::RequestOptions;
let custom = client.with_options(
RequestOptions::new().header("X-Custom", "value")
);
// All requests through `custom` will include the X-Custom header.
let resp = custom.chat().completions().create(req).await?;Sourcepub fn client(&self) -> &Self
pub fn client(&self) -> &Self
Returns a reference to self for direct access to low-level request methods.
Useful for napi/FFI bindings that need post_json_bytes etc.
Sourcepub fn from_env() -> Result<Self, OpenAIError>
pub fn from_env() -> Result<Self, OpenAIError>
Create a client using the OPENAI_API_KEY environment variable.
Sourcepub fn azure(config: AzureConfig) -> Result<Self, OpenAIError>
pub fn azure(config: AzureConfig) -> Result<Self, OpenAIError>
Sourcepub fn beta(&self) -> Beta<'_>
pub fn beta(&self) -> Beta<'_>
Access the Beta resources (Assistants, Threads, Runs, Vector Stores).
Sourcepub fn fine_tuning(&self) -> FineTuning<'_>
pub fn fine_tuning(&self) -> FineTuning<'_>
Access the Fine-tuning resource.
Sourcepub fn moderations(&self) -> Moderations<'_>
pub fn moderations(&self) -> Moderations<'_>
Access the Moderations resource.
Sourcepub fn embeddings(&self) -> Embeddings<'_>
pub fn embeddings(&self) -> Embeddings<'_>
Access the Embeddings resource.
Sourcepub fn conversations(&self) -> Conversations<'_>
pub fn conversations(&self) -> Conversations<'_>
Access conversation endpoints (multi-turn server-side state).
Sourcepub fn realtime(&self) -> Realtime<'_>
pub fn realtime(&self) -> Realtime<'_>
Access the GA Realtime endpoints (post-2026 client_secrets API).
The legacy beta accessor (client.beta().realtime()) targets the
retired /realtime/sessions and /realtime/transcription_sessions
paths. Use this instead for new code.
Sourcepub async fn post_json_bytes(
&self,
path: &str,
json_bytes: Bytes,
) -> Result<Value, OpenAIError>
pub async fn post_json_bytes( &self, path: &str, json_bytes: Bytes, ) -> Result<Value, OpenAIError>
Send a POST request with a pre-serialized JSON body and return raw JSON.
Skips serde_json::to_vec — the caller provides already-serialized bytes.
Used by napi bindings where JS already has the JSON string, avoiding
double serialization (napi copy + serde serialize).
Sourcepub async fn post_stream_json_bytes(
&self,
path: &str,
json_bytes: Bytes,
) -> Result<Response, OpenAIError>
pub async fn post_stream_json_bytes( &self, path: &str, json_bytes: Bytes, ) -> Result<Response, OpenAIError>
Send a POST request with pre-serialized JSON body and return an SSE stream.