Skip to content

Commit 8afc110

Browse files
committed
fix(deepseek): prevent incorrect ToolCall merging caused by empty id and name strings
Signed-off-by: Guo <[email protected]>
1 parent 3919204 commit 8afc110

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

models/spring-ai-deepseek/src/main/java/org/springframework/ai/deepseek/api/DeepSeekStreamFunctionCallingHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.Role;
2828
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.ToolCall;
2929
import org.springframework.util.CollectionUtils;
30+
import org.springframework.util.StringUtils;
3031

3132
/**
3233
* Helper class to support Streaming function calling. It can merge the streamed
@@ -95,7 +96,7 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti
9596
throw new IllegalStateException("Currently only one tool call is supported per message!");
9697
}
9798
var currentToolCall = current.toolCalls().iterator().next();
98-
if (currentToolCall.id() != null) {
99+
if (StringUtils.hasText(currentToolCall.id())) {
99100
if (lastPreviousTooCall != null) {
100101
toolCalls.add(lastPreviousTooCall);
101102
}
@@ -117,7 +118,7 @@ private ToolCall merge(ToolCall previous, ToolCall current) {
117118
if (previous == null) {
118119
return current;
119120
}
120-
String id = (current.id() != null ? current.id() : previous.id());
121+
String id = (StringUtils.hasText(current.id()) ? current.id() : previous.id());
121122
String type = (current.type() != null ? current.type() : previous.type());
122123
ChatCompletionFunction function = merge(previous.function(), current.function());
123124
return new ToolCall(id, type, function);
@@ -127,7 +128,7 @@ private ChatCompletionFunction merge(ChatCompletionFunction previous, ChatComple
127128
if (previous == null) {
128129
return current;
129130
}
130-
String name = (current.name() != null ? current.name() : previous.name());
131+
String name = (StringUtils.hasText(current.name()) ? current.name() : previous.name());
131132
StringBuilder arguments = new StringBuilder();
132133
if (previous.arguments() != null) {
133134
arguments.append(previous.arguments());

0 commit comments

Comments
 (0)