OpenAI Interface
A low-level Rust interface for interacting with OpenAI's API. Both streaming and non-streaming APIs are supported.
Currently, chat completions (create / retrieve / update / delete), completions, models, embeddings, moderations, file management (upload / list / retrieve / delete / download content), images (generate / edit / variation), audio (speech / transcriptions / translations), the Responses API (create / retrieve / delete / input items / cancel), batches, uploads, fine-tuning jobs, vector stores, containers, conversations, evals, and realtime session creation are supported. See the support matrix below for details.
Repository:
Codeberg: Codeberg Repo
GitCode: GitCode RepoYou are welcome to contribute to this project through any of the links above.
Features
- Chat Completions: Full support for OpenAI's chat completion and completion API, including both streaming and non-streaming responses, and multimodal user messages (text / image / audio / file content parts).
- Models: List, retrieve and delete models.
- Embeddings: Create embedding vectors from text input.
- Moderations: Classify whether text and/or image input is potentially harmful (untested).
- Images: Generate, edit, and create variations of images (untested).
- Audio: Text-to-speech, transcription, and translation endpoints (untested).
- Files: Support for the OpenAI file API (create / list / retrieve / delete / download content).
- Responses API: Create, retrieve and delete responses, list their input items, and cancel background responses, including streaming events.
- Batches: Create, retrieve, list and cancel async batch processing jobs.
- Uploads: Multi-part upload sessions for large files (create / add parts / complete / cancel).
- Fine-tuning: Create, retrieve, list and cancel fine-tuning jobs, list their events and checkpoints, and restore fine-tuned models.
- Vector Stores: Manage vector stores and their files (including file
batches and semantic search) for the
file_searchtool. - Containers: Manage containers and their files for the Code Interpreter tool.
- Conversations: Manage the stateful conversation layer of the Responses API and its items.
- Evals: Manage evals, their runs and the runs' output items.
- Realtime: Create ephemeral Realtime and transcription session tokens (the WebSocket transport itself is not implemented).
- Streaming and Non-streaming: Support for both streaming and non-streaming responses.
- Reasoning Effort: The OpenAI-compatible
reasoning_effortparameter is supported out of the box for reasoning models. - Configurable HTTP Client: Every request method takes a
reqwest::Client, so proxies, timeouts and connection pooling are under your control. - Strong Typing: Complete type definitions for all API requests and responses, utilizing Rust's powerful type system.
- Error Handling: Comprehensive error handling with detailed error types defined in
the [
errors] module. Failed requests carry the API's error message, type and code. - Async/Await: Built with async/await support.
- Musl Support: Designed to work with musl libc out-of-the-box.
- Multiple Provider Support: Expected to work with OpenAI, DeepSeek, Qwen, and other compatible API providers. Provider-specific fields are opt-in via cargo features (see below).
Installation
[!WARNING] Versions prior to 0.3.0 have serious issues with SSE streaming responses processing: instead of a single chunk, multiple chunks may be returned in each iteration of the response stream.
Add this to your Cargo.toml:
[]
= { = "0.8.1", = ["deepseek", "qwen"] }
Cargo Features
To keep the request and response types strictly OpenAI-compatible, fields that are proprietary to other providers are opt-in via cargo features:
deepseek: Enables DeepSeek's proprietary fields — the Beta chat prefix completion fields (prefix/reasoning_contenton assistant messages), thethinkinganduser_idrequest parameters,reasoning_contentin responses and logprobs,prompt_cache_hit_tokens/prompt_cache_miss_tokensusage statistics, and theinsufficient_system_resourcefinish reason. See api-docs.deepseek.com.qwen: Enables Qwen's proprietary request parameters (enable_thinking,thinking_budget,top_k) as direct fields of the chat request body. See the Qwen OpenAI-compatible Chat API docs.
Usage
Chat Completion
This crate provides methods for both streaming and non-streaming chat completions. The following examples demonstrate how to use these features.
Non-streaming Chat Completion
use ;
use ChatCompletion;
use ;
async
Streaming Chat Completion
This example demonstrates how to handle streaming responses from the API.
get_stream_response deserializes every server-sent event and stops
automatically at the data: [DONE] sentinel.
use ;
use ChatCompletionChunk;
use ;
use StreamExt;
async
Configuring the HTTP Client
Every request method takes the client as its first argument. Pass a custom client to use a proxy or a different timeout:
let client = builder
.proxy
.timeout
.build?;
Custom Request Parameters
For provider-specific parameters, prefer enabling the matching cargo feature
(deepseek or qwen) so the fields are available as typed members of the
request structs.
If you need a field that is not covered by the typed structs, you can inject arbitrary JSON properties:
- On the chat
RequestBody, useextra_body_map(Option<serde_json::Map<String, serde_json::Value>>, flattened into the request body). - On the
CompletionRequestandCreateFileRequest, useextra_body.
use ;
use json;
let request = RequestBody ;
Modules
- [
chat]: Contains all chat completion related structs, enums, and methods. - [
completions]: Contains all completion related structs, enums, and methods. Note that this API is getting deprecated in favour ofchatand is only available for out-dated LLM models. - [
models]: List, retrieve and delete models. - [
embeddings]: Create embedding vectors from text input. - [
moderations]: Classify whether text input is potentially harmful. - [
images]: Generate, edit, and create variations of images. - [
audio]: Turn audio into text (transcriptions / translations) or text into audio (speech). - [
files]: Providing the capacity to upload and manage files. - [
rest]: Providing all REST related traits and methods, plusdefault_clientand shared status/error handling. - [
errors]: Defines error types used throughout the crate. - [
pagination]: Shared cursor-pagination types (Page,PaginationQuery) used by the list endpoints.
API Support Matrix
All newly added modules are untested against a live OpenAI API (no API key was available); they follow the official documentation and should work with OpenAI-compatible providers that implement the same endpoints. Please report any issues on the repository.
| API group | Endpoints | Status |
|---|---|---|
| Chat Completions | create / retrieve / update / delete | tested |
| Completions (legacy) | create | tested |
| Models | list / retrieve / delete | tested |
| Embeddings | create | tested |
| Moderations | create | untested |
| Images | generate / edit / variation | untested |
| Audio | speech / transcriptions / translations | untested |
| Files | create / list / retrieve / delete / content | tested |
| Responses | create / retrieve / delete / input items / cancel | partially tested |
| Batches | create / retrieve / list / cancel | untested |
| Uploads | create / add part / complete / cancel | untested |
| Fine-tuning | jobs create / retrieve / list / cancel, events, checkpoints, model restore | untested |
| Vector Stores | create / retrieve / update / delete / list, files CRUD + content, search, file batches | untested |
| Containers | create / retrieve / delete / list, files CRUD + content | untested |
| Conversations | create / retrieve / update / delete / list, items CRUD + list | untested |
| Evals | create / retrieve / update / delete / list, runs CRUD + cancel, output items | untested |
| Realtime | sessions / transcription sessions (HTTP only; WebSocket not implemented) | untested |
Not implemented: the Realtime WebSocket transport, the evals alpha
permissions endpoints (/fine_tuning/alpha/permissions), and streaming
variants of the images and audio transcription endpoints.
Error Handling
All errors are converted into [errors::OapiError]. On a failed request the
response body is parsed into [errors::ApiError], which carries the API's
error message, type, code and the HTTP status, so failures can be
diagnosed without re-sending the request.
Musl Build
This crate is designed to work with musl libc, making it suitable for lightweight deployments in containerized environments. Longer compile times may be required as OpenSSL needs to be built from source.
To build for musl:
Supported Providers
This crate aims to support standard OpenAI-compatible API endpoints. Unfortunately, OpenAI aggressively restricts the access from the People's Republic of China. As a result, the implementation has been tested primarily with DeepSeek and Qwen. Please open an issue if you find any mistakes or inaccuracies in the implementation.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
License
This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.