Repository navigation
fix(llm): read Google's alt=sse stream, send the API key in a header - #482
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With a Gemini API key (not OAuth),
GoogleProvider::streamrequested…:streamGenerateContent?alt=ssebut 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.functionCallparts were never read, so tools never ran on this route even thoughbuild_bodysends them.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, notdata: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 andturn.stream_errorlogs on any connect failure or timeout.Fix
codeassist::gemini_sse, the SSE reader Code Assist already uses for the sameGenerateContentResponseevents. 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 thex-goog-api-keyheader on bothstreamandcomplete.events_from_google_stream_body) andGoogleReplayare removed.Tests
stream_reads_sse_events_with_the_key_in_a_header: twodata:events →"hello", usage, finalMessageStop; 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 withoutkey=.google.rshas 100% line coverage,codeassist.rs100% of lines. clippy, fmt, budgets.🤖 Generated with Claude Code