Skip to content

Fix streaming error parsing crash on non-Hash JSON error bodies#840

Open
Niraj22 wants to merge 1 commit into
crmne:mainfrom
Niraj22:fix-streaming-error-non-hash-body
Open

Fix streaming error parsing crash on non-Hash JSON error bodies#840
Niraj22 wants to merge 1 commit into
crmne:mainfrom
Niraj22:fix-streaming-error-non-hash-body

Conversation

@Niraj22

@Niraj22 Niraj22 commented Jul 13, 2026

Copy link
Copy Markdown

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_error crashed with TypeError: String does not have #dig method instead 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#[]), and dig('error', 'type') recursed into string error values — hence the crash.

This adds shape guards before the hash lookups in the chat_completions, anthropic, and gemini protocols plus the default fallback in Streaming. Untyped bodies return a nil status so build_stream_error_response falls back to the real HTTP status; the user-facing message still comes from Provider#parse_error, which already handles string bodies.

Type of change

  • Bug fix

How tested

Regression specs per protocol (typed object, bare string body, string error value) plus end-to-end specs through handle_failed_response asserting the provider's actual message surfaces in the raised error. Full suite: 1889 examples, 0 failures.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming: parse_streaming_error crashes with 'TypeError: String does not have #dig' on a non-Hash JSON error body

1 participant