Module: RubyLLM::Protocols::Responses::Media

Defined in:
lib/ruby_llm/protocols/responses/media.rb

Overview

Handles formatting of media content for the OpenAI Responses API

Class Method Summary collapse

Class Method Details

.format_attachment(attachment) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/protocols/responses/media.rb', line 19

def format_attachment(attachment)
  return format_provider_file(attachment) if attachment.provider_file?

  case attachment.type
  when :image
    format_image(attachment)
  when :pdf, :document
    format_document(attachment)
  when :text
    { type: 'input_text', text: attachment.for_llm }
  else
    raise UnsupportedAttachmentError, attachment.mime_type
  end
end

.format_content(content, attachments = []) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ruby_llm/protocols/responses/media.rb', line 10

def format_content(content, attachments = [])
  return content if attachments.empty?

  parts = []
  parts << { type: 'input_text', text: content } if content
  attachments.each { |attachment| parts << format_attachment(attachment) }
  parts
end

.format_document(document) ⇒ Object

The Responses API extracts text from documents, presentations, and spreadsheets as well as PDFs, so every document attachment rides along as a native input_file.



44
45
46
47
48
49
50
# File 'lib/ruby_llm/protocols/responses/media.rb', line 44

def format_document(document)
  {
    type: 'input_file',
    filename: document.filename,
    file_data: document.for_llm
  }
end

.format_image(image) ⇒ Object



34
35
36
37
38
39
# File 'lib/ruby_llm/protocols/responses/media.rb', line 34

def format_image(image)
  {
    type: 'input_image',
    image_url: image.url? ? image.source.to_s : image.for_llm
  }
end

.format_provider_file(file) ⇒ Object



52
53
54
55
56
57
# File 'lib/ruby_llm/protocols/responses/media.rb', line 52

def format_provider_file(file)
  {
    type: 'input_file',
    file_id: file.provider_file_id
  }
end