handle parsing for error when chunked#591
Open
stevenou wants to merge 3 commits intoalexrudall:mainfrom
Open
Conversation
stevenou
added a commit
to InvisibleCommerce/ruby-openai
that referenced
this pull request
Jan 15, 2026
When the OpenAI API returns an error during streaming, the error JSON body may be split across multiple HTTP chunks. Previously, the code would immediately try to raise an error on the first chunk, which would fail if the JSON was incomplete. This change accumulates error chunks until a complete, parsable JSON object is formed, then raises the appropriate Faraday error. It also prevents the user proc from being called when an error status is received. Based on the approach from PR alexrudall#591. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
So I based this off of #480 which seems like it might be abandoned at this point. I copy-pasted the spec minus a minor fix for splitting the json into 2 chunks (it was missing a character in the 2nd chunk).
The approach taken here assumes the error is parsable json. We accumulate the chunks until the message is parsable, then we raise an error. I don't really know enough about SSE/streaming to know whether this is definitely a bad idea but none of the existing specs broke so I'm hoping it's acceptable.
Not sure what would happen if, let's say, the stream disconnects midway so we never get the completed error message. I'm guessing in that case a different type of error (related to connection/streaming) will surface instead of the original error, which is probably ok?
Or if the error is never parsable - then possibly the whole thing fails silently?
Let me know your thoughts please.