Skip to content

Handle streaming errors and return to user proc, with result_type #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ client.chat(
messages: [{ role: "user", content: "Describe a character called Anna!"}], # Required.
temperature: 0.7,
stream: proc do |chunk, _bytesize|
print chunk.dig("choices", 0, "delta", "content")
if chunk["result_type"] == "data"
print chunk.dig("choices", 0, "delta", "content")
elsif chunk["result_type"] == "error"
STDERR.puts "Error: #{chunk.inspect}"
else
STDERR.puts "Unknown chunk type: #{chunk.inspect}"
end
end
})
# => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."
Expand Down
22 changes: 18 additions & 4 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,24 @@ def to_json(string)
# @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
def to_json_stream(user_proc:)
proc do |chunk, _|
chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
user_proc.call(JSON.parse(data))
rescue JSON::ParserError
# Ignore invalid JSON.
results = chunk.scan(/^\s*(data|error): *(\{.+\})/i)
if results.length.positive?
results.each do |result_type, result_json|
result = JSON.parse(result_json)
result.merge!("result_type" => result_type)
user_proc.call(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see result as an object rather than an extended parsed JSON hash. The object could extend some of Hash's []/dig methods for backwards compatibility. Reason being, it would be nice to eventually add header info, or add other metadata that would be helpful when processing the chunks externally to this gem without polluting the response's contents.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, but probably for another PR — trying to keep this one as tightly scoped as possible.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this implementation will catch all errors. I've been playing around with the solution and the regex doesn't work. Here is a sample using a hand-rolled HTTParty request with a bad API key:

irb(main):259:1* def handle_open_ai_response
irb(main):260:2*   proc do |response|
irb(main):261:2*     puts "IN PROC"
irb(main):262:2*     puts response
irb(main):263:2*     puts "PARSED RESPONSE"
irb(main):264:2*     puts response.scan(/^\s*(data|error): *(\{.+\})/i)
irb(main):265:2*     puts "END PARSED RESPONSE"
irb(main):266:1*   end
irb(main):267:0> end
:handle_open_ai_response
irb(main):268:0> HTTParty.post("https://api.openai.com/v1/chat/completions", headers:, body:, stream_body: true,  &handle_open_ai_response)
IN PROC
{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
PARSED RESPONSE
END PARSED RESPONSE

Notice that the Regexp does not match the error with any information.

rescue JSON::ParserError
# Ignore invalid JSON.
end
elsif !chunk.match(/^\s*(data|error):/i)
begin
result = JSON.parse(chunk)
result_type = result["error"] ? "error" : "unknown"
result.merge!("result_type" => result_type)
user_proc.call(result)
rescue JSON::ParserError
# Ignore invalid JSON.
end
end
end
end
Expand Down
76 changes: 67 additions & 9 deletions spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@

context "when called with a string containing a single JSON object" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect(user_proc).to receive(:call).with({ "foo" => "bar", "result_type" => "data" })
stream.call('data: { "foo": "bar" }')
end
end

context "when called with string containing more than one JSON object" do
it "calls the user proc for each data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect(user_proc).to receive(:call).with(JSON.parse('{"baz": "qud"}'))
expect(user_proc).to receive(:call).with({ "foo" => "bar", "result_type" => "data" })
expect(user_proc).to receive(:call).with({ "baz" => "qud", "result_type" => "data" })

stream.call(<<-CHUNK)
stream.call(<<~CHUNK)
data: { "foo": "bar" }

data: { "baz": "qud" }
Expand All @@ -141,14 +141,14 @@

context "when called with a string containing that looks like a JSON object but is invalid" do
let(:chunk) do
<<-CHUNK
<<~CHUNK
data: { "foo": "bar" }
data: { BAD ]:-> JSON }
CHUNK
end

it "does not raise an error" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect(user_proc).to receive(:call).with({ "foo" => "bar", "result_type" => "data" })

expect do
stream.call(chunk)
Expand All @@ -158,23 +158,81 @@

context "when called with a string containing an error" do
let(:chunk) do
<<-CHUNK
<<~CHUNK
data: { "foo": "bar" }
error: { "message": "A bad thing has happened!" }
CHUNK
end

it "does not raise an error" do
expect(user_proc).to receive(:call).with(JSON.parse('{ "foo": "bar" }'))
expect(user_proc).to receive(:call).with({ "foo" => "bar", "result_type" => "data" })
expect(user_proc).to receive(:call).with(
JSON.parse('{ "message": "A bad thing has happened!" }')
{ "message" => "A bad thing has happened!", "result_type" => "error" }
)

expect do
stream.call(chunk)
end.not_to raise_error
end
end

context "when called with a string that is a JSON object (with no 'data:' or 'error:' prefix)" do
context "when the JSON has a top level 'error' key" do
let(:chunk) do
<<~CHUNK
{
"error": {
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}
CHUNK
end

it "does not raise an error" do
expect(user_proc).to receive(:call).with(
{
"error" => {
"type" => "invalid_request_error",
"code" => "invalid_api_key"
},
"result_type" => "error"
}
)

expect do
stream.call(chunk)
end.not_to raise_error
end
end

context "when the JSON does not have a top level 'error' key" do
let(:chunk) do
<<~CHUNK
{
"warning": {
"message": "foobar"
}
}
CHUNK
end

it "does not raise an error" do
expect(user_proc).to receive(:call).with(
{
"result_type" => "unknown",
"warning" => {
"message" => "foobar"
}
}
)

expect do
stream.call(chunk)
end.not_to raise_error
end
end
end
end
end

Expand Down