Skip to content

fix(llm): read Google's alt=sse stream, send the API key in a header - #482

Merged
mrnasil merged 2 commits into
mainfrom
fix/google-sse-stream
Nov 25, 2026
Merged

mrnasil merged 2 commits into
mainfrom
fix/google-sse-stream

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Problem

With a Gemini API key (not OAuth), GoogleProvider::stream requested …:streamGenerateContent?alt=sse but parsed the reply as the JSON-array format ([{…}\n,{…}]). An SSE body (data: {…}\r\n\r\n) never matched: the whole body became one item, failed to parse, and was skipped.

  • Every streamed turn returned no text and no error.
  • functionCall parts were never read, so tools never ran on this route even though build_body sends them.
  • The body was buffered whole (resp.text()), so nothing streamed.

The tests missed it because they served a bare JSON object as application/json; one fixture even said "Gemini stream is a JSON array of chunks, not data: SSE".

Separately, the key travelled as ?key=…. reqwest puts the URL in its error text (error sending request for url (…?key=AIza…)), which reached the TUI and turn.stream_error logs on any connect failure or timeout.

Fix

  • The stream goes through codeassist::gemini_sse, the SSE reader Code Assist already uses for the same GenerateContentResponse events. It handles text deltas, function calls (→ ToolUse), usage, MessageStop, and multi-byte characters split across chunks (LineBuffer).
  • build_url(model) / build_complete_url(model) no longer take the key. It is sent as the x-goog-api-key header on both stream and complete.
  • The array parser (events_from_google_stream_body) and GoogleReplay are removed.

Tests

  • stream_reads_sse_events_with_the_key_in_a_header: two data: events → "hello", usage, final MessageStop; the request line has no key and the header carries it.
  • stream_turns_function_calls_into_tool_use.
  • complete_sends_the_key_in_a_header; HTTP error body surfaced; URL tests assert exact URLs without key=.
  • The loopback fixture now serves real SSE.
  • All 553 llm lib tests pass. google.rs has 100% line coverage, codeassist.rs 100% of lines. clippy, fmt, budgets.

🤖 Generated with Claude Code

mrnasil and others added 2 commits November 25, 2026 16:14
The Gemini API-key route asked for `streamGenerateContent?alt=sse` but
parsed the body as a JSON array: `data: {...}` lines failed to parse
and were skipped, so every turn came back empty with no error. Function
calls were never read either, so tools never ran. The body was also
buffered whole instead of streamed.

- Stream through the Gemini SSE reader Code Assist already uses
  (`gemini_sse`): text, function calls, usage, split UTF-8 safe.
- Move the key from `?key=` to `x-goog-api-key`: reqwest puts the URL
  in its error text, which reached the TUI and the logs.
- Drop the array parser and replay stream; the test fixture that served
  a bare JSON object as "Gemini SSE" now serves real `data:` events.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 13:27
@mrnasil
mrnasil merged commit 974f2d7 into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/google-sse-stream branch November 25, 2026 13:45
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant