Skip to content

Commit ff331d9

Browse files
authored
Merge pull request #566 from alexrudall/fix/non-json-content
Fixes for various JSON/non-JSON content issues
2 parents c87e36e + 19bce2b commit ff331d9

10 files changed

+399
-17
lines changed

lib/openai/http.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,52 @@ module HTTP
77
include HTTPHeaders
88

99
def get(path:, parameters: nil)
10-
parse_jsonl(conn.get(uri(path: path), parameters) do |req|
10+
parse_json(conn.get(uri(path: path), parameters) do |req|
1111
req.headers = headers
1212
end&.body)
1313
end
1414

1515
def post(path:)
16-
parse_jsonl(conn.post(uri(path: path)) do |req|
16+
parse_json(conn.post(uri(path: path)) do |req|
1717
req.headers = headers
1818
end&.body)
1919
end
2020

2121
def json_post(path:, parameters:, query_parameters: {})
22-
conn.post(uri(path: path)) do |req|
22+
parse_json(conn.post(uri(path: path)) do |req|
2323
configure_json_post_request(req, parameters)
2424
req.params = req.params.merge(query_parameters)
25-
end&.body
25+
end&.body)
2626
end
2727

2828
def multipart_post(path:, parameters: nil)
29-
conn(multipart: true).post(uri(path: path)) do |req|
29+
parse_json(conn(multipart: true).post(uri(path: path)) do |req|
3030
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
3131
req.body = multipart_parameters(parameters)
32-
end&.body
32+
end&.body)
3333
end
3434

3535
def delete(path:)
36-
conn.delete(uri(path: path)) do |req|
36+
parse_json(conn.delete(uri(path: path)) do |req|
3737
req.headers = headers
38-
end&.body
38+
end&.body)
3939
end
4040

4141
private
4242

43-
def parse_jsonl(response)
43+
def parse_json(response)
4444
return unless response
4545
return response unless response.is_a?(String)
4646

47-
# Convert a multiline string of JSON objects to a JSON array.
48-
response = response.gsub("}\n{", "},{").prepend("[").concat("]")
47+
original_response = response.dup
48+
if response.include?("}\n{")
49+
# Attempt to convert what looks like a multiline string of JSON objects to a JSON array.
50+
response = response.gsub("}\n{", "},{").prepend("[").concat("]")
51+
end
4952

5053
JSON.parse(response)
54+
rescue JSON::ParserError
55+
original_response
5156
end
5257

5358
# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.

spec/fixtures/cassettes/files_fetch_image.yml

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

spec/fixtures/cassettes/files_fetch_image_retrieve.yml

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

spec/fixtures/cassettes/files_fetch_image_upload.yml

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

spec/fixtures/cassettes/mocks/gpt-3_5-turbo_streamed_chat_with_error_response.yml

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

spec/fixtures/cassettes/mocks/gpt-3_5-turbo_streamed_chat_with_json_error_response.yml

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

0 commit comments

Comments
 (0)