sendMessageStream

fun sendMessageStream(text: String, config: GenerateContentConfig? = null, automaticFunctionCalling: AutomaticFunctionCalling? = null): Flow<GenerateContentResponse>

Sends text as the next turn and returns the model's response in chunks.

The turns so far are sent along with text, so the model has the conversation for context. The message is sent when the returned flow is collected, not when this method is called, and the flow carries one turn and is collected once. The overload taking a list documents the rest.

Parameters

text

The text of the message to send.

config

Replaces the session config for this turn rather than merging with it.

automaticFunctionCalling

Replaces the session setting for this turn rather than merging with it.

Throws

if the returned flow is collected again after a completed turn.


fun sendMessageStream(content: Content, config: GenerateContentConfig? = null, automaticFunctionCalling: AutomaticFunctionCalling? = null): Flow<GenerateContentResponse>

Sends content as the next turn and returns the model's response in chunks.

The turns so far are sent along with content, so the model has the conversation for context. The message is sent when the returned flow is collected, not when this method is called, and the flow carries one turn and is collected once. The overload taking a list documents the rest.

Parameters

content

The message to send.

config

Replaces the session config for this turn rather than merging with it.

automaticFunctionCalling

Replaces the session setting for this turn rather than merging with it.

Throws

if the returned flow is collected again after a completed turn.


fun sendMessageStream(contents: List<Content>, config: GenerateContentConfig? = null, automaticFunctionCalling: AutomaticFunctionCalling? = null): Flow<GenerateContentResponse>

Sends contents as the next turn and returns the model's response in chunks.

The turns so far are sent along with contents, so the model has the conversation for context. The turn joins the history once the flow completes, and every chunk is kept, so a ten chunk response adds ten model turns to the history rather than one. A stream that is cut off before the model finishes is kept but is not sent again in later requests.

The message is sent when the returned flow is collected, not when this method is called, so collect one turn before starting the next. Starting a turn while an earlier flow is still uncollected sends the two out of order:

val flow = chat.sendMessageStream("first") // nothing sent yet
chat.sendMessage("second") // sent, and recorded, first
flow.collect {} // "first" is sent now, and the model sees it after "second"

Each flow carries one turn and is collected once. Collecting it again after the turn has completed throws, rather than quietly sending the message a second time; collect into a list if you need to read the response more than once. A collection that failed or was abandoned did not complete a turn, so it can be retried.

With automatic function calling on, every chunk the model sends is emitted, including the ones carrying function calls. The responses to those calls are recorded in the history but never emitted, since they come from this side rather than the model.

Parameters

contents

The message to send. Several contents are recorded as one turn.

config

Replaces the session config for this turn rather than merging with it.

automaticFunctionCalling

Replaces the session setting for this turn. Null uses the session's.

Throws

if the returned flow is collected again after a completed turn.