Skip to main content

openai_compat/
lib.rs

1//! Async Rust client for OpenAI-compatible LLM provider APIs.
2//!
3//! A port of the core of the official `openai-python` SDK: chat completions
4//! (with SSE streaming), embeddings, models, moderations, images, files,
5//! audio, automatic retries with exponential backoff, and typed errors.
6//!
7//! The `base_url` is configurable, so any OpenAI-compatible provider works.
8//!
9//! ```no_run
10//! use openai_compat::Client;
11//!
12//! # async fn run() -> Result<(), openai_compat::OpenAIError> {
13//! // Reads OPENAI_API_KEY (and optional OPENAI_BASE_URL) from the environment.
14//! let client = Client::new()?;
15//! # Ok(())
16//! # }
17//! ```
18
19mod azure;
20mod client;
21mod config;
22mod error;
23mod pagination;
24pub mod realtime;
25mod request;
26pub mod resources;
27mod retry;
28mod streaming;
29pub mod types;
30pub mod webhooks;
31
32pub use client::Client;
33pub use config::{
34    ClientBuilder, DEFAULT_BASE_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT,
35};
36pub use error::{ApiError, ApiErrorDetail, ApiErrorKind, OpenAIError};
37pub use pagination::{HasId, List};
38pub use streaming::{EventStream, ServerSentEvent, SseDecoder};
39
40// Convenience re-exports of the most-used types.
41pub use types::chat::{
42    ChatCompletion, ChatCompletionChunk, ChatCompletionRequest, ContentPart, Message,
43    MessageContent, Stop, StreamOptions,
44};
45pub use types::common::{FinishReason, ResponseFormat, Role, Tool, ToolChoice, Usage};
46pub use types::responses::{
47    CreateResponseRequest, Input, InputItem, Reasoning, Response, ResponseStreamEvent,
48};
49pub use webhooks::{WebhookEvent, WebhookHeaders, Webhooks};