diff --git a/openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/FunctionMode.kt b/openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/FunctionMode.kt index f8c53e17..f25cee57 100644 --- a/openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/FunctionMode.kt +++ b/openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/FunctionMode.kt @@ -15,6 +15,7 @@ import kotlinx.serialization.json.JsonDecoder import kotlinx.serialization.json.JsonEncoder import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.jsonPrimitive import kotlin.jvm.JvmInline /** @@ -62,7 +63,7 @@ internal object FunctionModeSerializer : KSerializer { require(decoder is JsonDecoder) { "This decoder is not a JsonDecoder. Cannot deserialize `FunctionCall`" } return when (val json = decoder.decodeJsonElement()) { is JsonPrimitive -> Default(json.content) - is JsonObject -> Named.serializer().deserialize(decoder) + is JsonObject -> json["name"]?.jsonPrimitive?.content?.let(FunctionMode::Named) ?: error("Missing 'name'") else -> throw UnsupportedOperationException("Cannot deserialize FunctionMode. Unsupported JSON element.") } } diff --git a/openai-core/src/commonTest/kotlin/com.aallam.openai.api/chat/TestFunctionMode.kt b/openai-core/src/commonTest/kotlin/com.aallam.openai.api/chat/TestFunctionMode.kt new file mode 100644 index 00000000..ecb45239 --- /dev/null +++ b/openai-core/src/commonTest/kotlin/com.aallam.openai.api/chat/TestFunctionMode.kt @@ -0,0 +1,23 @@ +package com.aallam.openai.api.chat + +import com.aallam.openai.api.BetaOpenAI +import kotlinx.serialization.json.Json +import kotlin.test.Test +import kotlin.test.assertEquals + +@OptIn(BetaOpenAI::class) +class TestFunctionMode { + + @Test + fun serialize() { + listOf( + FunctionMode.Auto, + FunctionMode.None, + FunctionMode.Named("someFunctionName") + ).forEach { functionMode -> + val jsonString = Json.encodeToString(FunctionMode.serializer(), functionMode) + val decoded = Json.decodeFromString(FunctionMode.serializer(), jsonString) + assertEquals(functionMode, decoded) + } + } +}