You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
A inline value class SpecificTool, implementing a sealed interface ToolChoice, can be successfully serialized to a json string, but fails to be deserialized from a json string !
Test code
importkotlinx.serialization.SerialNameimportkotlinx.serialization.Serializableimportkotlinx.serialization.json.Json
@Serializable
data classRequest(
/** * Controls tool usage: `none`, `auto`, `required`, or specific tool.*/
@SerialName("tool_choice") valtoolChoice:ToolChoice? = null,
)
@Serializable
sealedinterfaceToolChoice {
@Serializable
@JvmInline
value classStringTool(valvalue:String) : ToolChoice
@Serializable
data classSpecificTool(
/** * Type of tool choice: `function`.*/
@SerialName("type") valtype:String = "function",
/** * Function to call.*/
@SerialName("function") valfunction:FunctionChoice
) : ToolChoice {
@Serializable
data classFunctionChoice(
/** * Name of the function to call.*/
@SerialName("name") valname:String
)
}
}
funmain() {
val jsonDefault =Json {
classDiscriminator ="json_class_type"
ignoreUnknownKeys =true
encodeDefaults =true
}
val stringTool =ToolChoice.StringTool("auto")
val request1 =Request(stringTool)
val request1_json = jsonDefault.encodeToString(request1)
println(request1_json)
val specificTool =ToolChoice.SpecificTool("function", ToolChoice.SpecificTool.FunctionChoice("testFunction"))
val request2 =Request(specificTool)
val request2_json = jsonDefault.encodeToString(request2)
println(request2_json)
val request2New = jsonDefault.decodeFromString<Request>(request2_json)
println(request2New)
val request1New = jsonDefault.decodeFromString<Request>(request1_json)
println(request1New)
}
Test output
{"tool_choice":"auto"}
{"tool_choice":{"json_class_type":"ToolChoice.SpecificTool","type":"function","function":{"name":"testFunction"}}}
Request(toolChoice=SpecificTool(type=function, function=FunctionChoice(name=testFunction)))
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Expected JsonObject, but had JsonLiteral as the serialized body of ToolChoice at element: $.tool_choice
JSON input: "auto"
at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:400)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
at kotlinx.serialization.encoding.AbstractDecoder.decodeNullableSerializableElement(AbstractDecoder.kt:78)
at Request$$serializer.deserialize(main.kt:5)
at Request$$serializer.deserialize(main.kt:5)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:69)
at kotlinx.serialization.json.Json.decodeFromString(Json.kt:149)
at MainKt.main(main.kt:67)
at MainKt.main(main.kt)
Describe the bug
A inline value class SpecificTool, implementing a sealed interface ToolChoice, can be successfully serialized to a json string, but fails to be deserialized from a json string !
Test code
Test output
Expected behavior
Environment
The text was updated successfully, but these errors were encountered: