Table of Contents

Class GoogleGenAIRealtimeSession

Namespace
Microsoft.Extensions.AI
Assembly
Google.GenAI.dll

Provides an Microsoft.Extensions.AI.IRealtimeClientSession implementation for Google GenAI's Live API, wrapping an AsyncSession WebSocket connection.

public sealed class GoogleGenAIRealtimeSession : IRealtimeClientSession, IAsyncDisposable
Inheritance
GoogleGenAIRealtimeSession
Implements
IRealtimeClientSession
Inherited Members

Constructors

GoogleGenAIRealtimeSession(AsyncSession, string, RealtimeSessionOptions?)

Initializes a new instance wrapping a connected AsyncSession.

public GoogleGenAIRealtimeSession(AsyncSession asyncSession, string model, RealtimeSessionOptions? initialOptions)

Parameters

asyncSession AsyncSession

The connected AsyncSession for WebSocket communication.

model string

The model name for metadata.

initialOptions RealtimeSessionOptions

Optional initial session options.

Properties

Options

Gets the current session options.

public RealtimeSessionOptions? Options { get; }

Property Value

RealtimeSessionOptions

Methods

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

GetService(Type, object?)

Asks the Microsoft.Extensions.AI.IRealtimeClientSession for an object of the specified type serviceType.

public object? GetService(Type serviceType, object? serviceKey = null)

Parameters

serviceType Type

The type of object being requested.

serviceKey object

An optional key that can be used to help identify the target service.

Returns

object

The found object, otherwise null.

Remarks

The purpose of this method is to allow for the retrieval of strongly typed services that might be provided by the Microsoft.Extensions.AI.IRealtimeClientSession, including itself or any services it might be wrapping.

Exceptions

ArgumentNullException

serviceType is null.

GetStreamingResponseAsync(CancellationToken)

Streams the response from the real-time session.

public IAsyncEnumerable<RealtimeServerMessage> GetStreamingResponseAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token to cancel the operation.

Returns

IAsyncEnumerable<RealtimeServerMessage>

The response messages generated by the session.

Remarks

This method cannot be called multiple times concurrently on the same session instance.

SendAsync(RealtimeClientMessage, CancellationToken)

Sends a client message to the session.

public Task SendAsync(RealtimeClientMessage message, CancellationToken cancellationToken = default)

Parameters

message RealtimeClientMessage

The client message to send.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task

A task that represents the asynchronous send operation.

Remarks

This method allows for sending client messages to the session at any time, which can be used to influence the session's behavior or state.

Concurrency note for provider implementers: SendAsync(RealtimeClientMessage, CancellationToken) may be called concurrently from multiple sources. For example, a caller may stream audio via SendAsync(RealtimeClientMessage, CancellationToken) on one thread while middleware such as FunctionInvokingRealtimeClientSession calls SendAsync(RealtimeClientMessage, CancellationToken) to return tool results from within GetStreamingResponseAsync(CancellationToken) enumeration on another thread. If the underlying transport (e.g., a WebSocket) does not support concurrent sends, provider implementations must serialize access — for example by using a SemaphoreSlim — to prevent protocol violations.