AsyncBetaRealtimeConnection Class
An open realtime WebSocket connection to a voice agent.
Iterate over the connection to receive strongly-typed server events, and use the sub-namespaces to send strongly-typed client events:
async with client.beta.voice_agents.realtime.connect(agent_name="my-agent") as conn:
await conn.input_audio_buffer.append(audio=chunk)
await conn.input_audio_buffer.commit()
await conn.response.create()
async for event in conn:
if event.type == RealtimeServerEventType.RESPONSE_DONE:
break
Constructor
AsyncBetaRealtimeConnection(connection: ClientWebSocketResponse, session: ClientSession)
Parameters
| Name | Description |
|---|---|
|
connection
Required
|
|
|
session
Required
|
|
Methods
| close |
Close the connection and release the underlying HTTP session. |
| recv |
Receive and parse the next server event. Known event types are returned as their strongly-typed
|
| send |
Send a client event over the connection. |
close
recv
Receive and parse the next server event.
Known event types are returned as their strongly-typed
VoiceAgentServerEventXxx model. Event types not (yet) represented by a
generated model are returned as a plain dict for forward compatibility.
async recv() -> RealtimeServerEventError | RealtimeServerEventResponseContentPartAdded | RealtimeServerEventConversationItemAdded | RealtimeServerEventConversationItemCreated | RealtimeServerEventConversationItemDeleted | RealtimeServerEventConversationItemDone | RealtimeServerEventConversationItemInputAudioTranscriptionCompleted | RealtimeServerEventConversationItemInputAudioTranscriptionDelta | RealtimeServerEventConversationItemInputAudioTranscriptionFailed | RealtimeServerEventConversationItemInputAudioTranscriptionSegment | RealtimeServerEventConversationItemRetrieved | RealtimeServerEventConversationItemTruncated | RealtimeServerEventInputAudioBufferCleared | RealtimeServerEventInputAudioBufferCommitted | RealtimeServerEventInputAudioBufferSpeechStarted | RealtimeServerEventInputAudioBufferSpeechStopped | RealtimeServerEventInputAudioBufferTimeoutTriggered | RealtimeServerEventMCPListToolsCompleted | RealtimeServerEventMCPListToolsFailed | RealtimeServerEventMCPListToolsInProgress | RealtimeServerEventOutputAudioBufferCleared | RealtimeServerEventRateLimitsUpdated | VoiceAgentServerEventResponseAnimationBlendshapesDelta | VoiceAgentServerEventResponseAnimationBlendshapesDone | VoiceAgentServerEventResponseAnimationVisemeDelta | VoiceAgentServerEventResponseAnimationVisemeDone | RealtimeServerEventResponseAudioDelta | RealtimeServerEventResponseAudioDone | VoiceAgentServerEventResponseAudioTimestampDelta | VoiceAgentServerEventResponseAudioTimestampDone | RealtimeServerEventResponseAudioTranscriptDelta | RealtimeServerEventResponseAudioTranscriptDone | RealtimeServerEventResponseContentPartDone | RealtimeServerEventResponseCreated | RealtimeServerEventResponseDone | RealtimeServerEventResponseFunctionCallArgumentsDelta | RealtimeServerEventResponseFunctionCallArgumentsDone | RealtimeServerEventResponseMCPCallArgumentsDelta | RealtimeServerEventResponseMCPCallArgumentsDone | RealtimeServerEventResponseMCPCallCompleted | RealtimeServerEventResponseMCPCallFailed | RealtimeServerEventResponseMCPCallInProgress | RealtimeServerEventResponseOutputItemAdded | RealtimeServerEventResponseOutputItemDone | RealtimeServerEventResponseTextDelta | RealtimeServerEventResponseTextDone | VoiceAgentServerEventResponseVideoDelta | VoiceAgentServerEventRtcCallError | VoiceAgentServerEventRtcCallSdpCreated | VoiceAgentServerEventSessionAvatarConnecting | VoiceAgentServerEventSessionAvatarSwitchToIdle | VoiceAgentServerEventSessionAvatarSwitchToSpeaking | RealtimeServerEventSessionCreated | VoiceAgentServerEventSessionSubagentAborted | VoiceAgentServerEventSessionSubagentCompleted | VoiceAgentServerEventSessionSubagentStarted | RealtimeServerEventSessionUpdated | VoiceAgentServerEventWarning | Mapping[str, Any]
Returns
| Type | Description |
|---|---|
|
<xref:azure.ai.projects.aio.ServerEvent>
|
The parsed server event. |
Exceptions
| Type | Description |
|---|---|
|
If the connection was closed by the server, gracefully or
otherwise, or if the transport reported an error. Iterating over the connection
( |
send
Send a client event over the connection.
async send(event: RealtimeClientEventConversationItemCreate | RealtimeClientEventConversationItemDelete | RealtimeClientEventConversationItemRetrieve | RealtimeClientEventConversationItemTruncate | RealtimeClientEventInputAudioBufferAppend | RealtimeClientEventInputAudioBufferClear | RealtimeClientEventInputAudioBufferCommit | RealtimeClientEventOutputAudioBufferClear | RealtimeClientEventResponseCancel | RealtimeClientEventResponseCreate | VoiceAgentClientEventRtcCallSdpCreate | VoiceAgentClientEventSessionAvatarConnect | VoiceAgentClientEventSessionUpdate | str | Mapping[str, Any]) -> None
Parameters
| Name | Description |
|---|---|
|
event
Required
|
<xref:azure.ai.projects.aio.ClientEvent> or
str
A strongly-typed client event, a ready-made mapping, or a raw JSON string. |
Exceptions
| Type | Description |
|---|---|
|
If |