Fix streaming error parsing crash on non-Hash JSON error bodies#840
Open
Niraj22 wants to merge 1 commit into
Open
Fix streaming error parsing crash on non-Hash JSON error bodies#840Niraj22 wants to merge 1 commit into
Niraj22 wants to merge 1 commit into
Conversation
parse_streaming_error assumed the error body parses to a Hash with a
Hash 'error' value. A bare JSON string body, or a string error value
like {"error": "msg"} (xAI/Grok shape), crashed with TypeError:
String does not have #dig instead of surfacing the provider's message.
Guard the body shape before digging, in the chat_completions, anthropic
and gemini protocols plus the default fallback. Untyped bodies return a
nil status so the real HTTP status from the response env wins.
Fixes crmne#837
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.
What this does
Fixes #837. When a streaming request fails and the error body parses to something other than the typed
{"error": {"type": …, "message": …}}hash — a bare JSON string, or a string error value like{"error": "msg"}(xAI/Grok shape) —parse_streaming_errorcrashed withTypeError: String does not have #dig methodinstead of surfacing the provider's message. The same bodies were already handled fine on the non-streaming path.The old guard
return unless error_data['error']did substring matching on string bodies (String#[]), anddig('error', 'type')recursed into string error values — hence the crash.This adds shape guards before the hash lookups in the
chat_completions,anthropic, andgeminiprotocols plus the default fallback inStreaming. Untyped bodies return anilstatus sobuild_stream_error_responsefalls back to the real HTTP status; the user-facing message still comes fromProvider#parse_error, which already handles string bodies.Type of change
How tested
Regression specs per protocol (typed object, bare string body, string error value) plus end-to-end specs through
handle_failed_responseasserting the provider's actual message surfaces in the raised error. Full suite: 1889 examples, 0 failures.