Freigeben über


VoiceLiveConnection Class

Represents an active asynchronous WebSocket connection to the Azure Voice Live API.

This class exposes resource-specific helpers for interacting with the service:

Instances are yielded by the connect context manager.

Initialize a VoiceLiveConnection instance.

Constructor

VoiceLiveConnection(client_session: ClientSession, ws: ClientWebSocketResponse)

Parameters

Name Description
client_session
Required
<xref:aiohttp.ClientSession>

The active aiohttp ClientSession used for HTTP and WebSocket operations.

ws
Required
<xref:aiohttp.ClientWebSocketResponse>

The established WebSocket connection to the Voice Live service.

Variables

Name Description
session

Resource for managing session updates.

response

Resource for creating and cancelling model responses.

input_audio_buffer

Resource for managing input audio buffer.

output_audio_buffer

Resource for clearing output audio.

conversation

Resource for managing the conversation and its items.

transcription_session

Resource for updating transcription session configuration.

Methods

close

Close the WebSocket and underlying HTTP session.

This will gracefully terminate the connection to the Voice Live service and release any network resources.

recv

Receive and parse the next message as a typed event.

recv_bytes

Receive raw bytes from the connection.

send

Send an event to the server over the active WebSocket connection (asynchronously).

Supported input types:

  • Mapping-like object (e.g., dict, MappingProxyType) — converted to

    a plain dict and then JSON-encoded. Any nested SDK models are serialized using the fallback serializer _json_default().

  • ClientEvent model instance — converted to a plain dictionary via

    as_dict() (preserving REST field names and discriminators), then JSON-encoded.

  • Other objects — directly passed to json.dumps() with _json_default()

    handling non-JSON-native values.

close

Close the WebSocket and underlying HTTP session.

This will gracefully terminate the connection to the Voice Live service and release any network resources.

async close(*, code: int = 1000, reason: str = '') -> None

Keyword-Only Parameters

Name Description
code
int

WebSocket close code to send to the server. Defaults to 1000 (Normal Closure).

Default value: 1000
reason
str

Optional reason string to include in the close frame.

Returns

Type Description

recv

Receive and parse the next message as a typed event.

async recv() -> ServerEvent

Returns

Type Description

The next typed server event.

recv_bytes

Receive raw bytes from the connection.

async recv_bytes() -> bytes

Returns

Type Description

The raw WebSocket message payload as bytes.

send

Send an event to the server over the active WebSocket connection (asynchronously).

Supported input types:

  • Mapping-like object (e.g., dict, MappingProxyType) — converted to

    a plain dict and then JSON-encoded. Any nested SDK models are serialized using the fallback serializer _json_default().

  • ClientEvent model instance — converted to a plain dictionary via

    as_dict() (preserving REST field names and discriminators), then JSON-encoded.

  • Other objects — directly passed to json.dumps() with _json_default()

    handling non-JSON-native values.

async send(event: Mapping[str, Any] | ClientEvent) -> None

Parameters

Name Description
event
Required

The event to send.

Exceptions

Type Description

If serialization fails or the WebSocket send raises an error.

Attributes

conversation

conversation: ConversationResource

input_audio_buffer

input_audio_buffer: InputAudioBufferResource

output_audio_buffer

output_audio_buffer: OutputAudioBufferResource

response

response: ResponseResource

session

session: SessionResource

transcription_session

transcription_session: TranscriptionSessionResource