ChatResponse
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)
}
}Content copied to clipboard
// 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()Content copied to clipboard