diff --git a/lib/openai/http.rb b/lib/openai/http.rb index ca59f903..332acd01 100644 --- a/lib/openai/http.rb +++ b/lib/openai/http.rb @@ -49,9 +49,12 @@ def to_json_stream(user_proc:) proc do |chunk, _bytes, env| if env && env.status != 200 - parser.feed(chunk) do |_type, data| - emit_json(json: data, user_proc: user_proc) unless data == "[DONE]" - end + raise_error = Faraday::Response::RaiseError.new + raise_error.on_complete(env.merge(body: JSON.parse(chunk))) + end + + parser.feed(chunk) do |_type, data| + user_proc.call(JSON.parse(data)) unless data == "[DONE]" end end end