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-pythonresources/webhooks/webhooks.pyalgorithm.
Structs§
- ApiError
- An error response (4xx/5xx) from the API.
- ApiError
Detail - Structured error detail returned by the API inside
{"error": {...}}. - Client
- Asynchronous client for OpenAI-compatible APIs.
- Client
Builder - 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. - Event
Stream - An async stream of deserialized SSE items from a streaming API response.
- List
- A single page of results from a list endpoint.
- Server
Sent Event - 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§
- ApiError
Kind - Classification of an API error by HTTP status code, mirroring the
APIStatusErrorsubclasses in_exceptions.py. - OpenAI
Error - 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
idusable as anaftercursor.