Skip to content

Commit d5423f2

Browse files
g00glen00bmarkpollack
authored andcommitted
added @JsonIgnore to ResponseFormat.schema to avoid exposing internal properties (#2969)
Signed-off-by: g00glen00b <[email protected]>
1 parent 78d90cd commit d5423f2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.Objects;
2121

22+
import com.fasterxml.jackson.annotation.JsonIgnore;
2223
import com.fasterxml.jackson.annotation.JsonInclude;
2324
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2425
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -62,6 +63,7 @@ public class ResponseFormat {
6263
@JsonProperty("json_schema")
6364
private JsonSchema jsonSchema = null;
6465

66+
@JsonIgnore
6567
private String schema;
6668

6769
public ResponseFormat() {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,49 @@ void jsonSchema() throws JsonMappingException, JsonProcessingException {
139139
assertThat(isValidJson(content)).isTrue();
140140
}
141141

142+
@Test
143+
void jsonSchemaThroughIndividualSetters() throws JsonMappingException, JsonProcessingException {
144+
145+
var jsonSchema = """
146+
{
147+
"type": "object",
148+
"properties": {
149+
"steps": {
150+
"type": "array",
151+
"items": {
152+
"type": "object",
153+
"properties": {
154+
"explanation": { "type": "string" },
155+
"output": { "type": "string" }
156+
},
157+
"required": ["explanation", "output"],
158+
"additionalProperties": false
159+
}
160+
},
161+
"final_answer": { "type": "string" }
162+
},
163+
"required": ["steps", "final_answer"],
164+
"additionalProperties": false
165+
}
166+
""";
167+
168+
var responseFormat = new ResponseFormat();
169+
responseFormat.setType(ResponseFormat.Type.JSON_SCHEMA);
170+
responseFormat.setSchema(jsonSchema);
171+
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
172+
OpenAiChatOptions.builder().model(ChatModel.GPT_4_O_MINI).responseFormat(responseFormat).build());
173+
174+
ChatResponse response = this.openAiChatModel.call(prompt);
175+
176+
assertThat(response).isNotNull();
177+
178+
String content = response.getResult().getOutput().getText();
179+
180+
logger.info("Response content: {}", content);
181+
182+
assertThat(isValidJson(content)).isTrue();
183+
}
184+
142185
@Test
143186
void jsonSchemaBeanConverter() throws JsonMappingException, JsonProcessingException {
144187

0 commit comments

Comments
 (0)