class RubyLLM::Image
An Image is a generated or edited image. Save it to a file with save or read its bytes with to_blob. Both handle hosted URLs and inline data.
image = RubyLLM.paint("a sunset over mountains in watercolor style") image.save("sunset.png")
Attributes
The Base64-encoded image data, for providers that return the image inline, or nil.
The MIME type of the image data, such as "image/png".
The id of the model that generated the image.
The provider’s rewritten version of the prompt, when reported.
The URL of the hosted image, for providers that return one, or nil.
Public Class Methods
# File lib/ruby_llm/image.rb, line 75 def self.paint(prompt, model: nil, provider: nil, assume_model_exists: false, size: nil, count: nil, context: nil, with: nil, mask: nil, provider_options: {}, metadata: nil, owner: nil, fallbacks: []) queue = Array(fallbacks).flatten.compact.map { |fallback| Fallback.build(fallback) } operation = painting(model, provider:, assume_model_exists:, context:, owner:) begin with_fallback_ready(queue) do operation.run(prompt:, size:, count:, provider_options:, metadata:) do |provider_instance, image_model| provider_instance.paint(prompt, model: image_model, size:, count:, with:, mask:, provider_options:) end end rescue StandardError => e raise unless Fallback.error?(e, Fallback::DEFAULT_ERRORS) _, operation = Fallback.next_usable(queue) do |fallback| painting(fallback.id, provider: fallback.provider, context:, owner:, resolved: fallback.model) end raise e unless operation retry end end
Generates an image from prompt and returns an Image. Most code calls this through RubyLLM.paint.
model: selects the image model and defaults to the configured default_image_model. provider: forces a specific provider, and assume_model_exists: skips the registry lookup, which is useful for custom endpoints. size: requests dimensions on models that support it. count: asks for several images in one request, returning an Array of Images instead of one. with: passes one or more source images for editing, and mask: constrains which parts of the image may change. provider_options: takes options in the provider’s request vocabulary and merges them into the request as-is. context: supplies a Context whose configuration replaces the global one. metadata: is included in the instrumentation payload. owner: attributes the usage to a record, such as a user, and wins over RubyLLM.with_owner.
image = RubyLLM.paint("A small watercolor robot", model: "gpt-image-2") images = RubyLLM.paint("A small watercolor robot", count: 4) images.each_with_index { |image, i| image.save("robot-#{i}.png") } RubyLLM.paint( "Turn the logo green and keep the background transparent", model: "gpt-image-2", with: "logo.png" )
Providers that cannot generate several images in one request raise ArgumentError when count: is above 1.
fallbacks: lists models to try, in order, when the request fails with one of the errors Chat#with_fallbacks moves on from by default, such as a rate limit or an overloaded provider. Each is a model id or a Model, and gets the same request. A fallback whose model can’t be found is skipped. Each attempt has its own image.ruby_llm event and usage entry.
RubyLLM.paint("A small watercolor robot", model: "gpt-image-2", fallbacks: ["gemini-3.1-flash-image"])
Public Instance Methods
Source
# File lib/ruby_llm/image.rb, line 155 def base64? !@data.nil? end
Returns true if the image holds inline Base64 data, false otherwise.
Source
# File lib/ruby_llm/image.rb, line 204 def cost return ruby_llm_usage_cost unless ruby_llm_usage_entries.empty? Cost.new(tokens:, model: model_info, category: :images, input_details: input_tokens_details) end
Returns a Cost across every provider attempt, using reported prices when available and registry pricing otherwise.
image.cost.total
Source
# File lib/ruby_llm/image.rb, line 213 def model_info super if model end
Source
# File lib/ruby_llm/image.rb, line 173
Writes the binary image to path, expanding it first. Returns path as given.
image.save("steampunk_owl.png")
Source
# File lib/ruby_llm/image.rb, line 164 def to_blob if base64? Base64.decode64 @data else Transport::Connection.download(@url, config) end end
Source
# File lib/ruby_llm/image.rb, line 188 def tokens return ruby_llm_usage_tokens unless ruby_llm_usage_entries.empty? @tokens ||= Tokens.new( input: raw_usage['input_tokens'], output: raw_usage['output_tokens'], cache_read: raw_usage['cache_read_tokens'], reported_cost: raw_usage['cost'] ) end
Returns a Tokens with usage across every provider attempt. Its fields are nil when none were reported.
image.tokens.input image.tokens.output