BaseModel provides convenient methods and features for all parameter and response objects in the OpenAI Ruby SDK
All parameter and response objects in the OpenAI Ruby SDK inherit from OpenAI::Internal::Type::BaseModel, which provides several conveniences for working with API data.
Both instances and classes can be pretty-printed for debugging:
# Pretty-print an instancepp completion# Output:# #<OpenAI::Chat::ChatCompletion# id="chatcmpl-123",# object="chat.completion",# created=1677652288,# model="gpt-5.2-0125",# choices=# [#<OpenAI::Chat::ChatCompletion::Choice# index=0,# message=# #<OpenAI::Chat::ChatCompletionMessage# role="assistant",# content="Hello! How can I help you today?">,# finish_reason="stop">],# usage=# #<OpenAI::CompletionUsage# prompt_tokens=9,# completion_tokens=9,# total_tokens=18>>
Pretty-printing classes shows their structure:
pp OpenAI::Chat::CompletionCreateParams# Shows all available parameters and their types
BaseModel’s hash access syntax lets you read undocumented or future API fields:
completion = client.chat.completions.create( messages: [{role: "user", content: "Hello"}], model: "gpt-5.2")# Access a field that might be added in the futureexperimental_data = completion[:experimental_feature]# Returns nil if the field doesn't existexperimental_data.nil?# => true (assuming the field doesn't exist)
This makes the SDK forward-compatible with API changes.
require 'json'# Save response to fileFile.write('response.json', completion.to_json)# Load and parse laterdata = JSON.parse(File.read('response.json'))# Access the cached dataputs data['choices'][0]['message']['content']
The BaseModel implementation is in lib/openai/internal/type/base_model.rb in the gem source code.
Method resolution
BaseModel uses Ruby’s method_missing to provide both method-style (dot notation) and hash-style (bracket notation) access to fields. Method-style access is preferred for known fields, while hash-style access is useful for dynamic or unknown fields.