File tree Expand file tree Collapse file tree 2 files changed +21
-6
lines changed
main/java/org/springframework/ai/parser
test/java/org/springframework/ai/parser Expand file tree Collapse file tree 2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -97,19 +97,33 @@ private void generateSchema() {
97
97
public T parse (String text ) {
98
98
try {
99
99
// 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 );
106
102
return (T ) this .objectMapper .readValue (text , this .clazz );
107
103
}
108
104
catch (JsonProcessingException e ) {
109
105
throw new RuntimeException (e );
110
106
}
111
107
}
112
108
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
+
113
127
/**
114
128
* Configures and returns an object mapper for JSON operations.
115
129
* @return Configured object mapper.
Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ public void shouldReturnFormatContainingResponseInstructionsAndJsonSchema() {
69
69
"""
70
70
Your response should be in JSON format.
71
71
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.
72
73
Here is the JSON Schema instance your output must adhere to:
73
74
```{
74
75
"$schema" : "https://json-schema.org/draft/2020-12/schema",
You can’t perform that action at this time.
0 commit comments