1717package org .springframework .ai .openai .chat ;
1818
1919import com .fasterxml .jackson .annotation .JsonProperty ;
20+ import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
2021import com .fasterxml .jackson .core .JacksonException ;
2122import com .fasterxml .jackson .core .JsonProcessingException ;
2223import com .fasterxml .jackson .databind .DeserializationFeature ;
2324import com .fasterxml .jackson .databind .JsonMappingException ;
2425import com .fasterxml .jackson .databind .ObjectMapper ;
26+ import org .junit .Assert ;
2527import org .junit .jupiter .api .Test ;
2628import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
2729import org .slf4j .Logger ;
4042import org .springframework .boot .test .context .SpringBootTest ;
4143import org .springframework .context .annotation .Bean ;
4244
45+ import static org .assertj .core .api .Assertions .as ;
4346import 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
0 commit comments