Skip to content
Open
Show file tree
Hide file tree
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 @@ -1684,6 +1684,12 @@ public ToolCall(String id, String type, ChatCompletionFunction function) {
public record ChatCompletionFunction(// @formatter:off
@JsonProperty("name") String name,
@JsonProperty("arguments") String arguments) { // @formatter:on

public ChatCompletionFunction {
if (arguments == null) {
arguments = "{}";
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;

import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
Expand Down Expand Up @@ -295,4 +297,19 @@ void userAgentHeaderIsSentInChatCompletionRequests() throws Exception {
}
}

@Test
void nullArgumentsDefaultsToEmpty() throws JsonProcessingException {
final String TOOL_FUNCTION_NAME = "CurrentWeather";

var function = new ChatCompletionMessage.ChatCompletionFunction(TOOL_FUNCTION_NAME, null);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(function);

assertThat(function.arguments()).isEqualTo("{}");

// Verify that the JSON string contains the "arguments" field ("arguments": "{}")
assertThat(json).contains("\"arguments\":\"{}\"");
}

}