Skip to content

Commit 37b83fd

Browse files
yangjm-41ilayaperumalg
authored andcommitted
Update OpenAiChatModel.java
Auto-cherry-pick to 1.0.x Fixes #3376 Fix NullPointerException caused by empty choice.index() and Map.of() with null value - Resolve the issue where choice.index() returns an empty value, leading to a NullPointerException. - Ensure that Map.of() does not accept null values to prevent runtime errors. - Implement null checks and provide default values to handle edge cases gracefully. - Update unit tests to cover scenarios where choice.index() is empty or null. Signed-off-by: yiangjm <[email protected]>
1 parent f391ff2 commit 37b83fd

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespons
216216
Map<String, Object> metadata = Map.of(
217217
"id", chatCompletion.id() != null ? chatCompletion.id() : "",
218218
"role", choice.message().role() != null ? choice.message().role().name() : "",
219-
"index", choice.index(),
219+
"index", choice.index() != null ? choice.index() : 0,
220220
"finishReason", choice.finishReason() != null ? choice.finishReason().name() : "",
221221
"refusal", StringUtils.hasText(choice.message().refusal()) ? choice.message().refusal() : "",
222222
"annotations", choice.message().annotations() != null ? choice.message().annotations() : List.of(Map.of()));
@@ -315,7 +315,7 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
315315
Map<String, Object> metadata = Map.of(
316316
"id", id,
317317
"role", roleMap.getOrDefault(id, ""),
318-
"index", choice.index(),
318+
"index", choice.index() != null ? choice.index() : 0,
319319
"finishReason", choice.finishReason() != null ? choice.finishReason().name() : "",
320320
"refusal", StringUtils.hasText(choice.message().refusal()) ? choice.message().refusal() : "",
321321
"annotations", choice.message().annotations() != null ? choice.message().annotations() : List.of());

0 commit comments

Comments
 (0)