ruby/index.md +18 −3
82 82
83Request parameters that correspond to file uploads can be passed as raw contents, a [`Pathname`](https://rubyapi.org/3.3/o/pathname) instance, [`StringIO`](https://rubyapi.org/3.3/o/stringio), or more.83Request parameters that correspond to file uploads can be passed as raw contents, a [`Pathname`](https://rubyapi.org/3.3/o/pathname) instance, [`StringIO`](https://rubyapi.org/3.3/o/stringio), or more.
84 84
85Raw `String` and `StringIO` values, and `IO` objects without a path, do not carry format-identifying metadata. The SDK sends them using the fallback filename `upload`; raw `String` values default to `text/plain`, while `StringIO` and pathless `IO` values default to `application/octet-stream`. For format-sensitive endpoints, such as audio transcriptions, wrap each value in `OpenAI::FilePart` and provide an extension-bearing filename and content type.
86
85```ruby87```ruby
86require "pathname"88require "pathname"
87 89
93 95
94puts(file_object.id)96puts(file_object.id)
95 97
9698# Or, to control the filename and/or content type:# For format-sensitive uploads, provide the filename and content type:
9799image = OpenAI::FilePart.new(Pathname('dog.jpg'), content_type: 'image/jpeg')audio_data = StringIO.new(File.binread("audio.wav"))
100audio = OpenAI::FilePart.new(
101 audio_data,
102 filename: "audio.wav",
103 content_type: "audio/wav"
104)
105transcription = openai.audio.transcriptions.create(
106 model: "gpt-4o-transcribe",
107 file: audio
108)
109puts(transcription.text)
110
111# FilePart also accepts a Pathname:
112image = OpenAI::FilePart.new(Pathname("dog.jpg"), content_type: "image/jpeg")
98edited = openai.images.edit(113edited = openai.images.edit(
99 prompt: "make this image look like a painting",114 prompt: "make this image look like a painting",
100 model: "gpt-image-1",115 model: "gpt-image-1",
101116 size: '1024x1024', size: "1024x1024",
102 image: image117 image: image
103)118)
104 119