Skip to content

Commit 49857bc

Browse files
committed
Fix extraBody serialization by adding @JsonProperty annotation
Signed-off-by: CorgiBoyG <[email protected]>
1 parent e16d75a commit 49857bc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public record ChatCompletionRequest(// @formatter:off
11601160
@JsonProperty("verbosity") String verbosity,
11611161
@JsonProperty("prompt_cache_key") String promptCacheKey,
11621162
@JsonProperty("safety_identifier") String safetyIdentifier,
1163-
Map<String, Object> extraBody) {
1163+
@JsonProperty("extra_body") Map<String, Object> extraBody) {
11641164

11651165
/**
11661166
* Compact constructor that ensures extraBody is initialized as a mutable HashMap

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/ExtraBodySerializationTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.fasterxml.jackson.databind.ObjectMapper;
2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.ai.model.ModelOptionsUtils;
26+
import org.springframework.ai.openai.OpenAiChatOptions;
2527
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest;
2628

2729
import static org.assertj.core.api.Assertions.assertThat;
@@ -208,4 +210,25 @@ void testDeserializationWithComplexExtraFields() throws Exception {
208210
assertThat(request.extraBody().get("stop_token_ids")).isInstanceOf(List.class);
209211
}
210212

213+
@Test
214+
void testMergeWithExtraBody() throws Exception {
215+
// Arrange: Create OpenAiChatOptions with extraBody
216+
OpenAiChatOptions requestOptions = OpenAiChatOptions.builder()
217+
.model("test-model")
218+
.extraBody(Map.of("enable_thinking", true, "max_depth", 10))
219+
.build();
220+
221+
// Create empty ChatCompletionRequest
222+
ChatCompletionRequest request = new ChatCompletionRequest(null, null);
223+
224+
// Act: Merge options into request
225+
request = ModelOptionsUtils.merge(requestOptions, request, ChatCompletionRequest.class);
226+
227+
// Assert: Verify extraBody was successfully merged
228+
assertThat(request.extraBody()).isNotNull();
229+
assertThat(request.extraBody()).containsEntry("enable_thinking", true);
230+
assertThat(request.extraBody()).containsEntry("max_depth", 10);
231+
assertThat(request.model()).isEqualTo("test-model");
232+
}
233+
211234
}

0 commit comments

Comments
 (0)