Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 +6533 to +6538

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Partial chunk may corrupt SSE framing for large payloads

httpx.aiter_bytes() splits by TCP/buffer boundaries, not by SSE event boundaries. If a large Gemini SSE event is split across two aiter_bytes() calls, the first half (e.g. data: {"candidates":... — incomplete JSON) starts with data: and is yielded immediately with \n\n appended, 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 to yield 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 long text fields.

Comment on lines +6527 to +6538

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test plan items unchecked — fix is unverified

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 bytes decode path appear to have been added.

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
Expand Down
Loading