Skip to content

Commit b87c834

Browse files
feat: support jsonl uploads (#10)
1 parent 30684f8 commit b87c834

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/openai/util.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ def encode_content(headers, body)
559559
case [content_type, body]
560560
in [%r{^application/(?:vnd\.api\+)?json}, Hash | Array]
561561
[headers, JSON.fast_generate(body)]
562+
in [%r{^application/(?:x-)?jsonl}, Enumerable]
563+
[headers, body.lazy.map { JSON.fast_generate(_1) }]
562564
in [%r{^multipart/form-data}, Hash | IO | StringIO]
563565
boundary, strio = encode_multipart_streaming(body)
564566
headers = {**headers, "content-type" => "#{content_type}; boundary=#{boundary}"}
@@ -589,14 +591,14 @@ def decode_content(headers, stream:, suppress_error: false)
589591
raise e unless suppress_error
590592
json
591593
end
592-
in %r{^text/event-stream}
593-
lines = decode_lines(stream)
594-
decode_sse(lines)
595594
in %r{^application/(?:x-)?jsonl}
596595
lines = decode_lines(stream)
597596
chain_fused(lines) do |y|
598597
lines.each { y << JSON.parse(_1, symbolize_names: true) }
599598
end
599+
in %r{^text/event-stream}
600+
lines = decode_lines(stream)
601+
decode_sse(lines)
600602
in %r{^text/}
601603
stream.to_a.join
602604
else

0 commit comments

Comments
 (0)