Skip to content

Commit f924502

Browse files
liugddxjitokim
authored andcommitted
Support single object tool_calls in OpenAI vendor APIs
XFY Spark and possibly other vendors implement the OpenAI interface but return tool_calls as a single object rather than an array. Add @jsonformat annotation to handle both array and single object responses in ChatCompletionMessage, making Spring AI more broadly compatible with OpenAI API implementations. Only ChatCompletionMessage.java required modification to support this alternate format while maintaining backward compatibility with the standard OpenAI API response structure. Co-authored-by: jito <[email protected]>
1 parent 7780411 commit f924502

File tree

1 file changed

+3
-1
lines changed
  • models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api

1 file changed

+3
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.Consumer;
2323
import java.util.function.Predicate;
2424

25+
import com.fasterxml.jackson.annotation.JsonFormat;
2526
import com.fasterxml.jackson.annotation.JsonIgnore;
2627
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2728
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -905,7 +906,8 @@ public record ChatCompletionMessage(// @formatter:off
905906
@JsonProperty("role") Role role,
906907
@JsonProperty("name") String name,
907908
@JsonProperty("tool_call_id") String toolCallId,
908-
@JsonProperty("tool_calls") List<ToolCall> toolCalls,
909+
@JsonProperty("tool_calls")
910+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) List<ToolCall> toolCalls,
909911
@JsonProperty("refusal") String refusal) { // @formatter:on
910912

911913
/**

0 commit comments

Comments
 (0)