RealtimeConnection Class

An open realtime WebSocket connection to a voice agent.

Iterate to receive events. Use conversation, response, and the audio buffers to send messages and control replies:


   with client.voice_agents.realtime.connect(agent_name="my-agent") as conn:
       for event in conn:
           if event.type == RealtimeServerEventType.RESPONSE_DONE:
               break

Constructor

RealtimeConnection(connection: ClientConnection)

Parameters

Name Description
connection
Required

Methods

close

Close the socket once.

recv

Receive and parse the next server event.

Known events use SDK model classes. Unknown event types are returned as dictionaries.

send

Send a client event over the connection.

close

Close the socket once.

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

Keyword-Only Parameters

Name Description
code
int

WebSocket close code.

Default value: 1000
reason
str

WebSocket close reason.

recv

Receive and parse the next server event.

Known events use SDK model classes. Unknown event types are returned as dictionaries.

recv(*, timeout: float | None = None) -> RealtimeServerEventError | RealtimeServerEventResponseContentPartAdded | RealtimeServerEventConversationItemAdded | RealtimeServerEventConversationItemCreated | RealtimeServerEventConversationItemDeleted | RealtimeServerEventConversationItemDone | RealtimeServerEventConversationItemInputAudioTranscriptionCompleted | RealtimeServerEventConversationItemInputAudioTranscriptionDelta | RealtimeServerEventConversationItemInputAudioTranscriptionFailed | RealtimeServerEventConversationItemInputAudioTranscriptionSegment | RealtimeServerEventConversationItemRetrieved | RealtimeServerEventConversationItemTruncated | RealtimeServerEventInputAudioBufferCleared | RealtimeServerEventInputAudioBufferCommitted | RealtimeServerEventInputAudioBufferSpeechStarted | RealtimeServerEventInputAudioBufferSpeechStopped | RealtimeServerEventInputAudioBufferTimeoutTriggered | VoiceAgentServerEventInputCollectionStatus | 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 | VoiceAgentServerEventSystemToolArgumentsDelta | VoiceAgentServerEventSystemToolArgumentsDone | VoiceAgentServerEventResponseVideoDelta | VoiceAgentServerEventRtcCallError | VoiceAgentServerEventRtcCallSdpCreated | VoiceAgentServerEventSessionAvatarConnecting | VoiceAgentServerEventSessionAvatarSwitchToIdle | VoiceAgentServerEventSessionAvatarSwitchToSpeaking | RealtimeServerEventSessionCreated | VoiceAgentServerEventSessionHandoffAborted | VoiceAgentServerEventSessionHandoffCompleted | VoiceAgentServerEventSessionHandoffStarted | VoiceAgentServerEventSessionSubagentAborted | VoiceAgentServerEventSessionSubagentCompleted | VoiceAgentServerEventSessionSubagentStarted | RealtimeServerEventSessionUpdated | VoiceAgentServerEventWarning | Mapping[str, Any]

Keyword-Only Parameters

Name Description
timeout
float or None

Maximum time in seconds to wait for the next event. If None (the default), block until an event is received. If no event arrives within timeout seconds, raise TimeoutError.

Default value: None

Returns

Type Description
<xref:azure.ai.projects.ServerEvent>

The parsed server event.

Exceptions

Type Description

If the connection was closed by the server, gracefully or otherwise. Iterating over the connection ( for event in conn:

If timeout elapses before an event is received.

send

Send a client event over the connection.

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.ClientEvent> or str

A client event model, mapping, or JSON string.

Exceptions

Type Description

If event is a str that is not valid JSON.

Attributes

closed

Whether the underlying WebSocket connection has been closed.

Returns

Type Description