fix(proxy): decode bytes and pass-through SSE for Google-native streamGenerateContent (#27444)

This commit is contained in:
Tai An 2026-05-08 03:21:38 -07:00
parent 98cd057f38
commit b57b2a50bb

View file

@ -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
elif isinstance(chunk, str) and chunk.startswith("data: "):
error_message = chunk
break