LiveSession

@PublicPreviewAPI
public final class LiveSession


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

Summary

Public methods

final void

Closes the client session.

final @NonNull Flow<@NonNull LiveServerMessage>

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

final void
send(@NonNull Content content)

Sends data to the model.

final void

Sends text to the model.

final void

Sends function calling responses to the model.

final void

Streams client data to the model.

final void
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
startAudioConversation(
    Function1<@NonNull FunctionCallPart, @NonNull FunctionResponsePart> functionCallHandler
)

Starts an audio conversation with the model, which can only be stopped using stopAudioConversation or close.

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 a LiveSession is closed, it can not be reopened; you'll need to start a new LiveSession.

See also
stopReceiving

receive

public final @NonNull Flow<@NonNull LiveServerMessagereceive()

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

Call close to stop receiving responses from the model.

Returns
@NonNull Flow<@NonNull LiveServerMessage>

A Flow which will emit LiveServerMessage from the model.

Throws
com.google.firebase.vertexai.type.SessionAlreadyReceivingException com.google.firebase.vertexai.type.SessionAlreadyReceivingException

when the session is already receiving.

See also
stopReceiving

send

public final void send(@NonNull Content content)

Sends data to the model.

Calling this after startAudioConversation will play the response audio immediately.

Parameters
@NonNull Content content

Client Content to be sent to the model.

send

public final void send(@NonNull String text)

Sends text to the model.

Calling this after startAudioConversation will play the response audio immediately.

Parameters
@NonNull String text

Text to be sent to the model.

sendFunctionResponse

public final void sendFunctionResponse(
    @NonNull List<@NonNull FunctionResponsePart> functionList
)

Sends function calling responses to the model.

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

Parameters
@NonNull List<@NonNull FunctionResponsePart> functionList

The list of FunctionResponsePart instances indicating the function response from the client.

sendMediaStream

public final void sendMediaStream(@NonNull List<@NonNull MediaData> mediaChunks)

Streams client data to the model.

Calling this after startAudioConversation will play the response audio immediately.

Parameters
@NonNull List<@NonNull MediaData> mediaChunks

The list of MediaData instances representing the media data to be sent.

startAudioConversation

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

Starts an audio conversation with the model, which can only be stopped using stopAudioConversation or close.

Parameters
Function1<@NonNull FunctionCallPart, @NonNull FunctionResponsePart> functionCallHandler

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

stopAudioConversation

public final void stopAudioConversation()

Stops the audio conversation with the model.

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

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 using receive, or indirectly by using startAudioConversation.

See also
close