LiveSession

@PublicPreviewAPI
public final classLiveSession


Represents a live WebSocket session capable of streaming content to and from the server.

Summary

Public methods

final void

Closes the client session.

final boolean

Indicates whether an audio conversation is being used for this session object.

final boolean

Indicates whether the underlying websocket connection is active.

final @NonNullFlow<@NonNullLiveServerMessage>

Receives responses from the model for both streaming and standard requests.

final void
send(@NonNullContent content)

Sendsdata to the model.

final void

Sends text to the model.

final void

Sends an audio input stream to the model, using the realtime API.

final void

Sends function calling responses to the model.

final void

This method is deprecated. Use sendAudioRealtime, sendVideoRealtime, or sendTextRealtime instead

final void

Sends a text input stream to the model, using the realtime API.

final void

Sends a video frame to the model, using the realtime API.

final void
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

final void
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
startAudioConversation(
    @NonNullLiveAudioConversationConfig liveAudioConversationConfig
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

final void
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler,
    boolean enableInterruptions
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

final void
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler,
    Function2<TranscriptionTranscriptionUnit> transcriptHandler,
    boolean enableInterruptions
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

final void

Stops the audio conversation with the model.

final void

Stops receiving from the model.

Public methods

close

public final void close()

Closes the client session.

Once aLiveSession is closed, it can not be reopened; you'll need to start a newLiveSession.

isAudioConversationActive

public final boolean isAudioConversationActive()

Indicates whether an audio conversation is being used for this session object.

isClosed

public final boolean isClosed()

Indicates whether the underlying websocket connection is active.

receive

public final @NonNullFlow<@NonNullLiveServerMessagereceive()

Receives responses from the model for both streaming and standard requests.

Callclose to stop receiving responses from the model.

Returns
@NonNullFlow<@NonNullLiveServerMessage>

AFlow which will emitLiveServerMessage from the model.

Throws
com.google.firebase.ai.type.SessionAlreadyReceivingException com.google.firebase.ai.type.SessionAlreadyReceivingException

when the session is already receiving.

send

public final void send(@NonNullContent content)

Sendsdata to the model.

Calling this afterstartAudioConversation will play the response audio immediately.

Parameters
@NonNullContent content

ClientContent to be sent to the model.

send

public final void send(@NonNullString text)

Sends text to the model.

Calling this afterstartAudioConversation will play the response audio immediately.

Parameters
@NonNullString text

Text to be sent to the model.

sendAudioRealtime

public final void sendAudioRealtime(@NonNullInlineData audio)

Sends an audio input stream to the model, using the realtime API.

To learn more about audio formats, and the required state they should be provided in, see the docs onSupported audio formats

Parameters
@NonNullInlineData audio

Raw audio data used to update the model on the client's conversation. For best results, send 16-bit PCM audio at 24kHz.

sendFunctionResponse

public final void sendFunctionResponse(
    @NonNullList<@NonNullFunctionResponsePart> functionList
)

Sends function calling responses to the model.

NOTE: If you're usingstartAudioConversation, the method will handle sending function responses to the model for you. You donot need to call this method in that case.

Parameters
@NonNullList<@NonNullFunctionResponsePart> functionList

The list ofFunctionResponsePart instances indicating the function response from the client.

sendMediaStream

public final void sendMediaStream(@NonNullList<@NonNullMediaData> mediaChunks)
This method is deprecated.
Use sendAudioRealtime, sendVideoRealtime, or sendTextRealtime instead

Streams client data to the model.

Calling this afterstartAudioConversation will play the response audio immediately.

Parameters
@NonNullList<@NonNullMediaData> mediaChunks

The list ofMediaData instances representing the media data to be sent.

sendTextRealtime

public final void sendTextRealtime(@NonNullString text)

Sends a text input stream to the model, using the realtime API.

Parameters
@NonNullString text

Text content to append to the current client's conversation.

sendVideoRealtime

public final void sendVideoRealtime(@NonNullInlineData video)

Sends a video frame to the model, using the realtime API.

Instead of raw video data, the model expects individual frames of the video, sent as images.

If your video has audio, send it separately throughsendAudioRealtime.

For better performance, frames can also be sent at a lower rate than the video; even as low as 1 frame per second.

Parameters
@NonNullInlineData video

Encoded image data extracted from a frame of the video, used to update the model on the client's conversation, with the corresponding IANA standard MIME type of the video frame data (for example,image/png,image/jpeg, etc.).

startAudioConversation

@RequiresPermission(value = "android.permission.RECORD_AUDIO")
public final void startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

Parameters
Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler

A callback function that is invoked whenever the model receives a function call. TheFunctionResponsePart that the callback function returns will be automatically sent to the model.

startAudioConversation

@RequiresPermission(value = "android.permission.RECORD_AUDIO")
public final void startAudioConversation(
    @NonNullLiveAudioConversationConfig liveAudioConversationConfig
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

Parameters
@NonNullLiveAudioConversationConfig liveAudioConversationConfig

ALiveAudioConversationConfig provided by the user to control the various aspects of the conversation.

startAudioConversation

@RequiresPermission(value = "android.permission.RECORD_AUDIO")
public final void startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler,
    boolean enableInterruptions
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

Parameters
Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler

A callback function that is invoked whenever the model receives a function call. TheFunctionResponsePart that the callback function returns will be automatically sent to the model.

boolean enableInterruptions

If enabled, allows the user to speak over or interrupt the model's ongoing reply.

WARNING: The user interruption feature relies on device-specific support, and may not be consistently available.

startAudioConversation

@RequiresPermission(value = "android.permission.RECORD_AUDIO")
public final void startAudioConversation(
    Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler,
    Function2<TranscriptionTranscriptionUnit> transcriptHandler,
    boolean enableInterruptions
)

Starts an audio conversation with the model, which can only be stopped usingstopAudioConversation orclose.

Parameters
Function1<@NonNullFunctionCallPart, @NonNullFunctionResponsePart> functionCallHandler

A callback function that is invoked whenever the model receives a function call. TheFunctionResponsePart that the callback function returns will be automatically sent to the model.

Function2<TranscriptionTranscriptionUnit> transcriptHandler

A callback function that is invoked whenever the model receives a transcript. The firstTranscription object is the input transcription, and the second is the output transcription.

boolean enableInterruptions

If enabled, allows the user to speak over or interrupt the model's ongoing reply.

WARNING: The user interruption feature relies on device-specific support, and may not be consistently available.

stopAudioConversation

public final void stopAudioConversation()

Stops the audio conversation with the model.

This only needs to be called after a previous call tostartAudioConversation.

If there is no audio conversation currently active, this function does nothing.

stopReceiving

public final void stopReceiving()

Stops receiving from the model.

If this function is called during an ongoing audio conversation, the model's response will not be received, and no audio will be played; the live session object will no longer receive data from the server.

To resume receiving data, you must either handle it directly usingreceive, or indirectly by usingstartAudioConversation.

See also
close

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.