Skip to content

Commit f3f90d1

Browse files
simonx1alexrudall
authored andcommitted
Allow StringIO object to be passed to
1 parent 27e1ae3 commit f3f90d1

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

lib/openai/http.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ def multipart_parameters(parameters)
9999
parameters&.transform_values do |value|
100100
next value unless value.respond_to?(:close) # File or IO object.
101101

102+
# Faraday::UploadIO does not require a path, so we will pass it
103+
# only if it is available. This allows StringIO objects to be
104+
# passed in as well.
105+
path = value.respond_to?(:path) ? value.path : nil
102106
# Doesn't seem like OpenAI needs mime_type yet, so not worth
103107
# the library to figure this out. Hence the empty string
104108
# as the second argument.
105-
Faraday::UploadIO.new(value, "", value.path)
109+
Faraday::UploadIO.new(value, "", path)
106110
end
107111
end
108112

spec/fixtures/cassettes/files_upload.yml

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/openai/client/files_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@
3131
it { expect { upload }.to raise_error(ArgumentError) }
3232
end
3333

34-
context "with a file content" do
34+
context "with a `File` instance content" do
3535
let(:file) { File.open(File.join(RSPEC_ROOT, "fixtures/files", filename)) }
3636

3737
it "succeeds" do
3838
expect(upload["filename"]).to eq(filename)
3939
end
4040
end
41+
42+
context "with a `StringIO` instance content" do
43+
let(:file) { StringIO.new(File.read(File.join(RSPEC_ROOT, "fixtures/files", filename))) }
44+
45+
it "succeeds" do
46+
expect(upload["filename"]).to eq("local.path")
47+
end
48+
end
4149
end
4250

4351
describe "#list" do

0 commit comments

Comments
 (0)