Skip to content

fix(deepseek): prevent incorrect ToolCall merging caused by empty id and name strings #3298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.Role;
import org.springframework.ai.deepseek.api.DeepSeekApi.ChatCompletionMessage.ToolCall;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
* Helper class to support Streaming function calling. It can merge the streamed
Expand Down Expand Up @@ -95,7 +96,7 @@ private ChatCompletionMessage merge(ChatCompletionMessage previous, ChatCompleti
throw new IllegalStateException("Currently only one tool call is supported per message!");
}
var currentToolCall = current.toolCalls().iterator().next();
if (currentToolCall.id() != null) {
if (StringUtils.hasText(currentToolCall.id())) {
if (lastPreviousTooCall != null) {
toolCalls.add(lastPreviousTooCall);
}
Expand All @@ -117,7 +118,7 @@ private ToolCall merge(ToolCall previous, ToolCall current) {
if (previous == null) {
return current;
}
String id = (current.id() != null ? current.id() : previous.id());
String id = (StringUtils.hasText(current.id()) ? current.id() : previous.id());
String type = (current.type() != null ? current.type() : previous.type());
ChatCompletionFunction function = merge(previous.function(), current.function());
return new ToolCall(id, type, function);
Expand All @@ -127,7 +128,7 @@ private ChatCompletionFunction merge(ChatCompletionFunction previous, ChatComple
if (previous == null) {
return current;
}
String name = (current.name() != null ? current.name() : previous.name());
String name = (StringUtils.hasText(current.name()) ? current.name() : previous.name());
StringBuilder arguments = new StringBuilder();
if (previous.arguments() != null) {
arguments.append(previous.arguments());
Expand Down