ChatChoice

@Serializable
data class ChatChoice(val index: Int, val contents: List<Content.Response>, val finishReason: String? = null)

A single completion choice within a io.kory.core.chat.response.ChatResponse.

Each choice contains an ordered list of content items (text, reasoning, tool calls, etc.) and an optional finish reason indicating why the model stopped generating.

See also

Samples

// ChatChoice has a Content.Response array because the API sends the response in JSON,
// including both the tool call and the response at the same time.

val builder = StringBuilder()

// We review all the content and formulate the final message.

if (choice.contents.isNotEmpty()) {
    for (content in choice.contents) {
        val printMessage = when (content) {
            is Content.Parts -> {
                val parts = content.parts
                if (parts.isNotEmpty()) {
                    for (part in parts) {
                        when (part) {
                            is ContentPart.Image -> "Image part: ${part.source}"
                            is ContentPart.Text -> "Text part: ${part.value}"
                        }
                    }
                } else ""
            }
            is Content.Reasoning -> "Reasoning: ${content.value}"
            is Content.Text -> "Output: ${content.text}"
            is Content.ToolCall -> "Tool Call: ${content.name}"
        }

        builder.append("$printMessage\n")
    }

    if (builder.isNotBlank()) {
        println("Choice index: ${choice.index}\nContents:\n${builder.toString().trim()}")
    }
} else {
    val finishReason = choice.finishReason ?: "unknown"
    println("Choice ${choice.index} has no content. Finish reason: $finishReason")
}

builder.clear()

Constructors

Link copied to clipboard
constructor(index: Int, contents: List<Content.Response>, finishReason: String? = null)

Properties

Link copied to clipboard

The content items produced by the model for this choice.

Link copied to clipboard

Why the model stopped: "stop", "length", "tool_calls", etc. null if not provided.

Link copied to clipboard

Returns the first content item in this choice, or null if the choice is empty.

Link copied to clipboard
val index: Int

The index of this choice (0-based).