Class AsyncSession
Represents a websocket connection to the Google's GenAI Live API. This class is not meant to be instantiated directly. Instead, use ConnectAsync(string, LiveConnectConfig, CancellationToken) to create an instance.
public class AsyncSession : IAsyncDisposable
- Inheritance
-
AsyncSession
- Implements
- Inherited Members
Constructors
AsyncSession(WebSocket, ApiClient)
public AsyncSession(WebSocket webSocket, ApiClient apiClient)
Parameters
Methods
CloseAsync()
Closes the WebSocket connection gracefully. This method is thread-safe and idempotent.
public Task CloseAsync()
Returns
DisposeAsync()
Asynchronously disposes the session by closing the WebSocket connection.
public ValueTask DisposeAsync()
Returns
ReceiveAsync(CancellationToken)
Receives model responses from the server.
public Task<LiveServerMessage?> ReceiveAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
- Task<LiveServerMessage>
A LiveServerMessage containing the model's response, or
nullif the connection has been gracefully closed.
Exceptions
- InvalidOperationException
Thrown if an empty or invalid message is received.
- WebSocketException
Thrown for underlying WebSocket errors that are not a graceful close.
SendClientContentAsync(LiveSendClientContentParameters, CancellationToken)
Sends non-realtime, turn-based content to the model.
There are two ways to send messages to the live API:
SendClientContentAsync and SendRealtimeInputAsync.
SendClientContentAsync messages are added to the model context
in order. Because SendClientContentAsync guarantees the order
of messages between the client and the server, the model cannot respond as
quickly as with SendRealtimeInputAsync. This is most noticeable when
sending objects that require significant preprocessing time (typically images).
SendRealtimeInputAsync sends a list of Content objects,
which offers more options than the Blob objects sent by
SendClientContentAsync.
The main use cases for SendClientContentAsync over
SendRealtimeInputAsync are:
- Prefilling a conversation context (including sending anything that can't be represented as a realtime message) before starting a realtime conversation.
- Conducting a non-realtime conversation with the live API.
SendClientContentAsync and
SendRealtimeInputAsync in the same conversation is not recommended and
can lead to unexpected behavior.
public Task SendClientContentAsync(LiveSendClientContentParameters clientContent, CancellationToken cancellationToken = default)
Parameters
clientContentLiveSendClientContentParametersThe client content to send to the model.
cancellationTokenCancellationTokenThe cancellation token to use for the send operation.
Returns
SendRealtimeInputAsync(LiveSendRealtimeInputParameters, CancellationToken)
Sends realtime input to the model. With SendRealtimeInputAsync,
Google's GenAI Live API will respond to audio automatically based on voice
activity detection (VAD). SendRealtimeInputAsync is optimized for
responsiveness at the expense of deterministic ordering of the conversation
messages. Response tokens are added to the context as they become available.
public Task SendRealtimeInputAsync(LiveSendRealtimeInputParameters realtimeInput, CancellationToken cancellationToken = default)
Parameters
realtimeInputLiveSendRealtimeInputParametersThe realtime input to send to the model.
cancellationTokenCancellationTokenThe cancellation token to use for the send operation.
Returns
SendToolResponseAsync(LiveSendToolResponseParameters, CancellationToken)
public Task SendToolResponseAsync(LiveSendToolResponseParameters toolResponse, CancellationToken cancellationToken = default)
Parameters
toolResponseLiveSendToolResponseParameterscancellationTokenCancellationToken