From e9c9ad8a91a408dae0c2b4547ae0d39ce33e4d3a Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Tue, 30 Sep 2025 23:53:55 +0100 Subject: [PATCH] Fix JsonSchemaGenerator for method input parameters - 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 Signed-off-by: Ilayaperumal Gopinathan --- .../util/json/schema/JsonSchemaGenerator.java | 2 ++ .../ai/util/json/JsonSchemaGeneratorTests.java | 18 ++++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java b/spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java index c66b9af2733..6131d40436e 100644 --- a/spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java +++ b/spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java @@ -140,6 +140,8 @@ public static String generateForMethodInput(Method method, SchemaOption... schem required.add(parameterName); } ObjectNode parameterNode = SUBTYPE_SCHEMA_GENERATOR.generateSchema(parameterType); + // Remove OpenAPI format as some LLMs (like Mistral) don't handle them. + parameterNode.remove("format"); String parameterDescription = getMethodParameterDescription(method, i); if (StringUtils.hasText(parameterDescription)) { parameterNode.put("description", parameterDescription); diff --git a/spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java b/spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java index 243ec73bbfb..6921014ad87 100644 --- a/spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java +++ b/spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java @@ -62,8 +62,7 @@ void generateSchemaForMethodWithSimpleParameters() throws Exception { "type": "string" }, "age": { - "type": "integer", - "format": "int32" + "type": "integer" } }, "required": [ @@ -265,8 +264,7 @@ void generateSchemaForMethodWithUpperCaseTypes() throws Exception { "type": "STRING" }, "age": { - "type": "INTEGER", - "format": "int32" + "type": "INTEGER" } }, "required": [ @@ -348,16 +346,13 @@ void generateSchemaForMethodWithTimeParameters() throws Exception { "type": "object", "properties": { "duration": { - "type": "string", - "format": "duration" + "type": "string" }, "localDateTime": { - "type": "string", - "format": "date-time" + "type": "string" }, "instant": { - "type": "string", - "format": "date-time" + "type": "string" } }, "required": [ @@ -387,8 +382,7 @@ void generateSchemaForMethodWithToolContext() throws Exception { "type": "string" }, "expectedDelivery": { - "type": "string", - "format": "date-time" + "type": "string" } }, "required": [