Skip to main content

Crate openai_client_rs

Crate openai_client_rs 

Source
Expand description

§openai-client-rs

A Rust client for the OpenAI API — chat completions, embeddings, images, audio, files, and streaming SSE. Compatible with OpenAI and any OpenAI-compatible provider (DeepSeek, Kimi, MiniMax, Ollama, etc).

§Features

  • Sync (default) — lightweight ureq
  • Async — optional reqwest + tokio (features = ["async"])
  • Chat Completions — full API with streaming SSE
  • Models / Embeddings / Images / Audio / Files
  • Tool calling — function calling with fragmented argument accumulation
  • Reasoning — thinking extraction from reasoning_content / reasoning / thinking fields and inline <think>...</think> tags (DeepSeek / Kimi / Qwen and other providers)
  • Vision — image URL and base64 inputs
  • Auto-retry — exponential backoff with jitter and Retry-After support
  • Token counting — approximate message token estimation
  • Multi-provider — OpenAI, DeepSeek, Kimi, MiniMax, Ollama

§Quick start

use openai_client_rs::{OpenAiClient, ChatMessage};

let client = OpenAiClient::new("sk-xxx", "gpt-4o");
let resp = client.chat_create(
    &[ChatMessage::user("Hello!")],
    None,
).unwrap();
println!("{}", resp.text);

Re-exports§

pub use audio::SpeechRequest;
pub use audio::TranscriptionRequest;
pub use audio::TranscriptionResponse;
pub use error::ApiError;
pub use error::OpenAiError;
pub use error::Result;
pub use files::FileList;
pub use files::FileObject;
pub use images::ImageData;
pub use images::ImageRequest;
pub use images::ImageResponse;
pub use models::ModelList;
pub use request::ChatCompletionRequest;
pub use request::JsonSchemaObject;
pub use request::ResponseFormat;
pub use request::StreamOptions;
pub use retry::RetryConfig;
pub use types::ChatCompletion;
pub use types::ChatCompletionChoice;
pub use types::ChatCompletionChunk;
pub use types::ChatCompletionTokenLogprob;
pub use types::ChatMessage;
pub use types::ChatMessageContent;
pub use types::ChoiceLogprobs;
pub use types::ContentPart;
pub use types::EmbeddingResponse;
pub use types::FunctionCall;
pub use types::ImageUrl;
pub use types::LlmResponse;
pub use types::ModelInfo;
pub use types::SimplifiedToolCall;
pub use types::Tool;
pub use types::ToolCall;
pub use types::ToolChoice;
pub use types::ToolFunction;
pub use types::TopLogprob;
pub use types::Usage;

Modules§

api_common
Shared API logic used by both sync and async clients.
audio
Audio API — TTS (text-to-speech) and STT (speech-to-text).
error
files
Files API — upload, list, retrieve, delete files.
images
Images API — DALL-E image generation, editing, variations.
models
Models API — list and retrieve model information.
request
Request builder for OpenAI Chat Completions API.
retry
Automatic retry with exponential backoff.
sse
Minimal Server-Sent-Events (SSE) line framing, spec-compliant.
thinking
Thinking / reasoning extraction for OpenAI-compatible streams.
tokens
Token counting utility.
types
Shared types for the OpenAI API: chat messages, tool calls, completions, etc.

Structs§

ChatChunkStream
Iterator over raw, typed ChatCompletionChunks from a streamed response.
OpenAiClient
An OpenAI-compatible API client.
StreamHandler

Functions§

parse_openai_stream
Parse an OpenAI-style SSE chat stream from any reader.
parse_openai_stream_rich
Parse an OpenAI-style SSE chat stream with an optional-callback StreamHandler.