Bug description
When generating a json schema for a function callback input type in Kotlin, if a property is nullable in the Kotlin language like this field1
:
class MyFunction() : java.util.function.Function<MyFunction.Request, String?> {
@JvmRecord
data class Request(
val field1: Int?)
override fun apply(r: Request): String {
return "OK"
}
}
@Bean
fun myFunctionBean(): FunctionCallback =
FunctionCallback.builder()
.function("myFunctionBean", MyFunction())
.inputType(MyFunction.Request::class.java)
.description("description of my function")
.build()
the json schema generated will be like :
{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"field1" : {
"type" : [ "integer", "null" ],
"format" : "int32",
}
}
}
But all Ollama calls will fail in a HTTP 400 error with this message :
[400] Bad Request - {"error":"json: cannot unmarshal array into Go struct field .tools.function.parameters.properties.type of type string"}
I also tried to add @field:NotNull
annotation, or set a default value val field1: Int? = null
without success.
Environment Spring AI version : 1.0.0-M5 Kotlin version : 1.9.25
Steps to reproduce
see bug description
Expected behavior
The json schema should look like below.:
{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"field1" : {
"type" : "integer",
"format" : "int32",
}
}
}
More generally, providing easy customization of json schema generation could be great... But there's quite lot of work.
Comment From: ThomasVitale
@PheelTi thanks for reporting this! Could you try to see if it's still a problem in the latest snapshot version? We have introduced new APIs for tool calling and improved several aspects, including the JSON Schema generation.
For example, when building a FunctionToolCallback
, you can provide a custom JSON schema:
ToolCallback toolCallback = FunctionToolCallback
.builder("myFunctionBean", new MyFunction())
.description("description of my function")
.inputType(MyFunction.Request.class)
.inputSchema(JsonSchemaGenerator.generateForType(MyFunction.Request.class))
.build();
Here's the new documentation about JSON Schema for tools: https://docs.spring.io/spring-ai/reference/api/tools.html#_json_schema