Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.27.0"
".": "0.27.1"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.27.1 (2025-09-29)

Full Changelog: [v0.27.0...v0.27.1](https://github.com/openai/openai-ruby/compare/v0.27.0...v0.27.1)

### Bug Fixes

* always send `filename=...` for multipart requests where a file is expected ([99849fd](https://github.com/openai/openai-ruby/commit/99849fddf478869b57b77cbe208efd13d9a8246e))

## 0.27.0 (2025-09-26)

Full Changelog: [v0.26.0...v0.27.0](https://github.com/openai/openai-ruby/compare/v0.26.0...v0.27.0)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GIT
PATH
remote: .
specs:
openai (0.27.0)
openai (0.27.1)
connection_pool

GEM
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To use this gem, install via Bundler by adding the following to your application
<!-- x-release-please-start-version -->

```ruby
gem "openai", "~> 0.27.0"
gem "openai", "~> 0.27.1"
```

<!-- x-release-please-end -->
Expand Down
17 changes: 10 additions & 7 deletions lib/openai/file_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ def to_json(*a) = read.to_json(*a)
def to_yaml(*a) = read.to_yaml(*a)

# @param content [Pathname, StringIO, IO, String]
# @param filename [String, nil]
# @param filename [Pathname, String, nil]
# @param content_type [String, nil]
def initialize(content, filename: nil, content_type: nil)
@content = content
@content_type = content_type
@filename =
case content
in Pathname
filename.nil? ? content.basename.to_path : ::File.basename(filename)
case [filename, (@content = content)]
in [String | Pathname, _]
::File.basename(filename)
in [nil, Pathname]
content.basename.to_path
in [nil, IO]
content.to_path
else
filename.nil? ? nil : ::File.basename(filename)
filename
end
@content_type = content_type
end
end
end
11 changes: 7 additions & 4 deletions lib/openai/internal/type/file_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,20 @@ def coerce(value, state:)
#
# @return [Pathname, StringIO, IO, String, Object]
def dump(value, state:)
# rubocop:disable Lint/DuplicateBranch
case value
in StringIO | String
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
# while not required, a filename is recommended, and in practice many servers do expect this
OpenAI::FilePart.new(value, filename: "upload")
in IO
state[:can_retry] = false
value.to_path.nil? ? OpenAI::FilePart.new(value, filename: "upload") : value
in OpenAI::FilePart if value.content.is_a?(IO)
state[:can_retry] = false
value
else
value
end
# rubocop:enable Lint/DuplicateBranch

value
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion lib/openai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module OpenAI
VERSION = "0.27.0"
VERSION = "0.27.1"
end
2 changes: 1 addition & 1 deletion rbi/openai/file_part.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module OpenAI
sig do
params(
content: T.any(Pathname, StringIO, IO, String),
filename: T.nilable(String),
filename: T.nilable(T.any(Pathname, String)),
content_type: T.nilable(String)
).returns(T.attached_class)
end
Expand Down
2 changes: 1 addition & 1 deletion sig/openai/file_part.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module OpenAI

def initialize: (
Pathname | StringIO | IO | String content,
?filename: String?,
?filename: (Pathname | String)?,
?content_type: String?
) -> void
end
Expand Down
29 changes: 20 additions & 9 deletions test/openai/internal/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,24 @@ def test_encoding_length

def test_file_encode
file = Pathname(__FILE__)
fileinput = OpenAI::Internal::Type::Converter.dump(OpenAI::Internal::Type::FileInput, "abc")
headers = {"content-type" => "multipart/form-data"}
cases = {
"abc" => "abc",
StringIO.new("abc") => "abc",
OpenAI::FilePart.new("abc") => "abc",
OpenAI::FilePart.new(StringIO.new("abc")) => "abc",
file => /^class OpenAI/,
OpenAI::FilePart.new(file) => /^class OpenAI/
"abc" => ["", "abc"],
StringIO.new("abc") => ["", "abc"],
fileinput => %w[upload abc],
OpenAI::FilePart.new(StringIO.new("abc")) => ["", "abc"],
file => [file.basename.to_path, /^class OpenAI/],
OpenAI::FilePart.new(file, filename: "d o g") => ["d%20o%20g", /^class OpenAI/]
}
cases.each do |body, val|
cases.each do |body, testcase|
filename, val = testcase
encoded = OpenAI::Internal::Util.encode_content(headers, body)
cgi = FakeCGI.new(*encoded)
io = cgi[""]
assert_pattern do
cgi[""].read => ^val
io.original_filename => ^filename
io.read => ^val
end
end
end
Expand All @@ -261,7 +265,14 @@ def test_hash_encode
cgi = FakeCGI.new(*encoded)
testcase.each do |key, val|
assert_pattern do
cgi[key] => ^val
parsed =
case (p = cgi[key])
in StringIO
p.read
else
p
end
parsed => ^val
end
end
end
Expand Down