You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Gemini Stream Error: Incomplete JSON segment at the end #2682
A Gemini context stream request sometimes fails and Zoo Code shows this error: Gemini generate context stream error: Incomplete JSON segment at the end.
Context (who is affected and when)
I use the Gemini provider with a direct Google API key and the gemini-flash-lite-latest model. The error occurs at random, but it occurs more often during long tasks. Also common in gemini-flash-latest and gemini-4-hydrogen. Don't know about the other Gemini models.
Reproduction steps
Zoo Code 3.98.2 in VSCodium 1.143.17232 on Windows 10 LTSC.
Do a request that will result in it needing to do some work.
Triaged and fixed in PR #2836 (branch sdd/issue-2682, head 073013a61b9f2da17147b7845e030836fbd5f218 off current main553936cab).
Root cause: @google/genai's SSE reader (processStreamResponse) ends with Incomplete JSON segment at the end whenever the stream closes while a final event is still unframed (no trailing \n\n) - and GeminiHandler.createMessage turned that into a hard provider error, losing the events already streamed. The request-side maxOutputTokens is clamped to 20% of the context window (getModelMaxOutputTokens), so this is a stream-termination defect, not the window-as-output-cap defect fixed for LiteLLM in #2833.
The fix repairs both shapes of the failure: a first-attempt end that delivered nothing retries the request once (a redo is clean before the first yield), and an end after delivered events completes the stream with what was already received. Aborts still surface as AbortError; unrelated stream errors still fail exactly as before.
Spec-first per the TLA+ policy: new docs/architecture/tla/GeminiStreamEnd.tla (base cfg passes, 35 states; sabotage cfgs Raw, RetryOff, AbortMasked, SwallowAll each fail exactly their named invariant). Tests red-verified on the pre-fix tree; full gates green (test:core 5316, test:api 6427, tsc, eslint, prettier, TLC).
Activity
gh311 commentedon Dec 4, 2026
Triaged and fixed in PR #2836 (branch
sdd/issue-2682, head073013a61b9f2da17147b7845e030836fbd5f218off currentmain553936cab).Root cause:
@google/genai's SSE reader (processStreamResponse) ends withIncomplete JSON segment at the endwhenever the stream closes while a final event is still unframed (no trailing\n\n) - andGeminiHandler.createMessageturned that into a hard provider error, losing the events already streamed. The request-sidemaxOutputTokensis clamped to 20% of the context window (getModelMaxOutputTokens), so this is a stream-termination defect, not the window-as-output-cap defect fixed for LiteLLM in #2833.The fix repairs both shapes of the failure: a first-attempt end that delivered nothing retries the request once (a redo is clean before the first yield), and an end after delivered events completes the stream with what was already received. Aborts still surface as
AbortError; unrelated stream errors still fail exactly as before.Spec-first per the TLA+ policy: new
docs/architecture/tla/GeminiStreamEnd.tla(base cfg passes, 35 states; sabotage cfgsRaw,RetryOff,AbortMasked,SwallowAlleach fail exactly their named invariant). Tests red-verified on the pre-fix tree; full gates green (test:core5316,test:api6427, tsc, eslint, prettier, TLC).