stream

open fun stream(request: ChatRequest): Flow<ChatEvent>

Returns the model's next message for request as the model writes it: a ChatEvent.TextDelta for each piece of text, and then one ChatEvent.Completed with the whole answer. The flow is cold: the model is asked when the flow is collected.

model.stream(request).collect { event ->
when (event) {
is ChatEvent.TextDelta -> print(event.text)
is ChatEvent.Completed -> conversation += event.response.message
}
}

A model that cannot stream keeps this default, which asks with chat and emits the text of the answer as one piece, so code that shows the pieces works with every model. The flow fails with a ChatModelException when the call fails or the model declines to answer; pieces that arrived before the failure are not an answer, so drop them.