Module: RubyLLM::Providers::OpenAIResponses::Media

Included in:
RubyLLM::Providers::OpenAIResponses
Defined in:
lib/ruby_llm/providers/openai_responses/media.rb

Overview

Media handling methods for the OpenAI Responses API. Handles images, audio, PDFs, and other file types.

Class Method Summary collapse

Class Method Details

.detect_audio_format(source) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 73

def detect_audio_format(source)
  return 'mp3' unless source

  ext = if source.respond_to?(:path)
          File.extname(source.path)
        else
          File.extname(source.to_s)
        end

  case ext.downcase
  when '.mp3' then 'mp3'
  when '.wav' then 'wav'
  when '.webm' then 'webm'
  when '.ogg' then 'ogg'
  when '.flac' then 'flac'
  when '.m4a' then 'm4a'
  else 'mp3'
  end
end

.extract_filename(source) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 63

def extract_filename(source)
  return 'file' unless source

  if source.respond_to?(:path)
    File.basename(source.path)
  else
    File.basename(source.to_s)
  end
end

.format_attachment(attachment) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 11

def format_attachment(attachment)
  case attachment.type
  when :image
    format_image(attachment)
  when :pdf
    format_pdf(attachment)
  when :audio
    format_audio(attachment)
  when :text
    format_text_file(attachment)
  else
    raise UnsupportedAttachmentError, attachment.type
  end
end

.format_audio(audio) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 48

def format_audio(audio)
  {
    type: 'input_audio',
    data: audio.for_llm,
    format: detect_audio_format(audio.source)
  }
end

.format_image(image) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 26

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

.format_pdf(pdf) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 40

def format_pdf(pdf)
  {
    type: 'input_file',
    filename: extract_filename(pdf.source),
    file_data: pdf.for_llm
  }
end

.format_text_file(text_file) ⇒ Object



56
57
58
59
60
61
# File 'lib/ruby_llm/providers/openai_responses/media.rb', line 56

def format_text_file(text_file)
  {
    type: 'input_text',
    text: text_file.for_llm
  }
end