| OpenAIProvider | R Documentation |
Provider class for OpenAI. Can create language and embedding models.
specification_versionProvider spec version.
new()Initialize the OpenAI provider.
OpenAIProvider$new(
api_key = NULL,
base_url = NULL,
organization = NULL,
project = NULL,
headers = NULL,
name = NULL,
timeout_seconds = NULL,
total_timeout_seconds = NULL,
first_byte_timeout_seconds = NULL,
connect_timeout_seconds = NULL,
idle_timeout_seconds = NULL,
disable_stream_options = FALSE,
api_format = c("auto", "chat", "responses")
)api_keyOpenAI API key. Defaults to OPENAI_API_KEY env var.
base_urlBase URL for API calls. Defaults to https://api.openai.com/v1.
organizationOptional OpenAI organization ID.
projectOptional OpenAI project ID.
headersOptional additional headers.
nameOptional provider name override (for compatible APIs).
timeout_secondsLegacy alias for total_timeout_seconds.
total_timeout_secondsOptional total request timeout in seconds for API calls.
first_byte_timeout_secondsOptional time-to-first-byte timeout in seconds for API calls.
connect_timeout_secondsOptional connection-establishment timeout in seconds for API calls.
idle_timeout_secondsOptional stall timeout in seconds for API calls.
disable_stream_optionsDisable stream_options parameter (for providers that don't support it).
api_formatDefault API surface for smart_model() / model():
"auto" (route reasoning models to Responses, others to Chat — the
canonical OpenAI behavior), "chat" (always Chat Completions — useful
for proxies that don't expose /responses, or that surface reasoning
models like gpt-5.x via /chat/completions), or "responses" (always
Responses API). The explicit language_model() and responses_model()
methods continue to ignore this setting.
language_model()Create a language model (always Chat Completions API).
OpenAIProvider$language_model(model_id = Sys.getenv("OPENAI_MODEL", "gpt-4o"))model_idThe model ID (e.g., "gpt-4o", "gpt-4o-mini").
An OpenAILanguageModel object.
responses_model()Create a language model using the Responses API.
OpenAIProvider$responses_model(model_id)
model_idThe model ID (e.g., "o1", "o3-mini", "gpt-4o").
The Responses API is designed for:
Models with built-in reasoning (o1, o3 series)
Stateful multi-turn conversations (server maintains history)
Advanced features like structured outputs
The model maintains conversation state internally via response IDs.
Call model$reset() to start a fresh conversation.
An OpenAIResponsesLanguageModel object.
model()Default-route factory. Picks chat vs responses based on the
api_format set in create_openai(). Recommended entry point for
callers that don't care which surface is used.
OpenAIProvider$model(model_id = Sys.getenv("OPENAI_MODEL", "gpt-4o"))model_idThe model ID. Defaults to OPENAI_MODEL env var.
A LanguageModelV1 instance.
smart_model()Smart model factory that selects the API surface.
OpenAIProvider$smart_model( model_id, api_format = private$config$api_format %||% "auto" )
model_idThe model ID.
api_formatAPI format: "auto" (default — reasoning → Responses,
others → Chat), "chat", or "responses". Defaults to the
api_format passed to create_openai().
When api_format = "auto", the method picks:
Responses API for reasoning models (o1, o3, gpt-5, ...)
Chat Completions API for everything else
Override per-call when the provider's default doesn't match the specific model you're about to use.
A language model object (either OpenAILanguageModel or OpenAIResponsesLanguageModel).
embedding_model()Create an embedding model.
OpenAIProvider$embedding_model(model_id = "text-embedding-3-small")
model_idThe model ID (e.g., "text-embedding-3-small").
An OpenAIEmbeddingModel object.
image_model()Create an image model.
OpenAIProvider$image_model(
model_id = Sys.getenv("OPENAI_IMAGE_MODEL", "gpt-image-2")
)model_idThe model ID (e.g., "gpt-image-2", "gpt-image-1.5").
An OpenAIImageModel object.
create_conversation()Create a server-side conversation object via
POST /v1/conversations. Returns the parsed response, including the
conversation id you can pass as conversation = "conv_..." to
generate_text() / stream_text() so OpenAI manages the message
history server-side instead of you sending the full transcript each
turn.
OpenAIProvider$create_conversation(items = NULL, metadata = NULL)
itemsOptional list of initial conversation items, each shaped
like list(type = "message", role = "user", content = "Hello!").
metadataOptional named list (up to 16 keys, values stringified).
Parsed response list with at least id, object, created_at,
metadata.
get_conversation()Retrieve a conversation object by id.
OpenAIProvider$get_conversation(conversation_id)
conversation_idConversation id returned from create_conversation().
Parsed response list.
delete_conversation()Delete a conversation object by id. Server-side history is irrecoverable after this call.
OpenAIProvider$delete_conversation(conversation_id)
conversation_idConversation id returned from create_conversation().
Parsed response list (typically list(id, object, deleted)).
clone()The objects of this class are cloneable with this method.
OpenAIProvider$clone(deep = FALSE)
deepWhether to make a deep clone.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.