findusl
findusl

Reputation: 2654

Why doesn't CIOEngine have a dispatcher in kotlin script?

I have the following code in a file testKtor.main.kts:

@file:DependsOn("com.aallam.openai:openai-client-jvm:3.7.0")
@file:DependsOn("io.ktor:ktor-client-cio-jvm:2.3.9")

import com.aallam.openai.client.OpenAI

val openAiClient = OpenAI(token = "your-api-token")

When running this with kotlin testKtor.main.kts I get this exception:

java.lang.AbstractMethodError: Receiver class io.ktor.client.engine.cio.CIOEngine does not define or inherit an implementation of the resolved method 'abstract kotlinx.coroutines.CoroutineDispatcher getDispatcher()' of interface io.ktor.client.engine.HttpClientEngine. at io.ktor.client.engine.cio.CIOEngine.(CIOEngine.kt:29) at io.ktor.client.engine.cio.CIO.create(CIOCommon.kt:35) at io.ktor.client.HttpClientKt.HttpClient(HttpClient.kt:42) at io.ktor.client.HttpClientJvmKt.HttpClient(HttpClientJvm.kt:21) at com.aallam.openai.client.internal.HttpClientKt.createHttpClient(HttpClient.kt:89) at com.aallam.openai.client.OpenAIKt.OpenAI(OpenAI.kt:60) at com.aallam.openai.client.OpenAIKt.OpenAI(OpenAI.kt:40) at com.aallam.openai.client.OpenAIKt.OpenAI$default(OpenAI.kt:30) at TestKtor_main.(TestKtor.main.kts:6)

I checked, the CIOEngine in ktor-client-cio-jvm depends on ktor-client-core-jvm where the dispatcher is implemented in the HttpClientEngineBase. It should exist.

Upvotes: 0

Views: 183

Answers (1)

findusl
findusl

Reputation: 2654

The solution is to reorder the dependencies. This works:

@file:DependsOn("io.ktor:ktor-client-cio-jvm:2.3.9")
@file:DependsOn("com.aallam.openai:openai-client-jvm:3.7.0")

import com.aallam.openai.client.OpenAI

val openAiClient = OpenAI(token = "your-api-token")

As to why, I can give an educated guess:

The openai-client-jvm library depends on the multiplatform version of ktor-client-core via the multiplatform commonMain sourceset (see build.gradle.kts). I assume the kotlin script cannot handle this multiplatform dependency correctly. However through the ktor-client-cio-jvm dependency we somehow get the correct version of ktor-client-core (maybe because its a project dependency, see built.gradle.kts).

Now depending on the order of our operations, the openai library can either find the ktor core correctly or it gets the non-jvm version and it fails.

Upvotes: 0

Related Questions