Skip to content

Conversation

@liqingdong
Copy link
Contributor

Problem

In the after method of PromptChatMemoryAdvisor, there was an issue with the conditional logic where the else-if branch was unreachable. The original code structure had redundant null checks and didn't properly prioritize the single-result case check.

Solution

Reordered the conditional statements to:

  1. First check for single-result case with complete null safety
  2. Then handle the multi-result case as the fallback

This approach is more efficient and maintains better null safety while handling both cases appropriately.

Changes

- if (chatClientResponse.chatResponse() != null) {
-     assistantMessages = chatClientResponse.chatResponse()
-         .getResults()
-         .stream()
-         .map(g -> (Message) g.getOutput())
-         .toList();
- }
- else if (chatClientResponse.chatResponse() != null && 
-          chatClientResponse.chatResponse().getResult() != null &&
-          chatClientResponse.chatResponse().getResult().getOutput() != null) {
-     assistantMessages = List.of((Message) chatClientResponse.chatResponse().getResult().getOutput());
- }

+ if (chatClientResponse.chatResponse() != null && 
+     chatClientResponse.chatResponse().getResult() != null &&
+     chatClientResponse.chatResponse().getResult().getOutput() != null) {
+     assistantMessages = List.of((Message) chatClientResponse.chatResponse().getResult().getOutput());
+ }
+ else if (chatClientResponse.chatResponse() != null) {
+     assistantMessages = chatClientResponse.chatResponse()
+         .getResults()
+         .stream()
+         .map(g -> (Message) g.getOutput())
+         .toList();
+ }

@ilayaperumalg
Copy link
Member

@liqingdong Thanks for fixing the conditional logic!

@sobychacko sobychacko merged commit 5010916 into spring-projects:main May 30, 2025
2 checks passed
sobychacko pushed a commit that referenced this pull request May 30, 2025
)

Fixes: #3395

Auto-cherry-pick to 1.0.x

Signed-off-by: aliqingdong <[email protected]>
Co-authored-by: aliqingdong <[email protected]>
namsoo2 pushed a commit to namsoo2/spring-ai that referenced this pull request Jun 9, 2025
…ring-projects#3395)

Fixes: spring-projects#3395

Auto-cherry-pick to 1.0.x

Signed-off-by: aliqingdong <[email protected]>
Co-authored-by: aliqingdong <[email protected]>
Signed-off-by: minsoo.nam <[email protected]>
chedim pushed a commit to couchbaselabs/spring-ai that referenced this pull request Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants