From dd331a337fc0e00f3b3734b6c6ac3fc33b2d5f83 Mon Sep 17 00:00:00 2001 From: xfl12345 <17960863+xfl12345@users.noreply.github.com> Date: Mon, 25 Aug 2025 17:24:11 +0800 Subject: [PATCH 1/2] fix(spring-ai-openai): Prevent out-of-range errors when merging tool calls. Signed-off-by: xfl12345 <17960863+xfl12345@users.noreply.github.com> --- .../ai/openai/api/OpenAiStreamFunctionCallingHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java index d8fcb056f1f..3b8a2d09769 100644 --- a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java +++ b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java @@ -112,7 +112,7 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti List toolCalls = new ArrayList<>(); ToolCall lastPreviousTooCall = null; - if (previous.toolCalls() != null) { + if (previous.toolCalls() != null && previous.toolCalls().size() > 0) { lastPreviousTooCall = previous.toolCalls().get(previous.toolCalls().size() - 1); if (previous.toolCalls().size() > 1) { toolCalls.addAll(previous.toolCalls().subList(0, previous.toolCalls().size() - 1)); @@ -138,6 +138,7 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti toolCalls.add(lastPreviousTooCall); } } + return new ChatCompletionMessage(content, role, name, toolCallId, toolCalls, refusal, audioOutput, annotations); } From 9ad1b79cd9f2bd187980cb12e9b155c71344803c Mon Sep 17 00:00:00 2001 From: xfl12345 <17960863+xfl12345@users.noreply.github.com> Date: Wed, 27 Aug 2025 14:47:07 +0800 Subject: [PATCH 2/2] style(spring-ai-openai): To pass spring-javaformat lint Signed-off-by: xfl12345 <17960863+xfl12345@users.noreply.github.com> --- .../ai/openai/api/OpenAiStreamFunctionCallingHelper.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java index 3b8a2d09769..55d74fa7b52 100644 --- a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java +++ b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java @@ -112,13 +112,13 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti List toolCalls = new ArrayList<>(); ToolCall lastPreviousTooCall = null; - if (previous.toolCalls() != null && previous.toolCalls().size() > 0) { + if (previous.toolCalls() != null && !previous.toolCalls().isEmpty()) { lastPreviousTooCall = previous.toolCalls().get(previous.toolCalls().size() - 1); if (previous.toolCalls().size() > 1) { toolCalls.addAll(previous.toolCalls().subList(0, previous.toolCalls().size() - 1)); } } - if (current.toolCalls() != null && current.toolCalls().size() > 0) { + if (current.toolCalls() != null && !current.toolCalls().isEmpty()) { if (current.toolCalls().size() > 1) { throw new IllegalStateException("Currently only one tool call is supported per message!"); } @@ -138,7 +138,6 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti toolCalls.add(lastPreviousTooCall); } } - return new ChatCompletionMessage(content, role, name, toolCallId, toolCalls, refusal, audioOutput, annotations); }