Skip to main content

enterprise_ai.multimodal.realtime

Source: sdks/python/src/enterprise_ai/multimodal/realtime/__init__.py. Back to the Python SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Class RealtimeError​

An error event received on the realtime socket.

EaiError

Model RealtimeEvent​

One decoded server event.

Fields:

NameTypeRequiredDescription
typestryesType of the event, for example transcript.delta or response.done (unknown when absent).
datadict[str, Any]noDecoded fields of the server event, besides its type.

Attributes:

NameTypeRequiredDescription
audiobytesyesDecoded audio of a response.audio.delta event.
textstryesText of a transcript.delta event.

Class RealtimeConnection​

An open realtime WebSocket (async context manager).

Attributes:

NameTypeRequiredDescription
headersyesHTTP headers sent when the socket opens.
raise_on_erroryesWhether a server error event raises RealtimeError.
urlyesWebSocket URL, with ?token= once a token was obtained.
usagedict[str, Any]yesLast usage reported by a usage.update event, {} before.
wsAnyyesThe open socket, None before opening and after close.

Constructor RealtimeConnection​

RealtimeConnection(url: str, headers: dict[str, str], raise_on_error: bool = True, connector: Any = None, token_provider: Callable[[], Any] | None = None)

Prepare the connection; the socket opens on async with.

NameTypeDefaultRequiredDescription
urlstryesWebSocket URL of the session.
headersdict[str, str]yesHTTP headers sent when the socket opens (authentication, tenant).
raise_on_errorboolTruenoRaise RealtimeError on a server error event (default True).
connectorAnyNonenoCoroutine function opening the socket, default websockets.asyncio.client.connect.
token_providerCallable[[], Any] | NoneNonenoCalled when the connection opens to get a new ws_token, added to url as ?token=.

RealtimeConnection.append_audio​

async append_audio(pcm: bytes) -> None

input_audio.append with base64 encoded audio.

NameTypeDefaultRequiredDescription
pcmbytesyesRaw audio bytes to send.

Returns: None.

RealtimeConnection.cancel_response​

async cancel_response() -> None

response.cancel: stops the response in progress (response.cancelled).

This element takes no parameter.

Returns: None.

RealtimeConnection.close​

async close() -> None

Close the socket; nothing happens when it is not open.

This element takes no parameter.

Returns: None.

RealtimeConnection.commit_audio​

async commit_audio() -> None

input_audio.commit: ends the utterance (saves the recording when config.record is set).

This element takes no parameter.

Returns: None.

RealtimeConnection.create_response​

async create_response(text: str, voice: str | None = None) -> None

response.create {text, voice?}: the server speaks text (response.created then audio).

NameTypeDefaultRequiredDescription
textstryesText the server speaks.
voicestr | NoneNonenoVoice to use, optional; left out of the event when not given.

Returns: None.

RealtimeConnection.events​

async events() -> AsyncIterator[RealtimeEvent]

Yield server events until the socket closes; error raises when raise_on_error.

This element takes no parameter.

Returns: AsyncIterator[RealtimeEvent]. A RealtimeEvent: One decoded server event.

Raises: RealtimeError.

RealtimeConnection.send​

async send(event: dict[str, Any]) -> None

Send a client event.

NameTypeDefaultRequiredDescription
eventdict[str, Any]yesClient event, sent as JSON (see client_event).

Returns: None.

RealtimeConnection.speech_started​

async speech_started() -> None

input_audio.speech_started: barge-in, the server cancels the response being spoken.

This element takes no parameter.

Returns: None.

RealtimeConnection.update_session​

async update_session(**settings: Any) -> None

session.update (voice, language, turn detection, instructions...).

NameTypeDefaultRequiredDescription
**settingsAnynoSession settings sent as session, for example voice and language.

Returns: None.

Class RealtimeClient​

/v1/realtime.

Resource

RealtimeClient.connect​

connect(session_id: str | dict[str, Any], ws_token: str | None = None, raise_on_error: bool = True, connector: Any = None) -> RealtimeConnection

Open the realtime socket (use as async with); the server requires a single use ws_token.

NameTypeDefaultRequiredDescription
session_idstr | dict[str, Any]yesSession id, or the create_session or create_token answer (its ws_token is then used).
ws_tokenstr | NoneNonenoSingle use token; without one, a new token is requested when the connection opens.
raise_on_errorboolTruenoRaise RealtimeError on a server error event (default True).
connectorAnyNonenoCoroutine function opening the socket, default websockets.asyncio.client.connect; replace it in tests.

Returns: RealtimeConnection. A RealtimeConnection: An open realtime WebSocket (async context manager).

RealtimeClient.create_session​

create_session(pipeline: str = 'echo', room: str | None = None, ttl_seconds: int | None = None, config: dict[str, Any] | None = None) -> Any

Open a realtime session. Opens a session (201) and returns it with session_id, livekit (url, token, room, configured) and the WebSocket access (ws_url, ws_token, ws_token_expires_at). Needs tenant_admin, developer or agent. Checks: pipeline is echo or stt (400 invalid_request); config.vad and config.dlp_mode are valid (422 parameter_invalid); an agent caller acts for on_behalf_of, a user may only open a session for itself (403 identity_mismatch); LiveKit credentials are configured; the policy realtime.session.create is evaluated. The room defaults to a name derived from the tenant and the session. Audited as realtime.session.create.

Route: /v1/realtime/sessions (POST).

NameTypeDefaultRequiredDescription
pipelinestr'echo'noecho (default) or stt.
roomstr | NoneNonenoLiveKit room name (1 to 128 letters, digits, ., _, -). Default: derived from the tenant and the session.
ttl_secondsint | NoneNonenoLifetime of the LiveKit token, in seconds (60 to 86400, default 3600).
configdict[str, Any] | NoneNonenoSession configuration: dlp_mode (mask default, tokenize or block), vad (enabled boolean, threshold in (0, 1]), record and pipeline options.

Returns: Any. Decoded JSON answer of the operation "Open a realtime session".

Raises: the typed errors of the transport (Errors).

RealtimeClient.create_token​

create_token(session_id: str) -> Any

Get a new WebSocket token. New single use WebSocket token for an open session (reconnection), read under RLS: ws_url, ws_token, ws_token_expires_at. 409 session_ended; 404 not_found. Needs tenant_admin, developer or agent. Audited as realtime.session.token.

Route: /v1/realtime/sessions/{session_id}/token (POST).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "Get a new WebSocket token".

Raises: the typed errors of the transport (Errors).

RealtimeClient.end_session​

end_session(session_id: str) -> Any

End a realtime session. Ends the session and records its usage event (voice_live: audio seconds, input and output tokens, measured cost). Ending a session already ended returns it unchanged. Needs tenant_admin, developer or agent. Audited as realtime.session.end.

Route: /v1/realtime/sessions/{session_id}/end (POST).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "End a realtime session".

Raises: the typed errors of the transport (Errors).

RealtimeClient.get_session​

get_session(session_id: str) -> Any

Get a realtime session. Returns the session: room, identities, agent, pipeline, configuration, status, audio seconds, tokens and usage. 404 not_found.

Route: /v1/realtime/sessions/{session_id} (GET).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "Get a realtime session".

Raises: the typed errors of the transport (Errors).

RealtimeClient.list_consents​

list_consents(session_id: str) -> Any

List the consents of a session. Consents recorded for the session, in capture order, with their revocation when revoked. One page only.

Route: /v1/realtime/sessions/{session_id}/consents (GET).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "List the consents of a session".

Raises: the typed errors of the transport (Errors).

record_consent(session_id: str, purpose: str, retention_days: int, recording_allowed: bool | None = None, transcription_allowed: bool | None = None, method: str | None = None, participant_identity: str | None = None) -> Any

Record a participant consent. Consent of one participant (purpose, recording, transcription, retention, method), captured by the caller (201). The participant defaults to the participant of the session. The method must be verbal, click, written or api (422 parameter_invalid); the session must not be ended (409 session_ended); the policy realtime.consent.record is evaluated. Needs tenant_admin, developer or agent. Audited as realtime.consent.record.

Route: /v1/realtime/sessions/{session_id}/consents (POST).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.
purposestryesPurpose the participant agrees to (1 to 500 characters).
retention_daysintyesHow long transcripts and recordings are kept, in days (1 to 3650).
recording_allowedbool | NoneNonenoWhether the audio may be recorded. Default false.
transcription_allowedbool | NoneNonenoWhether the speech may be transcribed and stored. Default false.
methodstr | NoneNonenoHow the consent was captured: verbal, click, written or api (default).
participant_identitystr | NoneNonenoIdentity of the participant (1 to 128 letters, digits, ., _, :, @, -). Default: the participant of the session.

Returns: Any. Decoded JSON answer of the operation "Record a participant consent".

Raises: the typed errors of the transport (Errors).

revoke_consent(session_id: str, consent_id: str) -> Any

Revoke a consent. Revocation takes effect on the next audio event: transcription and recording are refused again. A consent already revoked or unknown answers 404 not_found. Needs tenant_admin, developer or agent. Audited as realtime.consent.revoke.

Route: /v1/realtime/sessions/{session_id}/consents/{consent_id}/revoke (POST).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.
consent_idstryesIdentifier (UUID) of the consent.

Returns: Any. Decoded JSON answer of the operation "Revoke a consent".

Raises: the typed errors of the transport (Errors).

RealtimeClient.transcripts​

transcripts(session_id: str) -> Any

List the transcripts of a session. Stored transcripts, already filtered by the DLP (the raw transcript is never stored), with turn, consent, participant, DLP findings and expiry. Needs tenant_admin, developer, auditor or security_officer.

Route: /v1/realtime/sessions/{session_id}/transcripts (GET).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "List the transcripts of a session".

Raises: the typed errors of the transport (Errors).

RealtimeClient.turns​

turns(session_id: str) -> Any

List the turns of a session. Turns with the participant, the user, the agent, the trace id, the status (a barge-in records cancelled) and the measured latency, in order. One page only.

Route: /v1/realtime/sessions/{session_id}/turns (GET).

NameTypeDefaultRequiredDescription
session_idstryesIdentifier (UUID) of the realtime session.

Returns: Any. Decoded JSON answer of the operation "List the turns of a session".

Raises: the typed errors of the transport (Errors).

RealtimeClient.ws_url​

ws_url(session_id: str | dict[str, Any], ws_token: str | None = None) -> str

URL of WebSocket /v1/realtime/ws/{session_id}?token=<ws_token>.

NameTypeDefaultRequiredDescription
session_idstr | dict[str, Any]yesSession id, or the create_session answer (its session_id and ws_token are read).
ws_tokenstr | NoneNonenoSingle use token added as ?token=, optional.

Returns: str. wss:// URL (ws:// when the base URL is not HTTPS), with the token when known.

Functions​

client_event​

client_event(event_type: str, **payload: Any) -> dict[str, Any]

Build a client event with an event_id.

NameTypeDefaultRequiredDescription
event_typestryesOne of session.update, input_audio.append, input_audio.commit, input_audio.speech_started, response.create, response.cancel; any other raises ValueError.
**payloadAnynoFields of the event, merged next to type and event_id.

Returns: dict[str, Any]. Event dict with type, a new event_id (evt_ prefix) and the payload.

Raises: ValueError.

parse_event​

parse_event(message: str | bytes) -> RealtimeEvent

Decode a server message.

NameTypeDefaultRequiredDescription
messagestr | bytesyesJSON message received on the socket, text or bytes.

Returns: RealtimeEvent. A RealtimeEvent: One decoded server event.