Skip to content

[BUG] Gemini Stream Error: Incomplete JSON segment at the end #2682

Backlog

Description

@alpe12

Problem (one or two sentences)

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

  1. Zoo Code 3.98.2 in VSCodium 1.143.17232 on Windows 10 LTSC.
  2. Do a request that will result in it needing to do some work.
  3. Many errors before working, at random.

Expected result

Normal result

Actual result

Incomplete JSON segment at the end

Variations tried (optional)

No response

App Version

3.98.2

API Provider (optional)

Google Gemini

Model Used (optional)

gemini-flash-lite-latest

Zoo Code Task Links (optional)

No response

Relevant logs or errors (optional)

Activity

  1. added
    bug
    on Nov 24, 2026
  2. gh311 commented on Dec 4, 2026

    @gh311
    Contributor

    Triaged and fixed in PR #2836 (branch sdd/issue-2682, head 073013a61b9f2da17147b7845e030836fbd5f218 off current main 553936cab).

    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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions