The OpenAI Ruby library provides convenient access to the OpenAI REST API from any Ruby 3.1.0+ application.
Documentation for releases of this gem can be found on RubyDoc.
The REST API documentation can be found on platform.openai.com.
ℹ️ The openai
gem is not yet available on rubygems.org.
To use this gem, install via Bundler by adding the following to your application's Gemfile
:
gem "openai", github: "openai/openai-ruby", branch: "main"
require "bundler/setup"
require "openai"
openai = OpenAI::Client.new(
api_key: "My API Key" # defaults to ENV["OPENAI_API_KEY"]
)
chat_completion = openai.chat.completions.create(
messages: [{role: :user, content: "Say this is a test"}],
model: :"gpt-4.1"
)
puts(chat_completion)
This library is written with Sorbet type definitions. However, there is no runtime dependency on the sorbet-runtime
.
When using sorbet, it is recommended to use model classes as below. This provides stronger type checking and tooling integration.
openai.chat.completions.create(
messages: [OpenAI::Models::Chat::ChatCompletionUserMessageParam.new(role: :user, content: "Say this is a test")],
model: :"gpt-4.1"
)
List methods in the OpenAI API are paginated.
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
page = openai.fine_tuning.jobs.list(limit: 20)
# Fetch single item from page.
job = page.data[0]
puts(job.id)
# Automatically fetches more pages as needed.
page.auto_paging_each do |job|
puts(job.id)
end
We provide support for streaming responses using Server-Sent Events (SSE).
coming soon: openai.chat.completions.stream
will soon come with Python SDK style higher level streaming responses support.
stream = openai.chat.completions.stream_raw(
messages: [{role: :user, content: "Say this is a test"}],
model: :"gpt-4.1"
)
stream.each do |completion|
print(completion.choices.first.delta.content)
end
Request parameters that correspond to file uploads can be passed as StringIO
, or a Pathname
instance.
require "pathname"
# using `Pathname`, the file will be lazily read, without reading everything in to memory
file_object = openai.files.create(file: Pathname("input.jsonl"), purpose: :"fine-tune")
file = File.read("input.jsonl")
# using `StringIO`, useful if you already have the data in memory
file_object = openai.files.create(file: StringIO.new(file), purpose: :"fine-tune")
puts(file_object.id)
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of OpenAI::Errors::APIError
will be thrown:
begin
job = openai.fine_tuning.jobs.create(model: :"babbage-002", training_file: "file-abc123")
rescue OpenAI::Errors::APIError => e
puts(e.status) # 400
end
Error codes are as followed:
Cause | Error Type |
---|---|
HTTP 400 | BadRequestError |
HTTP 401 | AuthenticationError |
HTTP 403 | PermissionDeniedError |
HTTP 404 | NotFoundError |
HTTP 409 | ConflictError |
HTTP 422 | UnprocessableEntityError |
HTTP 429 | RateLimitError |
HTTP >= 500 | InternalServerError |
Other HTTP error | APIStatusError |
Timeout | APITimeoutError |
Network error | APIConnectionError |
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.
You can use the max_retries
option to configure or disable this:
# Configure the default for all requests:
openai = OpenAI::Client.new(
max_retries: 0 # default is 2
)
# Or, configure per-request:
openai.chat.completions.create(
messages: [{role: :user, content: "How can I get the name of the current day in JavaScript?"}],
model: :"gpt-4.1",
request_options: {max_retries: 5}
)
By default, requests will time out after 600 seconds.
Timeouts are applied separately to the initial connection and the overall request time, so in some cases a request could wait 2*timeout seconds before it fails.
You can use the timeout
option to configure or disable this:
# Configure the default for all requests:
openai = OpenAI::Client.new(
timeout: nil # default is 600
)
# Or, configure per-request:
openai.chat.completions.create(
messages: [{role: :user, content: "How can I list all files in a directory using Python?"}],
model: :"gpt-4.1",
request_options: {timeout: 5}
)
This library uses a simple DSL to represent request parameters and response shapes in lib/openai/models
.
With the right editor plugins, you can ctrl-click on elements of the DSL to navigate around and explore the library.
In all places where a BaseModel
type is specified, vanilla Ruby Hash
can also be used. For example, the following are interchangeable as arguments:
# This has tooling readability, for auto-completion, static analysis, and goto definition with supported language services
params = OpenAI::Models::Chat::CompletionCreateParams.new(
messages: [OpenAI::Models::Chat::ChatCompletionUserMessageParam.new(role: :user, content: "Say this is a test")],
model: :"gpt-4.1"
)
# This also works
params = {
messages: [{role: :user, content: "Say this is a test"}],
model: :"gpt-4.1"
}
A combination of Shopify LSP and Solargraph is recommended for non-Sorbet users. The former is especially good at go to definition, while the latter has much better auto-completion support.
If you want to explicitly send an extra param, you can do so with the extra_query
, extra_body
, and extra_headers
under the request_options:
parameter when making a requests as seen in examples above.
To make requests to undocumented endpoints, you can make requests using client.request
. Options on the client will be respected (such as retries) when making this request.
response = client.request(
method: :post,
path: '/undocumented/endpoint',
query: {"dog": "woof"},
headers: {"useful-header": "interesting-value"},
body: {"he": "llo"},
)
The OpenAI::Client
instances are thread-safe, and should be re-used across multiple threads. By default, each Client
have their own HTTP connection pool, with a maximum number of connections equal to thread count.
When the maximum number of connections has been checked out from the connection pool, the Client
will wait for an in use connection to become available. The queue time for this mechanism is accounted for by the per-request timeout.
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
Currently, OpenAI::Client
instances are only fork-safe if there are no in-flight HTTP requests.
Sorbet's typed enums require sub-classing of the T::Enum
class from the sorbet-runtime
gem.
Since this library does not depend on sorbet-runtime
, it uses a T.all
intersection type with a ruby primitive type to construct a "tagged alias" instead.
module OpenAI::Models::ChatModel
# This alias aids language service driven navigation.
TaggedSymbol = T.type_alias { T.all(Symbol, OpenAI::Models::ChatModel) }
end
It is possible to pass a compatible model / parameter class to a method that expects keyword arguments by using the **
splat operator.
params = OpenAI::Models::Chat::CompletionCreateParams.new(
messages: [OpenAI::Models::Chat::ChatCompletionUserMessageParam.new(role: :user, content: "Say this is a test")],
model: :"gpt-4.1"
)
openai.chat.completions.create(**params)
This package follows SemVer conventions. As the library is in initial development and has a major version of 0
, APIs may change at any time.
This package considers improvements to the (non-runtime) *.rbi
and *.rbs
type definitions to be non-breaking changes.
Ruby 3.1.0 or higher.