Skip to content

GH-3657: Fix DeepSeek tool call content null issue #3817

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -76,6 +76,7 @@
* backed by {@link DeepSeekApi}.
*
* @author Geng Rong
* @author Dongha Koo
*/
public class DeepSeekChatModel implements ChatModel {

Expand Down Expand Up @@ -338,7 +339,9 @@ private Generation buildGeneration(Choice choice, Map<String, Object> metadata)

String textContent = choice.message().content();
String reasoningContent = choice.message().reasoningContent();

if (textContent == null && !toolCalls.isEmpty()) {
textContent = "__tool_call__";
}
DeepSeekAssistantMessage assistantMessage = new DeepSeekAssistantMessage(textContent, reasoningContent,
metadata, toolCalls);
return new Generation(assistantMessage, generationMetadataBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package org.springframework.ai.chat.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.Generation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -28,6 +31,7 @@
* Unit tests for {@link ChatClientResponse}.
*
* @author Thomas Vitale
* @author Dongha Koo
*/
class ChatClientResponseTests {

Expand Down Expand Up @@ -82,4 +86,15 @@ void whenMutateThenImmutableContext() {
assertThat(response.context()).containsEntry("key", "value");
}

@Test
void whenAssistantMessageHasOnlyToolCalls_thenContentIsToolCallMarker() {
var toolCall = new AssistantMessage.ToolCall("tool-1", "function", "doSomething", "{\"foo\":\"bar\"}");
var assistantMessage = new AssistantMessage(null, Map.of(), List.of(toolCall), List.of());

assertThat(assistantMessage.getDisplayText()).isEqualTo("__tool_call__");

var generation = new Generation(assistantMessage);
assertThat(generation.getOutput().getDisplayText()).isEqualTo("__tool_call__");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @author Mark Pollack
* @author Christian Tzolov
* @author Dongha Koo
* @since 1.0.0
*/
public class AssistantMessage extends AbstractMessage implements MediaContent {
Expand Down Expand Up @@ -104,4 +105,16 @@ public record ToolCall(String id, String type, String name, String arguments) {

}

/**
* Returns a safe, non-null text representation. If text is null or empty and
* toolCalls exist, returns "__tool_call__".
*/
public String getDisplayText() {
String text = super.getText();
if ((text == null || text.trim().isEmpty()) && this.hasToolCalls()) {
return "__tool_call__";
}
return (text != null) ? text : "";
}

}