openai_chatgpt_api — OpenAI API client for Rust
Typed async Rust client for the OpenAI API — Responses API, Chat Completions, Embeddings, Images (gpt-image), Audio (text-to-speech / transcription / translation), Moderations, and Models. Server-sent-event streaming, Structured Outputs (JSON Schema), function calling, and multimodal input included.
- Current API surface (2026): built around the Responses API (
/v1/responses), OpenAI's primary generation API. Retired endpoints (edits, legacy completions, DALL·E variations) are intentionally absent. - Typed end to end: builder-style requests, typed responses and errors — no
serde_json::Valuejuggling for normal use. - Machine-readable summary: see
llms.txtif you are an AI assistant (or feeding one).
| API | Endpoint | Support |
|---|---|---|
| Responses (primary) | /v1/responses |
create / stream / retrieve / delete / cancel |
| Chat Completions | /v1/chat/completions |
create / stream |
| Embeddings | /v1/embeddings |
create |
| Images | /v1/images/generations, /v1/images/edits |
generate / edit |
| Audio | /v1/audio/speech, /v1/audio/transcriptions, /v1/audio/translations |
speech / transcribe / translate |
| Moderations | /v1/moderations |
create |
| Models | /v1/models |
list / retrieve / delete |
Installation
Quick start
use ResponseRequest;
use OpenAiClient;
async
Every example below is self-contained inside that main body. Runnable versions
live in examples/ — try cargo run --example responses.
Which API should I use?
Use the Responses API (client.responses()) for new projects — it is
OpenAI's recommended primary API, with built-in tools and server-side
conversation state. Chat Completions (client.chat()) remains fully
supported for existing message-array codebases.
How to stream output token by token
use StreamExt;
use ;
let request = new;
let mut stream = client.responses.stream.await?;
while let Some = stream.next.await
client.chat().stream(&request) works the same way and yields
ChatCompletionChunks (use chunk.delta_content()).
How to call functions (tools)
use ;
use json;
let tools = vec!;
// 1. The model decides to call your function
let request = new
.tools;
let response = client.responses.create.await?;
// 2. Execute the calls and send the results back
let mut items = Vecnew;
for call in response.function_calls
let follow_up = new
.previous_response_id
.tools;
// 3. The model answers using the results
let final_response = client.responses.create.await?;
println!;
Built-in OpenAI tools work too: Tool::WebSearch, Tool::FileSearch { .. },
Tool::CodeInterpreter { .. }, Tool::ImageGeneration.
How to get structured JSON output (Structured Outputs)
use ResponseRequest;
use json;
let request = new
.json_schema;
let response = client.responses.create.await?;
let person: Value = from_str?;
The same .json_schema(name, schema) shorthand exists on ChatCompletionRequest.
How to send images to the model (vision / multimodal input)
use ;
let request = new;
let response = client.responses.create.await?;
How to continue a conversation
let first = client.responses
.create
.await?;
let second = client.responses
.create
.await?; // "Alice" — history is kept server-side
How to use Chat Completions
use ;
let request = new
.max_completion_tokens; // note: max_tokens is deprecated upstream
let completion = client.chat.create.await?;
println!;
How to create embeddings
use EmbeddingsRequest;
let request = new
.dimensions; // optional truncation
let response = client.embeddings.create.await?;
let vector: & = &response.data.embedding;
How to generate and edit images
use ;
use FilePart;
// Generation — gpt-image models return base64, not URLs
let request = new
.size
.quality;
let images = client.images.generate.await?;
for b64 in images.b64_images
// Editing
let request = new;
let edited = client.images.edit.await?;
How to synthesize speech and transcribe audio
use ;
use FilePart;
// Text to speech (TTS)
let request = new;
let mp3_bytes = client.audio.speech.await?;
write?;
// Speech to text (transcription)
let request = new
.language;
let transcript = client.audio.transcribe.await?;
println!;
// Translate any language to English
let request = new;
let translated = client.audio.translate.await?;
How to moderate content
use ModerationRequest;
let request = new.model;
let moderation = client.moderations.create.await?;
if moderation.results.flagged
How to handle errors
use OpenAiError;
match client.responses.create.await
Client configuration
let client = builder
.organization // OpenAI-Organization header
.project // OpenAI-Project header
.base_url // proxies / gateways / mocks
.http_client // timeouts, proxies, ...
.build;
Model quick reference (July 2026)
Model ids are plain strings — new models work without a crate update. Check the OpenAI model list for the latest.
| Task | Current models |
|---|---|
| Text / reasoning | gpt-5.6-sol (flagship), gpt-5.6-terra (balanced), gpt-5.6-luna (budget) |
| Image generation | gpt-image-2, gpt-image-1-mini |
| Embeddings | text-embedding-3-large, text-embedding-3-small |
| Speech synthesis | gpt-4o-mini-tts, tts-1-hd |
| Transcription | gpt-4o-transcribe, gpt-4o-mini-transcribe, whisper-1 |
| Moderation | omni-moderation-latest |
Testing
OPENAI_API_KEY=sk-...
Migrating from v0.1
v0.2 is a full rewrite for the current OpenAI API. The v0.1 types (ChatGpt,
ChatGptRequestChatCompletions, ChatGptChatFormat, to_value(), ...) no
longer exist — do not use them in new code.
| v0.1 | v0.2 |
|---|---|
ChatGpt::new(key) |
OpenAiClient::new(key) |
gpt.chat_completions(&req) |
client.chat().create(&req) or client.responses().create(&req) |
ChatGptChatFormat::new_user(text) |
ChatMessage::user(text) / InputItem::user(text) |
res.to_value() |
typed fields: response.output_text(), completion.content() |
gpt.edits(..), gpt.completions_create(..), gpt.images_variations(..) |
removed — endpoints retired by OpenAI |