I'm afk but I got an error trying to deserialize a Kotlin data class using the ChatClient's entity(Class<T>) method

Comment From: mxsl-gr

try register jackson kotlin module, at least it works for me. essentially kotlin's data class compiles into a class with a full-parameter constructor. register relevant module should resolve the issue

add dependency:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
</dependency>

ObjectMapper register module:

OBJECT_MAPPER.registerModule(new KotlinModule.Builder().build())

Comment From: markpollack

Recent work in this area for kotlin support. Need to retry on latest code base.

Comment From: markpollack

@joshlong there is now dedicted kotlin support can you try again. clikc on the kotlin tab on https://docs.spring.io/spring-ai/reference/api/functions.html for some additional information

Comment From: joshlong

On it

Comment From: joshlong

package com.example.demo

import org.springframework.ai.chat.client.ChatClient
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding
import org.springframework.boot.ApplicationRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.support.beans

@SpringBootApplication
@RegisterReflectionForBinding(Joke::class)
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args) {
        val ctx = beans {
            bean {
                val cb = ref<ChatClient.Builder>()
                return@bean cb.build()
            }
            bean {

                ApplicationRunner {
                    val cc = ref<ChatClient>()
                    val joke = cc
                        .prompt("tell me a joke")
                        .call()
                        .entity(Joke::class.java)
                    println(joke)
                }
            }
        }
        addInitializers(ctx)
    }
}


data class Joke(val setup: String, val punchline: String)

This basic thing works with boot 3.4 snapshot and spring ai 1.0.0 snapshots! :) Nice! I can't wait to try some of the other things Sebastien worked on

Comment From: joshlong

One thing that doesn't appear to work is using a reified generic variable , eg

val joke = cc
  .prompt("tell me a joke")
  .call()
  .entity<Joke>()

Looks like we need n extension function for this. :) I'll craft a PR?

Comment From: joshlong

1729 i just sent this PR for your kind consideration. it enables the more idiomatic behavior shown above.