fix: prevent TransferEncodingError in Gemini/Claude SSE streams#2321
Closed
Stranmor wants to merge 1 commit into
Closed
fix: prevent TransferEncodingError in Gemini/Claude SSE streams#2321Stranmor wants to merge 1 commit into
Stranmor wants to merge 1 commit into
Conversation
When SSE streams encounter upstream errors, yield Err(...) causes hyper to abort HTTP/1.1 chunked transfer encoding without the final terminator. HTTP clients (aiohttp, httpx) then throw TransferEncodingError. Fix: convert stream errors to Ok(Bytes) SSE content, matching the pattern already used by the OpenAI streaming handler. Affected: Gemini handler, Claude SSE mapper Already correct: OpenAI handler (errors wrapped as Ok)
Contributor
Author
CI Failures — Pre-existing, Not Caused by This PRCheck Rust Code (fmt): The Build Tauri App (macOS/Windows): This PR only modifies 2 files ( |
Contributor
Author
|
Closing in favor of #2322 — it uses serde_json::json!() for proper JSON escaping instead of manual format!() + replace(), which is more robust. |
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.
Problem
HTTP clients using aiohttp/httpx (e.g. OpenWebUI) receive:
Root Cause
When SSE streams encounter upstream errors (connection drop, timeout), the Gemini handler and Claude SSE mapper use
yield Err(...)insideasync_stream::stream!. ThisErrpropagates throughBody::from_stream(), causing hyper to abort HTTP/1.1 chunked transfer encoding without sending the final0\r\n\r\nterminator chunk.HTTP clients that validate chunked encoding integrity then throw
TransferEncodingError.Fix
Convert stream errors from
Err(...)toOk(Bytes)containing SSE-formatted error content. This matches the pattern already used by the OpenAI streaming handler (line ~282 instreaming.rs), which correctly wraps errors asOk(error_json_bytes).The stream terminates cleanly with
None, hyper sends the proper0\r\n\r\nterminator, and the client receives a well-formed HTTP response with error details.Changes
handlers/gemini.rsyield Err(format!("Stream error: {}", e))yield Ok(Bytes::from(error_json_sse))mappers/claude/mod.rsyield Err(format!("Stream error: {}", e))yield Ok(Bytes::from(error_event_sse))Already correct: OpenAI handler wraps errors as
Ok(Bytes)with JSON error payload +[DONE].