Skip to content

Commit b833451

Browse files
committed
fix(openai): prevent incorrect ToolCall merging caused by empty id strings
Empty tool call id strings causing improper TooCall merging during OpenAI stream processing, resulting in incorrect function calling responses. This fix ensures both null and empty strings are properly validated before merging operations. Relates gh-2417,gh-2423 Signed-off-by: Ma Wenhao <[email protected]>
1 parent 8329402 commit b833451

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.ai.openai.api.OpenAiApi.LogProbs;
3232
import org.springframework.ai.openai.api.OpenAiApi.Usage;
3333
import org.springframework.util.CollectionUtils;
34+
import org.springframework.util.StringUtils;
3435

3536
/**
3637
* Helper class to support Streaming function calling.
@@ -111,7 +112,7 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti
111112
throw new IllegalStateException("Currently only one tool call is supported per message!");
112113
}
113114
var currentToolCall = current.toolCalls().iterator().next();
114-
if (currentToolCall.id() != null) {
115+
if (StringUtils.hasText(currentToolCall.id())) {
115116
if (lastPreviousTooCall != null) {
116117
toolCalls.add(lastPreviousTooCall);
117118
}
@@ -133,7 +134,7 @@ private ToolCall merge(ToolCall previous, ToolCall current) {
133134
if (previous == null) {
134135
return current;
135136
}
136-
String id = (current.id() != null ? current.id() : previous.id());
137+
String id = (StringUtils.hasText(current.id()) ? current.id() : previous.id());
137138
String type = (current.type() != null ? current.type() : previous.type());
138139
ChatCompletionFunction function = merge(previous.function(), current.function());
139140
return new ToolCall(id, type, function);
@@ -143,7 +144,7 @@ private ChatCompletionFunction merge(ChatCompletionFunction previous, ChatComple
143144
if (previous == null) {
144145
return current;
145146
}
146-
String name = (current.name() != null ? current.name() : previous.name());
147+
String name = (StringUtils.hasText(current.name()) ? current.name() : previous.name());
147148
StringBuilder arguments = new StringBuilder();
148149
if (previous.arguments() != null) {
149150
arguments.append(previous.arguments());

0 commit comments

Comments
 (0)