Skip to main content

Crate openai_chatgpt_api

Crate openai_chatgpt_api 

Source
Expand description

Typed async Rust client for the OpenAI API.

Covers the current (2026) API surface:

ModuleEndpoint
responses/v1/responses — primary generation API (text, tools, reasoning, streaming)
chat/v1/chat/completions — Chat Completions (incl. streaming)
embeddings/v1/embeddings
images/v1/images/generations, /v1/images/edits
audio/v1/audio/speech, /v1/audio/transcriptions, /v1/audio/translations
moderations/v1/moderations
models/v1/models

§Quick start

use openai_chatgpt_api::OpenAiClient;
use openai_chatgpt_api::responses::ResponseRequest;

#[tokio::main]
async fn main() -> Result<(), openai_chatgpt_api::OpenAiError> {
    let client = OpenAiClient::from_env()?; // OPENAI_API_KEY
    let request = ResponseRequest::new("gpt-5.6-terra", "Explain ownership in Rust.")
        .instructions("You are a concise assistant.")
        .max_output_tokens(500);
    let response = client.responses().create(&request).await?;
    println!("{}", response.output_text());
    Ok(())
}

§Streaming

use futures_util::StreamExt;
use openai_chatgpt_api::OpenAiClient;
use openai_chatgpt_api::responses::{ResponseRequest, ResponseStreamEvent};

let client = OpenAiClient::from_env()?;
let request = ResponseRequest::new("gpt-5.6-luna", "Write a haiku.");
let mut stream = client.responses().stream(&request).await?;
while let Some(event) = stream.next().await {
    if let ResponseStreamEvent::OutputTextDelta { delta, .. } = event? {
        print!("{delta}");
    }
}

Model names are passed as plain strings: the API’s model lineup changes faster than any type system should chase. See https://platform.openai.com/docs/models for the current list.

Modules§

audio
Audio APIs: speech synthesis (/v1/audio/speech), transcription (/v1/audio/transcriptions), and translation (/v1/audio/translations).
chat
Chat Completions API (/v1/chat/completions).
embeddings
Embeddings API (/v1/embeddings).
images
Images API (/v1/images/generations, /v1/images/edits).
models
Models API (/v1/models).
moderations
Moderations API (/v1/moderations).
responses
Responses API (/v1/responses) — OpenAI’s primary generation API.

Structs§

DeletedObject
Result of a DELETE call (responses, fine-tuned models).
FilePart
An in-memory file attached to a multipart request (audio and image endpoints).
OpenAiClient
Asynchronous OpenAI API client.
OpenAiClientBuilder
Builder for OpenAiClient.

Enums§

OpenAiError
Errors returned by crate::OpenAiClient.

Constants§

DEFAULT_BASE_URL
Default API endpoint.

Type Aliases§

EventStream
A stream of parsed server-sent events returned by the streaming endpoints.