Skip to content

Commit 836c4fd

Browse files
committed
fix bean parser and tests
1 parent 944c4eb commit 836c4fd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

spring-ai-core/src/main/java/org/springframework/ai/parser/BeanOutputParser.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,33 @@ private void generateSchema() {
9797
public T parse(String text) {
9898
try {
9999
// If the response is a JSON Schema, extract the properties and use them as
100-
// the
101-
// response.
102-
Map<String, Object> map = this.objectMapper.readValue(text, Map.class);
103-
if (map.containsKey("$schema")) {
104-
text = this.objectMapper.writeValueAsString(map.get("properties"));
105-
}
100+
// the response.
101+
text = this.jsonSchemaToInstance(text);
106102
return (T) this.objectMapper.readValue(text, this.clazz);
107103
}
108104
catch (JsonProcessingException e) {
109105
throw new RuntimeException(e);
110106
}
111107
}
112108

109+
/**
110+
* If the response is a JSON Schema, extract the properties and use them as the
111+
* response.
112+
* @param text
113+
* @return
114+
*/
115+
private String jsonSchemaToInstance(String text) {
116+
try {
117+
Map<String, Object> map = this.objectMapper.readValue(text, Map.class);
118+
if (map.containsKey("$schema")) {
119+
return this.objectMapper.writeValueAsString(map.get("properties"));
120+
}
121+
}
122+
catch (Exception e) {
123+
}
124+
return text;
125+
}
126+
113127
/**
114128
* Configures and returns an object mapper for JSON operations.
115129
* @return Configured object mapper.

spring-ai-core/src/test/java/org/springframework/ai/parser/BeanOutputParserTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void shouldReturnFormatContainingResponseInstructionsAndJsonSchema() {
6969
"""
7070
Your response should be in JSON format.
7171
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
72+
Do not include markdown code blocks in your response.
7273
Here is the JSON Schema instance your output must adhere to:
7374
```{
7475
"$schema" : "https://json-schema.org/draft/2020-12/schema",

0 commit comments

Comments
 (0)