OpenAI Tools
API Wrapper for OpenAI API.
Installation
To start using the openai-tools, add it to your projects's dependencies in the `Cargo.toml' file:
Quick Example
use ChatCompletion;
use ;
async
Environment Setup
OpenAI API
Set the API key in the .env file:
OPENAI_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Azure OpenAI API
Set Azure-specific environment variables:
AZURE_OPENAI_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
AZURE_OPENAI_BASE_URL = "https://my-resource.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
Note: Each API (Chat, Embedding, etc.) requires its own complete endpoint URL including the API path.
Provider Detection
All API clients support multiple ways to configure authentication:
use ChatCompletion;
use ;
// OpenAI (default)
let chat = new;
// Azure (from environment variables)
let chat = azure?;
// Auto-detect provider from environment variables
let chat = detect_provider?;
// URL-based detection (auto-detects provider from URL pattern)
// *.openai.azure.com → Azure, all other URLs → OpenAI-compatible
let chat = with_url;
// OpenAI-compatible APIs (Ollama, vLLM, LocalAI, etc.)
let chat = with_url;
// Explicit Azure auth configuration
let auth = Azure;
let chat = with_auth;
Modules
Import the necessary modules in your code:
use ChatCompletion;
use Responses;
use Embedding;
use RealtimeClient;
use Conversations;
use Models;
use Files;
use Moderations;
use Images;
use Audio;
use Batches;
use FineTuning;
use Videos;
Supported APIs
| API | Endpoint | Features |
|---|---|---|
| Chat | /v1/chat/completions |
Structured Output, Function Calling, Multi-modal Input (Text + Image), Safety Identifier |
| Responses | /v1/responses |
CRUD, Structured Output, Function Calling, Image Input, Reasoning, Tool Choice, Prompt Templates, Safety Identifier |
| Conversations | /v1/conversations |
CRUD |
| Embedding | /v1/embeddings |
Basic |
| Realtime | wss://api.openai.com/v1/realtime |
Function Calling, Audio I/O, VAD, WebSocket |
| Models | /v1/models |
CRUD |
| Files | /v1/files |
CRUD, Multipart Upload |
| Moderations | /v1/moderations |
Basic |
| Images | /v1/images |
Generate, Edit, Variations, Multipart Upload |
| Audio | /v1/audio |
Audio I/O, Multipart Upload |
| Batch | /v1/batches |
CRUD |
| Fine-tuning | /v1/fine_tuning/jobs |
CRUD |
| Videos | /v1/videos |
CRUD, Content Download, Remix |
Type-Safe Model Selection
Models are selected through enums, so a typo is a compile error rather than a
runtime model_not_found.
| Enum | API | Variants |
|---|---|---|
ChatModel |
Chat, Responses | GPT-5.6: Gpt5_6 (alias to Sol), Gpt5_6Sol, Gpt5_6Terra, Gpt5_6LunaGPT-5.5: Gpt5_5, Gpt5_5ProGPT-5.4: Gpt5_4, Gpt5_4Pro, Gpt5_4Mini, Gpt5_4NanoGPT-5.3: Gpt5_3ChatLatestCodex: Gpt5_3Codex, Gpt5_2Codex, Gpt5_1Codex, Gpt5_1CodexMaxGPT-5: Gpt5, Gpt5Pro, Gpt5_2, Gpt5_2ChatLatest, Gpt5_2Pro, Gpt5_1, Gpt5_1ChatLatest, Gpt5Mini, Gpt5NanoSearch: Gpt5SearchApi, Gpt4oSearchPreview, Gpt4oMiniSearchPreviewAudio: GptAudio, GptAudio1_5, GptAudioMiniGPT-4.1 / 4o / 4 / 3.5: Gpt4_1, Gpt4_1Mini, Gpt4_1Nano, Gpt4o, Gpt4oMini, Gpt4oAudioPreview, Gpt4Turbo, Gpt4, Gpt3_5Turbo, Gpt3_5Turbo16ko-series: O1, O1Pro, O3, O3Pro, O3Mini, O4MiniCustom(String) |
EmbeddingModel |
Embedding | TextEmbedding3Small, TextEmbedding3Large, TextEmbeddingAda002 |
RealtimeModel |
Realtime | GptRealtime2_1, GptRealtime2_1Mini, GptRealtime2, GptRealtime1_5, GptRealtime, GptRealtimeMini, GptRealtimeTranslate, GptRealtime_2025_08_28 (default), Custom(String) |
ImageModel |
Images | GptImage1 (default), GptImage1Mini, GptImage1_5, GptImage2, ChatGptImageLatest, DallE2, DallE3 |
VideoModel |
Videos | Sora2 (default), Sora2Pro |
TtsModel |
Audio | Tts1, Tts1Hd, Tts1_1106, Tts1Hd1106, Gpt4oMiniTts |
SttModel |
Audio | Whisper1, Gpt4oTranscribe, Gpt4oMiniTranscribe, Gpt4oTranscribeDiarize, GptTranscribe, GptLiveTranscribe, GptRealtimeWhisper |
FineTuningModel |
Fine-tuning | Gpt41_2025_04_14, Gpt41Mini_2025_04_14, Gpt41Nano_2025_04_14, Gpt4o_2024_08_06, Gpt4oMini_2024_07_18, Babbage002, Davinci002, etc. |
use ChatModel;
chat.model; // Latest flagship
chat.model; // Most cost-efficient
responses.model; // Responses API only
chat.model; // Fine-tuned
Parameter Restrictions by Model Family
The library validates parameters per model and drops unsupported ones with a
tracing::warn! instead of letting the API reject the request.
| Family | temperature / top_p / n / penalties |
reasoning |
Detect with |
|---|---|---|---|
Standard (GPT-4o, GPT-4.1, gpt-audio, ...) |
Yes | No | - |
Reasoning (GPT-5.x, *-codex, o-series) |
No (fixed) | Yes | is_reasoning_model() |
Search (gpt-5-search-api, gpt-4o-*-search-preview) |
No (fixed) | No | is_search_model() |
Two things are easy to get wrong here:
- The
*-chat-latestaliases (gpt-5.1-chat-latest,gpt-5.2-chat-latest,gpt-5.3-chat-latest) point at the non-reasoning "Instant" snapshots, so they accept the full standard parameter set despite thegpt-5prefix. - Search models reject the same parameters as reasoning models but expose no
reasoningparameter, so they are a separate category.
Responses API only (no Chat Completions endpoint): gpt-5.5-pro,
gpt-5.4-pro, gpt-5.2-pro, gpt-5-pro, o3-pro, and every *-codex model.
Chat Completions API
use ChatCompletion;
use ;
async
Multi-modal Input (Text + Image)
use ChatCompletion;
use ;
async
Safety Identifier
Track end users for abuse detection with safety_identifier (successor to the legacy user parameter):
use ChatCompletion;
use ;
async
Responses API
use Responses;
use ChatModel;
async
Responses API CRUD Operations
use ;
use ChatModel;
async
Conversations API
Manage long-running conversations with the Responses API:
use Conversations;
use InputItem;
use HashMap;
async
Realtime API
Real-time audio and text communication through WebSocket:
use ;
use ServerEvent;
async
Models API
List and retrieve available models:
use Models;
async
Files API
Upload, manage, and retrieve files:
use Files;
use FilePurpose;
async
Moderations API
Check content for policy violations:
use Moderations;
async
Images API
Generate images with the GPT Image models:
use ;
async
DALL-E has been retired.
dall-e-2anddall-e-3no longer exist onapi.openai.com- requests naming them fail withThe model 'dall-e-3' does not exist.TheDallE2/DallE3variants are kept because Azure OpenAI deployments can still serve them, andImageModel::default()is nowGptImage1.
The GPT Image models differ from DALL-E in three ways:
| DALL-E 3 | GPT Image | |
|---|---|---|
quality |
Standard, Hd |
Low, Medium, High, Auto |
size |
1024x1024, 1792x1024, 1024x1792 |
1024x1024, 1024x1536, 1536x1024, Auto |
response_format / style |
Supported | Rejected (unknown_parameter); always base64 |
Audio API
Text-to-speech and transcription:
use ;
async
Batch API
Process large volumes of requests asynchronously with 50% cost savings:
use ;
async
Fine-tuning API
Customize models with your training data:
use ;
use Hyperparameters;
async
Videos API (Sora)
Generate video clips with Sora. Generation is asynchronous: create queues the
job and returns immediately, so poll until it settles before downloading.
use ;
async
Other operations:
use ;
async
Cost: video generation is billed per second of output and is far more expensive than a text completion.
VideoSecondsallows4,8and12.
Embedding API
use Embedding;
use EmbeddingModel;
async
Update History
Breaking changes
- Model enums (
ChatModel,RealtimeModel,ImageModel,ImageSize,ImageQuality,SttModel,TtsModel,FineTuningModel) gained new variants. These enums are not#[non_exhaustive], so exhaustivematchexpressions downstream will no longer compile - add a_ =>arm or handle the new variants. ImageModel::default()changed fromDallE3toGptImage1. OpenAI retireddall-e-2/dall-e-3onapi.openai.com, so the old default no longer resolves. The variants remain for Azure OpenAI deployments.gpt-5.1-chat-latestandgpt-5.2-chat-latestare no longer classified as reasoning models. Previouslytemperature,n,logprobsand the penalties were silently dropped for them; they are now sent through. If you relied on that filtering, the values now reach the API.Functionno longer serializesargumentsas a nested object. It is emitted once, as the JSON-encoded string the API schema requires.
New: Videos API (/v1/videos)
- Added the
videosmodule:Videosclient with create, retrieve, list, delete, content download and remix VideoModel(Sora2,Sora2Pro),VideoSize,VideoSeconds,VideoVariant,InputReference,CreateVideoOptions,SortOrderVideo,VideoStatus,VideoError,VideoListResponse,DeleteVideoResponse- Unknown lifecycle states deserialize into
VideoStatus::Otherrather than failing
New models
ChatModel: GPT-5.6 (Gpt5_6,Gpt5_6Sol,Gpt5_6Terra,Gpt5_6Luna), GPT-5.5 (Gpt5_5,Gpt5_5Pro), GPT-5.4 (Gpt5_4,Gpt5_4Pro,Gpt5_4Mini,Gpt5_4Nano),Gpt5_3ChatLatest, Codex (Gpt5_3Codex,Gpt5_2Codex,Gpt5_1Codex),Gpt5,Gpt5Pro,O3Pro, search models (Gpt5SearchApi,Gpt4oSearchPreview,Gpt4oMiniSearchPreview), audio chat models (GptAudio,GptAudio1_5,GptAudioMini),Gpt3_5Turbo16kRealtimeModel:GptRealtime2_1,GptRealtime2_1Mini,GptRealtime2,GptRealtime1_5,GptRealtime,GptRealtimeMini,GptRealtimeTranslateImageModel:GptImage1Mini,GptImage1_5,GptImage2,ChatGptImageLatestImageSize:Size1024x1536,Size1536x1024,Auto;ImageQuality:Low,Medium,High,AutoSttModel:GptTranscribe,GptLiveTranscribe,GptRealtimeWhisper,Gpt4oMiniTranscribe,Gpt4oTranscribeDiarizeTtsModel:Tts1_1106,Tts1Hd1106;FineTuningModel:Babbage002,Davinci002
Fixes
- Fix:
Functionserializedargumentstwice (once as an object, once as a JSON string), which the API rejects withduplicate JSON key 'arguments'. It is now emitted once, as the JSON string the schema requires. This broke multi-turn function calling. - Fix:
gpt-5.1-chat-latestandgpt-5.2-chat-latestwere classified as reasoning models, sotemperature,n,logprobsand the penalties were silently dropped. They point at the non-reasoning "Instant" snapshots and now accept the full standard parameter set. - Fix:
ImageModel::default()wasDallE3, which OpenAI has retired; it is nowGptImage1. TheDallE2/DallE3variants remain for Azure deployments.
Other
- Added
ChatModel::is_search_model()andParameterSupport::search_model()for the web-search models, which reject the sampling parameters like reasoning models but expose noreasoningparameter - Added
SttModel::supports_file_transcription()-gpt-live-transcribeandgpt-realtime-whisperare realtime-only and are rejected by/v1/audio/transcriptions - Fixed the
Modulesexample, which used module paths that do not exist (openai_tools::chat::ChatCompletioninstead ofopenai_tools::chat::request::ChatCompletion)
- Removed deprecated GPT-4o Realtime and Audio Preview model variants (confirmed shutdown March 24, 2026)
RealtimeModel: RemovedGpt4oRealtimePreview,Gpt4oMiniRealtimePreview. AddedGptRealtime_2025_08_28as defaultTranscriptionModel(Realtime): RemovedGpt4oTranscribe,Gpt4oMiniTranscribe,Gpt4oTranscribeDiarize. OnlyWhisper1remains
- GPT-4o base models (
Gpt4o,Gpt4oMini,Gpt4oAudioPreview) remain available inChatModel - GPT-4o fine-tuning models (
Gpt4oMini_2024_07_18,Gpt4o_2024_08_06) remain available inFineTuningModel - GPT-4o audio models (
Gpt4oMiniTts,Gpt4oTranscribe) remain available inTtsModelandSttModel - Fix:
Response.incomplete_detailschanged fromOption<String>toOption<Value> - Fix:
Response.errorchanged fromOption<String>toOption<Value> - Updated documentation to reflect current model availability
- Added
safety_identifierparameter to Chat Completions API- Successor to the legacy
userparameter for end-user abuse detection - Available via
ChatCompletion::safety_identifier()builder method - Also improves cache hit rates when set
- Successor to the legacy
- Note: Images API does not support
safety_identifier(useuserfield instead)
- Fixed Chat Completions API multimodal message serialization
Contenttype was sending Responses API format (input_text,input_image) to Chat API- Chat API requires
textandimage_urltype names with nested{"url": "..."}structure - Added zero-copy serialization wrappers that automatically convert at request time
- No public API changes - existing code works without modification
- Added
instructionsparameter for TTS API- Control voice tone, emotion, and pacing with natural language instructions
- Available via
TtsOptions.instructionsfield
- Applied cargo fmt formatting
- Added 88 comprehensive model-specific parameter validation tests
- Chat API: 30 tests for parameter restrictions across model generations (GPT-5, o-series, standard models)
- Responses API: 32 tests for temperature, top_p, top_logprobs validation
- Models: 26 tests for reasoning model detection and
ParameterSupport/ParameterRestrictiontypes
- Fixed Responses API integration test assertions (max_output_tokens minimum, JSON formatting)
- Updated documentation to recommend
cargo nextest runfor test execution
- Breaking Change: Simplified
AzureAuthto accept complete endpoint URLAzureAuth::new(api_key, base_url)- simple 2-argument constructorbase_urlmust be the complete endpoint URL including API path (e.g.,/chat/completions)endpoint()method now returnsbase_urlas-is (path parameter is ignored)- Removed
resource_name,deployment_name,api_versionfields - Removed
use_entra_id,with_entra_id(),is_entra_id()(Entra ID support removed)
- Breaking Change: Updated
with_url()method signature- Changed from
with_url(url, api_key, deployment_name)towith_url(url, api_key)
- Changed from
- Environment variable changes:
- Use
AZURE_OPENAI_BASE_URL(complete endpoint URL) instead of separate resource/deployment vars - Removed
AZURE_OPENAI_TOKEN(Entra ID token support removed)
- Use
- Added URL-based provider detection for all API clients
with_url(url, api_key, deployment_name)- auto-detect provider from URL patternfrom_url(url)- auto-detect with env var credentials*.openai.azure.com→ Azure, all other URLs → OpenAI-compatible
- Support for OpenAI-compatible APIs (Ollama, vLLM, LocalAI, etc.)
- Added Azure OpenAI support with
azure()and environment variable configuration - Added
AuthProviderabstraction for unified authentication handling
- Added automatic handling for reasoning model (o1, o3 series) parameter restrictions
- Chat API: temperature, frequency_penalty, presence_penalty, logprobs, top_logprobs, logit_bias, n
- Responses API: temperature, top_p, top_logprobs
- Unsupported parameters are automatically ignored with
tracing::warn!warnings - Added "Model-Specific Parameter Restrictions" documentation section
- Initial release with all OpenAI APIs:
- Chat Completions API
- Responses API
- Conversations API
- Embedding API
- Realtime API (WebSocket)
- Models API
- Files API
- Moderations API
- Images API (DALL-E)
- Audio API (TTS, STT)
- Batch API
- Fine-tuning API
License
MIT License
This file was generated by Claude Code.