Repository navigation
fix(proxy): decode bytes and pass-through SSE for Google-native streamGenerateContent (#27444) #27456
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
fix(proxy): decode bytes and pass-through SSE for Google-native streamGenerateContent (#27444) #27456
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6524,6 +6524,18 @@ async def async_data_generator( | |
|
|
||
| if isinstance(chunk, BaseModel): | ||
| chunk = chunk.model_dump_json(exclude_none=True, exclude_unset=True) | ||
| elif isinstance(chunk, bytes): | ||
| # Some upstream streaming iterators (e.g. AsyncGoogleGenAIGenerateContentStreamingIterator | ||
| # for /v1beta/.../streamGenerateContent) yield raw SSE bytes from Gemini. | ||
| # Decode to str so the f-string below does not emit a Python b'...' literal, | ||
| # and pass already-formatted SSE through unchanged to avoid double "data:" prefix. | ||
| chunk = chunk.decode("utf-8", errors="replace") | ||
| if chunk.startswith(("data:", "event:", ":")): | ||
| try: | ||
| yield chunk if chunk.endswith("\n\n") else chunk + "\n\n" | ||
| except Exception as e: | ||
| yield f"data: {str(e)}\n\n" | ||
| continue | ||
|
Comment on lines
+6527
to
+6538
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The PR description lists three test plan items (curl reproduction, OpenAI-format regression, existing proxy streaming tests) and all remain unchecked. Per the team's custom standard, a PR claiming to fix a reported issue should include evidence that the issue is resolved, such as passing tests or concrete verification steps. No automated tests for the Rule Used: What: Ensure that any PR claiming to fix an issue ... (source) |
||
| elif isinstance(chunk, str) and chunk.startswith("data: "): | ||
| error_message = chunk | ||
| break | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
httpx.aiter_bytes()splits by TCP/buffer boundaries, not by SSE event boundaries. If a large Gemini SSE event is split across twoaiter_bytes()calls, the first half (e.g.data: {"candidates":...— incomplete JSON) starts withdata:and is yielded immediately with\n\nappended, making the client parse it as a complete but malformed event. The continuation bytes (e.g....}\n\n) don't start with an SSE prefix, fall through toyield f"data: {chunk}\n\n", and are double-wrapped. For typical small Gemini payloads this won't trigger, but it can occur for responses with longtextfields.