OpenAIProvider: OpenAI Provider Class

OpenAIProviderR Documentation

OpenAI Provider Class

Description

Provider class for OpenAI. Can create language and embedding models.

Public fields

specification_version

Provider spec version.

Methods

Public methods


Method new()

Initialize the OpenAI provider.

Usage
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")
)
Arguments
api_key

OpenAI API key. Defaults to OPENAI_API_KEY env var.

base_url

Base URL for API calls. Defaults to https://api.openai.com/v1.

organization

Optional OpenAI organization ID.

project

Optional OpenAI project ID.

headers

Optional additional headers.

name

Optional provider name override (for compatible APIs).

timeout_seconds

Legacy alias for total_timeout_seconds.

total_timeout_seconds

Optional total request timeout in seconds for API calls.

first_byte_timeout_seconds

Optional time-to-first-byte timeout in seconds for API calls.

connect_timeout_seconds

Optional connection-establishment timeout in seconds for API calls.

idle_timeout_seconds

Optional stall timeout in seconds for API calls.

disable_stream_options

Disable stream_options parameter (for providers that don't support it).

api_format

Default 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.


Method language_model()

Create a language model (always Chat Completions API).

Usage
OpenAIProvider$language_model(model_id = Sys.getenv("OPENAI_MODEL", "gpt-4o"))
Arguments
model_id

The model ID (e.g., "gpt-4o", "gpt-4o-mini").

Returns

An OpenAILanguageModel object.


Method responses_model()

Create a language model using the Responses API.

Usage
OpenAIProvider$responses_model(model_id)
Arguments
model_id

The model ID (e.g., "o1", "o3-mini", "gpt-4o").

Details

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.

Returns

An OpenAIResponsesLanguageModel object.


Method 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.

Usage
OpenAIProvider$model(model_id = Sys.getenv("OPENAI_MODEL", "gpt-4o"))
Arguments
model_id

The model ID. Defaults to OPENAI_MODEL env var.

Returns

A LanguageModelV1 instance.


Method smart_model()

Smart model factory that selects the API surface.

Usage
OpenAIProvider$smart_model(
  model_id,
  api_format = private$config$api_format %||% "auto"
)
Arguments
model_id

The model ID.

api_format

API format: "auto" (default — reasoning → Responses, others → Chat), "chat", or "responses". Defaults to the api_format passed to create_openai().

Details

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.

Returns

A language model object (either OpenAILanguageModel or OpenAIResponsesLanguageModel).


Method embedding_model()

Create an embedding model.

Usage
OpenAIProvider$embedding_model(model_id = "text-embedding-3-small")
Arguments
model_id

The model ID (e.g., "text-embedding-3-small").

Returns

An OpenAIEmbeddingModel object.


Method image_model()

Create an image model.

Usage
OpenAIProvider$image_model(
  model_id = Sys.getenv("OPENAI_IMAGE_MODEL", "gpt-image-2")
)
Arguments
model_id

The model ID (e.g., "gpt-image-2", "gpt-image-1.5").

Returns

An OpenAIImageModel object.


Method 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.

Usage
OpenAIProvider$create_conversation(items = NULL, metadata = NULL)
Arguments
items

Optional list of initial conversation items, each shaped like list(type = "message", role = "user", content = "Hello!").

metadata

Optional named list (up to 16 keys, values stringified).

Returns

Parsed response list with at least id, object, created_at, metadata.


Method get_conversation()

Retrieve a conversation object by id.

Usage
OpenAIProvider$get_conversation(conversation_id)
Arguments
conversation_id

Conversation id returned from create_conversation().

Returns

Parsed response list.


Method delete_conversation()

Delete a conversation object by id. Server-side history is irrecoverable after this call.

Usage
OpenAIProvider$delete_conversation(conversation_id)
Arguments
conversation_id

Conversation id returned from create_conversation().

Returns

Parsed response list (typically list(id, object, deleted)).


Method clone()

The objects of this class are cloneable with this method.

Usage
OpenAIProvider$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


aisdk documentation built on May 29, 2026, 9:07 a.m.