ChatResponse

@Serializable
data class ChatResponse(val choices: List<ChatChoice>, val usage: TokensUsage? = null)

A non-streaming response from an LLM provider.

Contains one or more ChatChoice objects representing different completion options. In most cases there is a single choice, but some providers support returning multiple candidates.

See also

Samples

// ChatResponse is returned after a successful API request made via the chat() function.
runBlocking {
    // We receive a ChatResponse (this function was created for illustrative purposes only and does not exist in the actual API!)
    val response: ChatResponse = sendToAPI()

    // Process all chat choices from the response.
    for (choice in response.choices) {
        processChatChoice(choice)
    }
}
// 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(choices: List<ChatChoice>, usage: TokensUsage? = null)

Properties

Link copied to clipboard

The list of completion choices returned by the model.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard