Skip to content

Commit c1fc687

Browse files
committed
Update OpenAiChatModelResponseFormatIT with JSON order usecase
- Verify that the BeanOutputConverter converts with the right order as specified in the JSON schema Update the BeanOutputConverterTest's test case to tweak the order for validation
1 parent 79266be commit c1fc687

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelResponseFormatIT.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package org.springframework.ai.openai.chat;
1818

1919
import com.fasterxml.jackson.annotation.JsonProperty;
20+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
2021
import com.fasterxml.jackson.core.JacksonException;
2122
import com.fasterxml.jackson.core.JsonProcessingException;
2223
import com.fasterxml.jackson.databind.DeserializationFeature;
2324
import com.fasterxml.jackson.databind.JsonMappingException;
2425
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import org.junit.Assert;
2527
import org.junit.jupiter.api.Test;
2628
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2729
import org.slf4j.Logger;
@@ -40,10 +42,12 @@
4042
import org.springframework.boot.test.context.SpringBootTest;
4143
import org.springframework.context.annotation.Bean;
4244

45+
import static org.assertj.core.api.Assertions.as;
4346
import static org.assertj.core.api.Assertions.assertThat;
4447

4548
/**
4649
* @author Christian Tzolov
50+
* @author Ilayaperumal Gopinathan
4751
*/
4852
@SpringBootTest(classes = OpenAiChatModelResponseFormatIT.Config.class)
4953
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
@@ -140,11 +144,13 @@ void jsonSchema() throws JsonMappingException, JsonProcessingException {
140144
@Test
141145
void jsonSchemaBeanConverter() throws JsonMappingException, JsonProcessingException {
142146

147+
@JsonPropertyOrder({ "steps", "final_answer" })
143148
record MathReasoning(@JsonProperty(required = true, value = "steps") Steps steps,
144149
@JsonProperty(required = true, value = "final_answer") String finalAnswer) {
145150

146151
record Steps(@JsonProperty(required = true, value = "items") Items[] items) {
147152

153+
@JsonPropertyOrder({ "output", "explanation" })
148154
record Items(@JsonProperty(required = true, value = "explanation") String explanation,
149155
@JsonProperty(required = true, value = "output") String output) {
150156

@@ -156,9 +162,45 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan
156162

157163
var outputConverter = new BeanOutputConverter<>(MathReasoning.class);
158164

165+
var expectedJsonSchema = """
166+
{
167+
"$schema" : "https://json-schema.org/draft/2020-12/schema",
168+
"type" : "object",
169+
"properties" : {
170+
"steps" : {
171+
"type" : "object",
172+
"properties" : {
173+
"items" : {
174+
"type" : "array",
175+
"items" : {
176+
"type" : "object",
177+
"properties" : {
178+
"output" : {
179+
"type" : "string"
180+
},
181+
"explanation" : {
182+
"type" : "string"
183+
}
184+
},
185+
"required" : [ "output", "explanation" ],
186+
"additionalProperties" : false
187+
}
188+
}
189+
},
190+
"required" : [ "items" ],
191+
"additionalProperties" : false
192+
},
193+
"final_answer" : {
194+
"type" : "string"
195+
}
196+
},
197+
"required" : [ "steps", "final_answer" ],
198+
"additionalProperties" : false
199+
}""";
159200
var jsonSchema1 = outputConverter.getJsonSchema();
160201

161-
System.out.println(jsonSchema1);
202+
assertThat(jsonSchema1).isNotNull();
203+
assertThat(jsonSchema1).isEqualTo(expectedJsonSchema);
162204

163205
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
164206
OpenAiChatOptions.builder()
@@ -174,11 +216,16 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan
174216

175217
logger.info("Response content: {}", content);
176218

177-
MathReasoning mathReasoning = outputConverter.convert(content);
219+
assertThat(isValidJson(content)).isTrue();
178220

179-
System.out.println(mathReasoning);
221+
// Check if the order is correct as specified in the schema. Steps should come
222+
// first before final answer.
223+
assertThat(content.startsWith("{\"steps\":{\"items\":["));
180224

181-
assertThat(isValidJson(content)).isTrue();
225+
MathReasoning mathReasoning = outputConverter.convert(content);
226+
227+
assertThat(mathReasoning).isNotNull();
228+
logger.info(mathReasoning.toString());
182229
}
183230

184231
@SpringBootConfiguration

spring-ai-core/src/test/java/org/springframework/ai/converter/BeanOutputConverterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ String getSomeString() {
120120
record TestClassWithJsonPropertyOrder(
121121
@JsonProperty("string_property") @JsonPropertyDescription("string_property_description") String someString,
122122

123-
@JsonProperty(required = true, value = "foo_property") String foo,
123+
@JsonProperty(required = true, value = "bar_property") String bar,
124124

125-
@JsonProperty(required = true, value = "bar_property") String bar) {
125+
@JsonProperty(required = true, value = "foo_property") String foo) {
126126
}
127127

128128
@Nested

0 commit comments

Comments
 (0)