Fix googleai malformed function call crash#548
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses a crash in ChatGoogleAI streaming mode when Gemini returns candidates with finishReason: "MALFORMED_FUNCTION_CALL" (or other unexpected candidate shapes), ensuring streaming returns an error tuple instead of raising during delta reindexing.
Changes:
- Detect
{:error, %LangChainError{}}entries in streamed candidate deltas and return the first error early (avoidsKeyErroron:indexduring reindexing). - Extract the delta reindexing logic into a dedicated
reindex_deltas/1helper. - Add a test covering a streamed response body containing a mix of deltas and error tuples.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/chat_models/chat_google_ai.ex |
Prevents streaming reduce/reindex from crashing when an error tuple appears among streamed candidates; refactors reindexing into a helper. |
test/chat_models/chat_google_ai_test.exs |
Adds regression test ensuring streaming returns an error instead of crashing when malformed-function-call candidates appear. |
Comments suppressed due to low confidence (1)
test/chat_models/chat_google_ai_test.exs:2036
- Same as above: streamed
%MessageDelta{}fromChatGoogleAIuses a single%ContentPart{}(ornil) forcontent, not a list. Aligning this fixture with actual streaming output will keep the test representative and reduce the risk of false confidence.
delta2 = %LangChain.MessageDelta{
content: [%LangChain.Message.ContentPart{type: :text, content: "Part 2"}],
index: 0,
role: :assistant,
status: :incomplete
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
313774a to
535c2b0
Compare
|
Hey @brainlid, first off thanks for this library and for accepting my previous, and first contribution! I am wondering whether this PR is making sense for users of the lib from your perspective - I at least encountered the issue. A note: I have another fix about google ai here nelsonkopliku#3. |
|
Thanks @nelsonkopliku! |
When Gemini (tested with gemini-2.5-flash) cannot form a valid function call, it returns a candidate with the following shape:
This in turn ends up in the last
do_process_responsematchlangchain/lib/chat_models/chat_google_ai.ex
Line 902 in 8a5c2e6
and returns an
{:error, %LangChainError{}}tuple and making the deltas reindexing reduction with a** (KeyError) key :index not found in...This PR makes sure the streaming does not crash and bubbles up the (first encoundered) error.
Caveats:
MessageDeltas and error tuples, although IRL testing always manifested with a single entry list with the error tupleMessageDeltas and error tuples" case is real, there might be different considerations to make like:MessageDeltas? would it be valuable to not "miss" in the best effort failure handling?LangChainErrorstruct that contains a list of manyLangChainError?I don't have evidences, yet, about other cases so I went for the simplest and less intrusive approach.