Skip to main content
The OpenAI Ruby library provides convenient access to the OpenAI REST API from any Ruby 3.2.0+ application. It ships with comprehensive types and docstrings in Yard, RBS, and RBI for a fully type-safe development experience.

Installation

Install the gem using Bundler or directly with gem install

Quick Start

Make your first API call in minutes

API Reference

Explore all available methods and resources

Examples

Browse real-world code examples

Key Features

The OpenAI Ruby SDK is designed to make working with the OpenAI API seamless and intuitive:

Streaming Support

Built-in support for streaming responses using Server-Sent Events (SSE), allowing you to process API responses in real-time as they’re generated.

Auto-Pagination

Automatic pagination for list methods with auto_paging_each, so you don’t have to manually request successive pages of results.

Structured Outputs & Function Calling

Powerful helpers with OpenAI::BaseModel, OpenAI::ArrayOf, OpenAI::EnumOf, and OpenAI::UnionOf to define JSON schemas for structured outputs and function calling.

File Upload Support

Flexible file upload handling with support for Pathname, StringIO, raw file contents, and more.

Webhook Verification

Built-in methods to verify webhook signatures and parse webhook payloads securely.

Error Handling

Comprehensive error classes for all API status codes with automatic retries for transient failures.

Type Safety

Full Sorbet, RBS, and RBI type definitions included for a completely type-safe development experience.

Concurrency & Connection Pooling

Threadsafe client instances with built-in connection pooling using the connection_pool gem.

Requirements

  • Ruby 3.2.0 or higher
  • The standard library’s net/http is used as the HTTP transport
  • Connection pooling via the connection_pool gem

Get Started

Ready to build with OpenAI? Head over to the Installation guide to get set up, or jump straight to the Quick Start to make your first API call.

Build docs developers (and LLMs) love