Skip to content
Get Started

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.

CapabilitySupportModel / feature flag
CompletionYesChat Completions and Responses API — e.g. gpt-5.5, gpt-5-mini
StreamingYesAny completion model, via StreamingCompletion
Tools / function callingYesTool-capable models (e.g. gpt-5.5) through the Tool trait
Structured outputYesStrict JSON-schema outputs via extractors
Vision / image inputYesVision-capable models (e.g. gpt-5.5)
Image generationYesimage feature — dall-e-3, gpt-image-1
Audio / transcriptionYesaudio feature for TTS (tts-1, tts-1-hd); transcription via whisper-1
EmbeddingsYestext-embedding-3-small, text-embedding-3-large, text-embedding-ada-002
use rig::prelude::*;
use rig::providers::openai;
// Read OPENAI_API_KEY from the environment
let 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 client
let 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?

Like every Rig provider, the OpenAI client has three constructors:

use rig::prelude::*;
use rig::providers::openai;
// From the OPENAI_API_KEY environment variable
let client = openai::Client::from_env()?;
// From an explicit key
let client = openai::Client::new("your-api-key")?;
// From a key and a custom base URL — for a proxy, gateway, or
// an OpenAI-compatible endpoint
let client = openai::Client::builder()
.api_key("your-api-key")
.base_url("https://your-gateway.example.com/v1")
.build()?;

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);

The provider exports constants for OpenAI’s embedding models along with their output dimensions:

ConstantDimensionsNotes
TEXT_EMBEDDING_3_LARGE3072
TEXT_EMBEDDING_3_SMALL1536
TEXT_EMBEDDING_ADA_0021536legacy

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.

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.