Module: RubyLLM::Providers::XAI::Images

Included in:
ChatCompletions, Responses
Defined in:
lib/ruby_llm/providers/xai/images.rb

Overview

Image generation and editing for the xAI API. Generation rejects the size parameter. Editing takes JSON image references (URL, data URI, or file id) instead of multipart uploads, up to three per request, and has no mask support.

Class Method Summary collapse

Class Method Details

.blank_attachment?(value) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby_llm/providers/xai/images.rb', line 85

def blank_attachment?(value)
  Protocols::ChatCompletions::Images.blank_attachment?(value)
end

.editing?(with, mask) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_llm/providers/xai/images.rb', line 81

def editing?(with, mask)
  Protocols::ChatCompletions::Images.editing?(with, mask)
end

.image_reference_url(source) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ruby_llm/providers/xai/images.rb', line 46

def image_reference_url(source)
  attachment = Attachment.new(source, config: @config)
  return attachment.provider_file_id if attachment.provider_file?
  return attachment.source.to_s if attachment.url?

  raise UnsupportedAttachmentError, attachment.mime_type unless attachment.image?

  attachment.for_llm
end

.image_references(sources) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/ruby_llm/providers/xai/images.rb', line 38

def image_references(sources)
  Array(sources).filter_map do |source|
    next if blank_attachment?(source)

    { type: 'image_url', url: image_reference_url(source) }
  end
end

.images_url(with: nil, mask: nil) ⇒ Object



13
14
15
# File 'lib/ruby_llm/providers/xai/images.rb', line 13

def images_url(with: nil, mask: nil)
  editing?(with, mask) ? 'images/edits' : 'images/generations'
end

.parse_image_response(response, model:) ⇒ Object



56
57
58
# File 'lib/ruby_llm/providers/xai/images.rb', line 56

def parse_image_response(response, model:)
  parse_image_responses(response, model:).first
end

.parse_image_responses(response, model:) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_llm/providers/xai/images.rb', line 60

def parse_image_responses(response, model:)
  data = response.body
  entries = Array(data['data'])

  raise Error, 'Unexpected response format from xAI image API' if entries.empty?

  entries.map.with_index do |image_data, index|
    Image.new(
      url: image_data['url'],
      data: image_data['b64_json'],
      mime_type: image_data['mime_type'] || 'image/png',
      model: model,
      usage: index.zero? ? (data['usage'] || {}) : {}
    )
  end
end

.render_edit_payload(prompt, model:, with:, provider_options:, count: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/providers/xai/images.rb', line 27

def render_edit_payload(prompt, model:, with:, provider_options:, count: nil)
  payload = {
    model: model,
    prompt: prompt,
    images: image_references(with)
  }
  payload[:n] = count if count

  payload.merge(provider_options)
end

.render_image_payload(prompt, model:, size:, count: nil, with: nil, mask: nil, provider_options: {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_llm/providers/xai/images.rb', line 17

def render_image_payload(prompt, model:, size:, count: nil, with: nil, mask: nil, provider_options: {})
  return render_edit_payload(prompt, model:, count:, with:, provider_options:) if editing?(with, mask)

  RubyLLM.logger.debug { "Ignoring size #{size}. xAI image generation does not support a size parameter." }
  payload = { model: model, prompt: prompt }
  payload[:n] = count if count

  payload.merge(provider_options)
end

.validate_paint_inputs!(with:, mask:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument

Raises:



77
78
79
# File 'lib/ruby_llm/providers/xai/images.rb', line 77

def validate_paint_inputs!(with:, mask:) # rubocop:disable Lint/UnusedMethodArgument
  raise Error, 'xAI image editing does not support a mask parameter' if mask
end