spring-projects / spring-projects/spring-ai

Ollama tools is not compatible with a json shema containing an array in a "type" field (kotlin)

Open
#2,047 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 10h
Merged PRs (30d)
5

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the Kotlin FunctionCallback.builder() and inputType() example, then trace how its nullable request type becomes an Ollama tool schema. Compare the generated schema with the Ollama HTTP error and expected schema; done means nullable Kotlin callback inputs no longer produce an unsupported array in the tool parameter type.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, ollama, spring
Domain
ai, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.