Skip to content

Commit 30001ac

Browse files
Fix JsonSchemaGenerator for method input parameters (#4524)
- For method input parameters, if the JSON schema generator adds OpenAPI "format" property, some LLMs (ex: Mistral) fail to process the request. Hence, removing this specific property from the JSON schema - This issue was uncovered by MistralAiChatModelIT#chatMemoryWithTools where the method toolcallback is used - Update tests to remove "format" from the expected JSON schema Auto-cherry-pick to 1.0.x Fixes #4524 Signed-off-by: Ilayaperumal Gopinathan <[email protected]>
1 parent 4408c46 commit 30001ac

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public static String generateForMethodInput(Method method, SchemaOption... schem
140140
required.add(parameterName);
141141
}
142142
ObjectNode parameterNode = SUBTYPE_SCHEMA_GENERATOR.generateSchema(parameterType);
143+
// Remove OpenAPI format as some LLMs (like Mistral) don't handle them.
144+
parameterNode.remove("format");
143145
String parameterDescription = getMethodParameterDescription(method, i);
144146
if (StringUtils.hasText(parameterDescription)) {
145147
parameterNode.put("description", parameterDescription);

spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ void generateSchemaForMethodWithSimpleParameters() throws Exception {
6262
"type": "string"
6363
},
6464
"age": {
65-
"type": "integer",
66-
"format": "int32"
65+
"type": "integer"
6766
}
6867
},
6968
"required": [
@@ -265,8 +264,7 @@ void generateSchemaForMethodWithUpperCaseTypes() throws Exception {
265264
"type": "STRING"
266265
},
267266
"age": {
268-
"type": "INTEGER",
269-
"format": "int32"
267+
"type": "INTEGER"
270268
}
271269
},
272270
"required": [
@@ -348,16 +346,13 @@ void generateSchemaForMethodWithTimeParameters() throws Exception {
348346
"type": "object",
349347
"properties": {
350348
"duration": {
351-
"type": "string",
352-
"format": "duration"
349+
"type": "string"
353350
},
354351
"localDateTime": {
355-
"type": "string",
356-
"format": "date-time"
352+
"type": "string"
357353
},
358354
"instant": {
359-
"type": "string",
360-
"format": "date-time"
355+
"type": "string"
361356
}
362357
},
363358
"required": [
@@ -387,8 +382,7 @@ void generateSchemaForMethodWithToolContext() throws Exception {
387382
"type": "string"
388383
},
389384
"expectedDelivery": {
390-
"type": "string",
391-
"format": "date-time"
385+
"type": "string"
392386
}
393387
},
394388
"required": [

0 commit comments

Comments
 (0)