Fix NoSuchElementException for safety-blocked candidates in GoogleGenAiChatModel - #6757
Open
yyyCode wants to merge 1 commit into
Open
Fix NoSuchElementException for safety-blocked candidates in GoogleGenAiChatModel#6757yyyCode wants to merge 1 commit into
yyyCode wants to merge 1 commit into
Conversation
…AiChatModel Guard the unguarded Optional.get() call on candidate.content() in responseCandidateToGeneration. When a response candidate is blocked by safety filters, content() may be an empty Optional, causing a NoSuchElementException in the non-function-call path. Replace the chained .get().parts() with .flatMap(Content::parts) to handle the absent case gracefully. Adds a regression test simulating a safety-blocked candidate with finishReason=SAFETY and no content field. Fixes spring-projects#6665 Signed-off-by: yqz <2678785492@qq.com>
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.
Summary
GoogleGenAiChatModel.responseCandidateToGenerationcallscandidate.content().get()without checking whethercontent()is present. When a response candidate is blocked by safety filters, the candidate carries afinishReason(e.g.SAFETY) but nocontent— an emptyOptional. In the non-function-call path this throwsNoSuchElementException: No value present, failing the wholecall()even though the API responded successfully.This is the same class of bug reported in #6665 for
modelVersion().get(), but on a different accessor that is not covered by #6681.Changes
candidate.content().get().parts()withcandidate.content().flatMap(Content::parts)in the non-function-call branch, so an absentcontentfalls back to an empty parts list (which then yields a single empty generation via the existing fallback).testSafetyBlockedCandidateWithoutContentthat feeds a candidate withfinishReason=SAFETYand nocontent, asserting the call returns a validChatResponseinstead of throwing.Testing
Unit test added. Note: I was unable to run
./mvnwlocally because the build enforces JDK 17.0.19+ (-XDaddTypeAnnotationsToSymbol) and my environment has 17.0.12. Please verify in CI.Fixes #6665