Skip to main content

Crate openai_compat

Crate openai_compat 

Source
Expand description

Async Rust client for OpenAI-compatible LLM provider APIs.

A port of the core of the official openai-python SDK: chat completions (with SSE streaming), embeddings, models, moderations, images, files, audio, automatic retries with exponential backoff, and typed errors.

The base_url is configurable, so any OpenAI-compatible provider works.

use openai_compat::Client;

// Reads OPENAI_API_KEY (and optional OPENAI_BASE_URL) from the environment.
let client = Client::new()?;

Re-exports§

pub use types::chat::ChatCompletion;
pub use types::chat::ChatCompletionChunk;
pub use types::chat::ChatCompletionRequest;
pub use types::chat::ContentPart;
pub use types::chat::Message;
pub use types::chat::MessageContent;
pub use types::chat::Stop;
pub use types::chat::StreamOptions;
pub use types::common::FinishReason;
pub use types::common::ResponseFormat;
pub use types::common::Role;
pub use types::common::Tool;
pub use types::common::ToolChoice;
pub use types::common::Usage;
pub use webhooks::WebhookEvent;
pub use webhooks::WebhookHeaders;
pub use webhooks::Webhooks;

Modules§

realtime
Realtime WebSocket support for the OpenAI Realtime API.
resources
API resources, one module per endpoint group (mirrors openai-python/src/openai/resources/).
types
Request/response types, one module per endpoint group (mirrors openai-python/src/openai/types/).
webhooks
Webhook signature verification, mirroring the openai-python resources/webhooks/webhooks.py algorithm.

Structs§

ApiError
An error response (4xx/5xx) from the API.
ApiErrorDetail
Structured error detail returned by the API inside {"error": {...}}.
Client
Asynchronous client for OpenAI-compatible APIs.
ClientBuilder
Builder for Client. Unset fields fall back to the same environment variables the Python SDK uses: OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_ORG_ID, OPENAI_PROJECT_ID.
EventStream
An async stream of deserialized SSE items from a streaming API response.
List
A single page of results from a list endpoint.
ServerSentEvent
A single decoded server-sent event.
SseDecoder
Incremental SSE parser. Push bytes in with SseDecoder::feed; complete events are returned as they are terminated by a blank line.

Enums§

ApiErrorKind
Classification of an API error by HTTP status code, mirroring the APIStatusError subclasses in _exceptions.py.
OpenAIError
All errors produced by this crate.

Constants§

DEFAULT_BASE_URL
DEFAULT_CONNECT_TIMEOUT
Default connect timeout (5s), from _constants.py::DEFAULT_TIMEOUT.
DEFAULT_MAX_RETRIES
Default retry count, from _constants.py::DEFAULT_MAX_RETRIES.
DEFAULT_TIMEOUT
Default request timeout (600s), from _constants.py::DEFAULT_TIMEOUT.

Traits§

HasId
Items that expose an id usable as an after cursor.