Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package com.openai.core
import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.http.HttpRequest
import com.openai.models.ChatModel
import com.openai.models.ResponsesModel
import java.util.Optional
import java.util.concurrent.CompletableFuture
import kotlin.reflect.full.declaredFunctions
Expand Down Expand Up @@ -49,8 +51,29 @@ internal fun Params.modelNameOrNull(): String? {
null
}

return when (modelName) {
is Optional<*> -> modelName.orElse(null)?.toString()
else -> modelName?.toString()
val unwrappedModelName =
when (modelName) {
is Optional<*> -> modelName.orElse(null)
else -> modelName
}

return when (unwrappedModelName) {
// The `toString()` of a union type is a debug representation, so resolve its variant.
is ResponsesModel -> unwrappedModelName.modelNameOrNull()
else -> unwrappedModelName?.toString()
}
}

private fun ResponsesModel.modelNameOrNull(): String? =
accept(
object : ResponsesModel.Visitor<String?> {
override fun visitString(string: String): String = string

override fun visitChat(chat: ChatModel): String = chat.toString()

override fun visitOnly(only: ResponsesModel.ResponsesOnlyModel): String =
only.toString()

override fun unknown(json: JsonValue?): String? = null
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.openai.core

import com.openai.core.http.Headers
import com.openai.core.http.QueryParams
import com.openai.models.ChatModel
import com.openai.models.ResponsesModel
import com.openai.models.chat.completions.ChatCompletionListParams
import com.openai.models.embeddings.EmbeddingCreateParams
import com.openai.models.embeddings.EmbeddingModel
import com.openai.models.responses.ResponseCreateParams
import com.openai.models.uploads.UploadCancelParams
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatNoException
Expand Down Expand Up @@ -106,4 +109,30 @@ internal class PrepareRequestTest {
assertThatNoException().isThrownBy { params.modelNameOrNull() }
assertThat(params.modelNameOrNull()).isNull()
}

@Test
fun modelUnionStringNotNull() {
val params = ResponseCreateParams.builder().model("my-model").input("Hello, world!").build()

assertThat(params.modelNameOrNull()).isEqualTo("my-model")
}

@Test
fun modelUnionChatNotNull() {
val params =
ResponseCreateParams.builder().model(ChatModel.GPT_4O).input("Hello, world!").build()

assertThat(params.modelNameOrNull()).isEqualTo("gpt-4o")
}

@Test
fun modelUnionOnlyNotNull() {
val params =
ResponseCreateParams.builder()
.model(ResponsesModel.ResponsesOnlyModel.O1_PRO)
.input("Hello, world!")
.build()

assertThat(params.modelNameOrNull()).isEqualTo("o1-pro")
}
}