54 releases (19 breaking)

0.20.0 Mar 31, 2024
0.18.3 Feb 6, 2024
0.17.1 Nov 26, 2023
0.12.2 Jul 26, 2023
0.5.0 Dec 30, 2022

#25 in Web programming

Download history 2993/week @ 2023-12-22 3623/week @ 2023-12-29 4910/week @ 2024-01-05 5230/week @ 2024-01-12 4723/week @ 2024-01-19 5835/week @ 2024-01-26 5472/week @ 2024-02-02 5165/week @ 2024-02-09 6481/week @ 2024-02-16 6818/week @ 2024-02-23 6349/week @ 2024-03-01 6881/week @ 2024-03-08 6349/week @ 2024-03-15 5875/week @ 2024-03-22 6232/week @ 2024-03-29 4738/week @ 2024-04-05

24,291 downloads per month
Used in 81 crates (62 directly)

MIT license



Async Rust library for OpenAI

Logo created by this repo itself


async-openai is an unofficial Rust library for OpenAI.

  • It's based on OpenAI OpenAPI spec
  • Current features:
    • Assistants (Beta)
    • Audio
    • Chat
    • Completions (Legacy)
    • Embeddings
    • Files
    • Fine-Tuning
    • Images
    • Microsoft Azure OpenAI Service
    • Models
    • Moderations
    • WASM support (experimental and only available in experiments branch)
  • Support SSE streaming on available APIs
  • All requests including form submissions (except SSE streaming) are retried with exponential backoff when rate limited by the API server.
  • Ergonomic builder pattern for all request objects.

Note on Azure OpenAI Service (AOS): async-openai primarily implements OpenAI spec, and doesn't try to maintain parity with spec of AOS.


The library reads API key from the environment variable OPENAI_API_KEY.

# On macOS/Linux
export OPENAI_API_KEY='sk-...'
# On Windows Powershell

Image Generation Example

use async_openai::{
    types::{CreateImageRequestArgs, ImageSize, ResponseFormat},
use std::error::Error;

async fn main() -> Result<(), Box<dyn Error>> {
    // create client, reads OPENAI_API_KEY environment variable for API key.
    let client = Client::new();

    let request = CreateImageRequestArgs::default()
        .prompt("cats on sofa and carpet in living room")

    let response = client.images().create(request).await?;

    // Download and save images to ./data directory.
    // Each url is downloaded and saved in dedicated Tokio task.
    // Directory is created if it doesn't exist.
    let paths = response.save("./data").await?;

        .for_each(|path| println!("Image file path: {}", path.display()));


Scaled up for README, actual size 256x256


Thank you for taking the time to contribute and improve the project. I'd be happy to have you!

All forms of contributions, such as new features requests, bug fixes, issues, documentation, testing, comments, examples etc. are welcome.

A good starting point would be to look at existing open issues.

To maintain quality of the project, a minimum of the following is a must for code contribution:

  • Names & Documentation: All struct names, field names and doc comments are from OpenAPI spec. Nested objects in spec without names leaves room for making appropriate name.
  • Tested: Examples are primary means of testing and should continue to work. For new features supporting example is required.
  • Scope: Keep scope limited to APIs available in official documents such as API Reference or OpenAPI spec. Other LLMs or AI Providers offer OpenAI-compatible APIs, yet they may not always have full parity. In such cases, the OpenAI spec takes precedence.
  • Consistency: Keep code style consistent across all the "APIs" that library exposes; it creates a great developer experience.

This project adheres to Rust Code of Conduct

Complimentary Crates

  • openai-func-enums provides procedural macros that make it easier to use this library with OpenAI API's tool calling feature. It also provides derive macros you can add to existing clap application subcommands for natural language use of command line tools. It also supports openai's parallel tool calls and allows you to choose between running multiple tool calls concurrently or own their own OS threads.


This project is licensed under MIT license.


~367K SLoC