Fix google ai empty stream body#563
Merged
brainlid merged 5 commits intoJun 9, 2026
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
When a streaming request to Gemini returns 200 OK with zero delta
chunks (e.g. immediate finish without content, or all chunks filtered
by the SSE decoder), `Req.Response.body` stays as the binary default
`""` instead of the list of `%MessageDelta{}` the downstream code
expects. `Utils.handle_stream_fn/3` only converts `""` to `[]` on the
first `{:data, ...}` callback — if no callback fires, the conversion
is skipped.
The body then reaches `List.flatten(data)` and crashes with
`FunctionClauseError` on `:lists.flatten("")`, killing the upstream
Task. Symptom in production:
** (FunctionClauseError) no function clause matching in :lists.flatten/1
:lists.flatten("") at lists.erl:1145
LangChain.ChatModels.ChatGoogleAI.do_api_request/3 at line 634
Add an `is_list(data)` guard to the existing 200 OK clause and a
sibling clause that returns
`{:error, %LangChainError{type: "empty_stream"}}` for the zero-chunk
case. The upstream chain already knows how to surface
`%LangChainError{}` as a clean status change instead of crashing.
Regression test asserts the structured error is returned instead of
the FunctionClauseError.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a guard and structured error handling for Gemini streaming calls that return HTTP 200 but provide no streamed chunks, preventing a crash and validating the behavior with a regression test.
Changes:
- Add a regression test for a streaming 200 response with an empty body to ensure a structured
empty_streamerror is returned. - Update the streaming response handling to only flatten list bodies, and return a
LangChainErrorwhen the stream ends without deltas.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/chat_models/chat_google_ai_test.exs | Adds a regression test covering the “200 + empty streaming body” scenario. |
| lib/chat_models/chat_google_ai.ex | Adds guarded matching for streamed bodies and returns a structured empty_stream error instead of crashing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Owner
|
Thanks @nelsonkopliku! |
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.
When a streaming request to Gemini returns 200 OK with zero delta chunks (e.g. immediate finish without content, or all chunks filtered by the SSE decoder),
Req.Response.bodystays as the binary default""instead of the list of%MessageDelta{}the downstream code expects.Utils.handle_stream_fn/3only converts""to[]on the first{:data, ...}callback — if no callback fires, the conversion is skipped.The body then reaches
List.flatten(data)and crashes withFunctionClauseErroron:lists.flatten(""), killing the upstream Task. Symptom in production:Add an
is_list(data)guard to the existing 200 OK clause and a sibling clause that returns{:error, %LangChainError{type: "empty_stream"}}for the zero-chunk case. The upstream chain already knows how to surface%LangChainError{}as a clean status change instead of crashing.Regression test asserts the structured error is returned instead of the FunctionClauseError.