ChatChunk
A streaming response chunk from an LLM provider.
Each chunk contains one or more ChatChunkChoice objects with incremental content. Collect chunks from ChatClient.chatStream to build up the full response.
Samples
// Let's go through all the Choices.
for (chunkChoice in chunk.choices) {
// We formulate the printMessage based on the content.
val printMessage: String = when (val content = chunkChoice.content) {
is Content.Reasoning -> "Reasoning chunk: $content"
is Content.Text -> "Output chunk: $content"
is Content.ToolCallDelta -> "Tool call delta chunk: $content"
}
// Print information about the chunk.
println("Chunk index: ${chunkChoice.index}" +
"\n$printMessage" +
"\n${if (!chunkChoice.finishReason.isNullOrBlank()) chunkChoice.finishReason else ""}")
}Content copied to clipboard
// Collecting all chunks using Collect Handler.
chunks.collectHandler {
// onChunk is called for each chunk.
onChunk { chunk ->
chunk.forEachChoice { choiceIndex, choice ->
val output = when (val content = choice.content) {
is Content.Reasoning -> "reasoning: ${content.value}"
is Content.Text -> "output: ${content.text}"
is Content.ToolCallDelta -> "tool call: ${content.name}"
}
println("Choice: $choiceIndex: $output")
}
}
// onError is called if an error occurred during collect.
onError { throwable ->
println("Error on collecting chunk: ${throwable.message}")
}
// onComplete is called when collect is complete.
onCompleted {
println("Collect ended!")
}
}Content copied to clipboard
Properties
Functions
Link copied to clipboard
inline suspend fun ChatChunk.forEachChoice(crossinline onChoice: suspend (choiceIndex: Int, choice: ChatChunkChoice) -> Unit)
Iterates over each choice in a ChatChunk.