Skip to content

Commit b0008c7

Browse files
authored
Merge pull request #37 from alexrudall/validate_jsonl
Add JSONL validation
2 parents 26a6c29 + c42876b commit b0008c7

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.1] - 2021-04-11
9+
10+
### Added
11+
12+
- Add validation of JSONL files to make it easier to debug during upload.
13+
814
## [1.2.0] - 2021-04-08
915

1016
### Added

lib/ruby/openai/files.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ def list(version: default_version)
1818
end
1919

2020
def upload(version: default_version, parameters: {})
21+
file = validate(file: parameters[:file])
22+
2123
self.class.post(
2224
"/#{version}/files",
2325
headers: {
2426
"Content-Type" => "application/json",
2527
"Authorization" => "Bearer #{@access_token}"
2628
},
27-
body: parameters.merge(file: File.open(parameters[:file]))
29+
body: parameters.merge(file: file)
2830
)
2931
end
3032

@@ -53,5 +55,13 @@ def delete(id:, version: default_version)
5355
def default_version
5456
"v1".freeze
5557
end
58+
59+
def validate(file:)
60+
File.open(file).each_line.with_index do |line, index|
61+
JSON.parse(line)
62+
rescue JSON::ParserError => e
63+
raise JSON::ParserError, "#{e.message} - found on line #{index + 1} of #{file}"
64+
end
65+
end
5666
end
5767
end

lib/ruby/openai/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Ruby
22
module OpenAI
3-
VERSION = "1.2.0".freeze
3+
VERSION = "1.2.1".freeze
44
end
55
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"text": "puppy A is happy", "metadata": "emotional state of puppy A"}
2+
{"text": "puppy B is sad", "metadata": "emotional state of puppy B}

spec/ruby/openai/client/files_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@
1111
OpenAI::Client.new.files.upload(parameters: { file: file, purpose: purpose })
1212
end
1313

14-
it "succeeds" do
15-
VCR.use_cassette(cassette) do
16-
r = JSON.parse(response.body)
17-
expect(r["filename"]).to eq(filename)
14+
context "with a valid JSON lines file" do
15+
it "succeeds" do
16+
VCR.use_cassette(cassette) do
17+
r = JSON.parse(response.body)
18+
expect(r["filename"]).to eq(filename)
19+
end
1820
end
1921
end
22+
23+
context "with an invalid file" do
24+
let(:filename) { File.join("errors", "missing_quote.jsonl") }
25+
26+
it { expect { response }.to raise_error(JSON::ParserError) }
27+
end
2028
end
2129

2230
describe "#list" do

0 commit comments

Comments
 (0)