diff --git a/spring-ai-core/src/main/java/org/springframework/ai/parser/BeanOutputParser.java b/spring-ai-core/src/main/java/org/springframework/ai/parser/BeanOutputParser.java index 8c8f4326a..a4d53a22a 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/parser/BeanOutputParser.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/parser/BeanOutputParser.java @@ -97,12 +97,8 @@ public class BeanOutputParser implements OutputParser { public T parse(String text) { try { // If the response is a JSON Schema, extract the properties and use them as - // the - // response. - Map map = this.objectMapper.readValue(text, Map.class); - if (map.containsKey("$schema")) { - text = this.objectMapper.writeValueAsString(map.get("properties")); - } + // the response. + text = this.jsonSchemaToInstance(text); return (T) this.objectMapper.readValue(text, this.clazz); } catch (JsonProcessingException e) { @@ -110,6 +106,24 @@ public class BeanOutputParser implements OutputParser { } } + /** + * If the response is a JSON Schema, extract the properties and use them as the + * response. + * @param text + * @return + */ + private String jsonSchemaToInstance(String text) { + try { + Map map = this.objectMapper.readValue(text, Map.class); + if (map.containsKey("$schema")) { + return this.objectMapper.writeValueAsString(map.get("properties")); + } + } + catch (Exception e) { + } + return text; + } + /** * Configures and returns an object mapper for JSON operations. * @return Configured object mapper. diff --git a/spring-ai-core/src/test/java/org/springframework/ai/parser/BeanOutputParserTest.java b/spring-ai-core/src/test/java/org/springframework/ai/parser/BeanOutputParserTest.java index 28c47e0a7..aa825198a 100644 --- a/spring-ai-core/src/test/java/org/springframework/ai/parser/BeanOutputParserTest.java +++ b/spring-ai-core/src/test/java/org/springframework/ai/parser/BeanOutputParserTest.java @@ -69,6 +69,7 @@ class BeanOutputParserTest { """ Your response should be in JSON format. Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation. + Do not include markdown code blocks in your response. Here is the JSON Schema instance your output must adhere to: ```{ "$schema" : "https://json-schema.org/draft/2020-12/schema",