OpenAI
The OpenAI provider (rig::providers::openai) gives you completion models, embedding models, and everything built on top of them — agents, extractors, and RAG systems. It also serves as the base client for the many providers that expose an OpenAI-compatible API.
Capabilities
Section titled “Capabilities”| Capability | Support | Model / feature flag |
|---|---|---|
| Completion | Yes | Chat Completions and Responses API — e.g. gpt-5.5, gpt-5-mini |
| Streaming | Yes | Any completion model, via StreamingCompletion |
| Tools / function calling | Yes | Tool-capable models (e.g. gpt-5.5) through the Tool trait |
| Structured output | Yes | Strict JSON-schema outputs via extractors |
| Vision / image input | Yes | Vision-capable models (e.g. gpt-5.5) |
| Image generation | Yes | image feature — dall-e-3, gpt-image-1 |
| Audio / transcription | Yes | audio feature for TTS (tts-1, tts-1-hd); transcription via whisper-1 |
| Embeddings | Yes | text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002 |
Basic usage
Section titled “Basic usage”use rig::prelude::*;use rig::providers::openai;
// Read OPENAI_API_KEY from the environmentlet client = openai::Client::from_env()?;
// A completion model...let gpt4o = client.completion_model("gpt-5.5");
// ...an embedding model...let embedder = client.embedding_model("text-embedding-3-small");
// ...or an agent built straight from the clientlet agent = client .agent("gpt-5.5") .preamble("You are a helpful assistant.") .build();This snippet only constructs the client, models, and agent — it prints nothing on
its own. Once you prompt the agent (agent.prompt("Hello!").await?), a run looks
like this:
Hello! How can I help you today?Configuring the client
Section titled “Configuring the client”Like every Rig provider, the OpenAI client has three constructors:
use rig::prelude::*;use rig::providers::openai;
// From the OPENAI_API_KEY environment variablelet client = openai::Client::from_env()?;
// From an explicit keylet client = openai::Client::new("your-api-key")?;
// From a key and a custom base URL — for a proxy, gateway, or// an OpenAI-compatible endpointlet client = openai::Client::builder() .api_key("your-api-key") .base_url("https://your-gateway.example.com/v1") .build()?;Completion models
Section titled “Completion models”Pass any OpenAI model id as a string to completion_model or agent:
let agent = client.agent("gpt-5-mini").build();The provider module also exports constants for common models (for example
openai::GPT_5_5 and openai::GPT_4). Use whichever you prefer — the string id
and the constant are interchangeable:
use rig::providers::openai;
let gpt4o = client.completion_model(openai::GPT_5_5);Embedding models
Section titled “Embedding models”The provider exports constants for OpenAI’s embedding models along with their output dimensions:
| Constant | Dimensions | Notes |
|---|---|---|
TEXT_EMBEDDING_3_LARGE | 3072 | |
TEXT_EMBEDDING_3_SMALL | 1536 | |
TEXT_EMBEDDING_ADA_002 | 1536 | legacy |
Build an embedding model by constant or by string id:
use rig::prelude::*;use rig::providers::openai;
let client = openai::Client::from_env()?;let embedder = client.embedding_model(openai::TEXT_EMBEDDING_3_SMALL);The reported dimension count matters when you provision a vector store index — it must match the model you embed with. See Embeddings for building embeddings and Vector Stores for wiring them into a store.
Tool calling
Section titled “Tool calling”OpenAI models support function calling. Rig handles the conversion between its
own tool definitions and OpenAI’s expected JSON format for you: any tool you
register on an agent (via the Tool trait) is automatically translated into an
OpenAI function definition when the request is built, and tool-call responses
are parsed back into Rig’s types. You don’t construct the OpenAI-specific
payload by hand — define tools once and they work across providers. See
Tools for how to write and register them.
See also
Section titled “See also”- Model Providers — vocabulary and shared configuration
- Completions and Agents
- Tools and Embeddings
