Skip to content

Commit dc4d1c6

Browse files
committed
Use Faraday's built in JSON parsing
1 parent 1e463b4 commit dc4d1c6

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

lib/openai/http.rb

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,32 @@
33
module OpenAI
44
module HTTP
55
def get(path:)
6-
to_json(conn.get(uri(path: path)) do |req|
6+
conn.get(uri(path: path)) do |req|
77
req.headers = headers
8-
end&.body)
8+
end&.body
99
end
1010

1111
def json_post(path:, parameters:)
12-
to_json(conn.post(uri(path: path)) do |req|
12+
conn.post(uri(path: path)) do |req|
1313
configure_json_post_request(req, parameters)
14-
end&.body)
14+
end&.body
1515
end
1616

1717
def multipart_post(path:, parameters: nil)
18-
to_json(conn(multipart: true).post(uri(path: path)) do |req|
18+
conn(multipart: true).post(uri(path: path)) do |req|
1919
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
2020
req.body = multipart_parameters(parameters)
21-
end&.body)
21+
end&.body
2222
end
2323

2424
def delete(path:)
25-
to_json(conn.delete(uri(path: path)) do |req|
25+
conn.delete(uri(path: path)) do |req|
2626
req.headers = headers
27-
end&.body)
27+
end&.body
2828
end
2929

3030
private
3131

32-
def to_json(string)
33-
return unless string
34-
35-
JSON.parse(string)
36-
rescue JSON::ParserError
37-
# Convert a multiline string of JSON objects to a JSON array.
38-
JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
39-
end
40-
4132
# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
4233
# For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
4334
# be a data object or an error object as described in the OpenAI API documentation.
@@ -64,6 +55,7 @@ def conn(multipart: false)
6455
f.options[:timeout] = @request_timeout
6556
f.request(:multipart) if multipart
6657
f.response :raise_error
58+
f.response :json
6759
end
6860
end
6961

spec/openai/client/finetunes_spec.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@
5353

5454
it "succeeds" do
5555
VCR.use_cassette(cassette) do
56-
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
56+
response
57+
rescue Faraday::ResourceNotFound => e
58+
expect(e.response).to include(status: 404)
59+
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
60+
else
61+
raise "Expected to raise Faraday::ResourceNotFound"
5762
end
5863
end
5964
end
@@ -64,7 +69,12 @@
6469

6570
it "succeeds" do
6671
VCR.use_cassette(cassette) do
67-
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
72+
response
73+
rescue Faraday::ResourceNotFound => e
74+
expect(e.response).to include(status: 404)
75+
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
76+
else
77+
raise "Expected to raise Faraday::ResourceNotFound"
6878
end
6979
end
7080
end
@@ -75,7 +85,12 @@
7585

7686
it "succeeds" do
7787
VCR.use_cassette(cassette) do
78-
expect(response.dig("error", "code")).to eq("fine_tune_not_found")
88+
response
89+
rescue Faraday::ResourceNotFound => e
90+
expect(e.response).to include(status: 404)
91+
expect(e.response.dig(:body, "error", "code")).to eq("fine_tune_not_found")
92+
else
93+
raise "Expected to raise Faraday::ResourceNotFound"
7994
end
8095
end
8196
end

spec/openai/client/http_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,6 @@
189189
end
190190
end
191191

192-
describe ".to_json" do
193-
context "with a jsonl string" do
194-
let(:body) { "{\"prompt\":\":)\"}\n{\"prompt\":\":(\"}\n" }
195-
let(:parsed) { OpenAI::Client.new.send(:to_json, body) }
196-
197-
it { expect(parsed).to eq([{ "prompt" => ":)" }, { "prompt" => ":(" }]) }
198-
end
199-
end
200-
201192
describe ".uri" do
202193
let(:path) { "/chat" }
203194
let(:uri) { OpenAI::Client.new.send(:uri, path: path) }

0 commit comments

Comments
 (0)