Table of Contents

Class AsyncSession

Namespace
Google.GenAI
Assembly
Google.GenAI.dll

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

webSocket WebSocket
apiClient ApiClient

Methods

CloseAsync()

Closes the WebSocket connection gracefully. This method is thread-safe and idempotent.

public Task CloseAsync()

Returns

Task

DisposeAsync()

Asynchronously disposes the session by closing the WebSocket connection.

public ValueTask DisposeAsync()

Returns

ValueTask

ReceiveAsync(CancellationToken)

Receives model responses from the server.

public Task<LiveServerMessage?> ReceiveAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<LiveServerMessage>

A LiveServerMessage containing the model's response, or null if 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:

  1. Prefilling a conversation context (including sending anything that can't be represented as a realtime message) before starting a realtime conversation.
  2. Conducting a non-realtime conversation with the live API.
Caution: Interleaving 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

clientContent LiveSendClientContentParameters

The client content to send to the model.

cancellationToken CancellationToken

The cancellation token to use for the send operation.

Returns

Task

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

realtimeInput LiveSendRealtimeInputParameters

The realtime input to send to the model.

cancellationToken CancellationToken

The cancellation token to use for the send operation.

Returns

Task

SendToolResponseAsync(LiveSendToolResponseParameters, CancellationToken)

public Task SendToolResponseAsync(LiveSendToolResponseParameters toolResponse, CancellationToken cancellationToken = default)

Parameters

toolResponse LiveSendToolResponseParameters
cancellationToken CancellationToken

Returns

Task