openai-api-rs for Rust: Complete Guide with Code Examples
The openai-api-rs library is a modern, asynchronous, and type-safe client for the OpenAI API, written in Rust. It provides developers with a clean interface to interact with GPT models (including GPT-4 and GPT-3.5), DALL-E (image generation), Whisper (speech recognition), as well as embeddings, content moderation, and file management. The library fully supports streaming responses, which is critical for building interactive chat applications and low-latency assistants.
Why use Rust for the OpenAI API? Rust delivers top-tier performance and memory safety without a garbage collector. This makes openai-api-rs an ideal choice for high-load server applications, Telegram/Discord bots, real-time natural language processing systems, and embedded devices. Thanks to Rust's strict type system, many errors (like malformed request payloads) are caught at compile time rather than at runtime. The library is built on top of reqwest and tokio, ensuring efficient asynchronous I/O operations.
On the PythonLib educational site, we explore this library to demonstrate how the Rust ecosystem can be leveraged for machine learning and NLP tasks. Even if you primarily write Python, understanding Rust clients will give you deeper insight into how API requests work internally and how streaming data is processed. The library is actively maintained by the community, comes with thorough documentation, and is well-tested.
Installation
To add the openai-api-rs library to your Rust project, run the following command in your project's root directory:
cargo add openai-api-rs
This command adds the latest version to your Cargo.toml file. If you prefer manual editing, add this line under [dependencies]:
[dependencies]
openai-api-rs = "0.2" # Check crates.io for the latest version
You'll also need an async runtime. We recommend using tokio:
cargo add tokio --features full
Don't forget to set the OPENAI_API_KEY environment variable with your OpenAI API key. You can do this in your terminal:
export OPENAI_API_KEY="sk-your_key_here"
Alternatively, you can pass the key directly in code (not recommended for production).
Quick Start
A minimal working example: sending a simple request to the GPT-3.5 model and printing the response to the console.
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::chat_completion::{ChatCompletionRequest, Message};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client using the API key from the environment variable
let client = Client::new(env::var("OPENAI_API_KEY")?);
// Build the request
let request = ChatCompletionRequest::new(
"gpt-3.5-turbo".to_string(),
vec![Message {
role: "user".to_string(),
content: "Write a short poem about Rust".to_string(),
}],
);
// Send the request and get the response
let response = client.chat_completion(request).await?;
// Extract the response text
if let Some(choice) = response.choices.first() {
println!("Response: {}", choice.message.content);
}
Ok(())
}
Run the program with cargo run. You should see a generated poem. This example demonstrates the basic workflow: creating a client, building a request, and handling the response.