openai-rust2 1.7.6

An unofficial library for the OpenAI API (OpenAI-compatible endpoints, streaming chat, Responses API, images)
Documentation

openai-rust2

Crates.io docs.rs License: MIT

Unofficial async Rust client for the OpenAI API and OpenAI-compatible endpoints (xAI Grok, Ollama, OpenRouter, self-hosted gateways, …).

This is a maintained fork published as openai-rust2 (original: openai-rust).

Features

  • List models
  • Chat Completions (+ streaming)
  • Completions (legacy)
  • Embeddings
  • Images
  • xAI Agent Tools / Responses API (create_responses)
  • OpenAI Responses API tools (web_search, file_search, code_interpreter)
  • Shared connection-pooled HTTP client + application-level retry on transient failures
  • Audio / Files / Moderations / Fine-tuning

Quick start

use openai_rust2 as openai_rust;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = openai_rust::Client::new(&std::env::var("OPENAI_API_KEY")?);
    let args = openai_rust::chat::ChatArguments::new(
        "gpt-4o-mini",
        vec![openai_rust::chat::Message {
            role: "user".into(),
            content: "Hello!".into(),
        }],
    );
    let res = client.create_chat(args, None).await?;
    println!("{}", res);
    Ok(())
}

Local Ollama (or any OpenAI-compatible base URL):

use openai_rust2 as openai_rust;

let client = openai_rust::Client::new_with_base_url(
    "", // many local servers ignore the key
    "http://localhost:11434",
);

Run the chat example:

OPENAI_API_KEY=sk-... cargo run --example chat

Development

make clippy   # fmt + clippy -D warnings
make test     # unit tests always; live tests skip without OPENAI_API_KEY

Projects using this crate

  • cloudllm — agent toolkit (LLMSession, multi-provider clients, MentisDB)
  • uninews — universal news scraper (via cloudllm)

License

MIT