TwilioAudioAdapter

TwilioAudioAdapter(
    websocket: WebSocket,
    *,
    logger: Logger | None = None
)
Adapter for streaming audio from Twilio to OpenAI Realtime API and vice versa.
Adapter for streaming audio from Twilio to OpenAI Realtime API and vice versa.
Parameters:
NameDescription
websocketType: WebSocket
loggerType: logging.Logger | None

Default: None

Instance Methods

handle_speech_started_event

handle_speech_started_event(self) -> None
Handle interruption when the caller’s speech starts.

initialize_session

initialize_session(self) -> None
Control initial session with OpenAI.

on_close

on_close(self) -> None
Handle close of RealtimeClient.

on_event

on_event(self, event: RealtimeEvent) -> None
Receive events from the OpenAI Realtime API, send audio back to Twilio. Parameters:
NameDescription
eventType: RealtimeEvent

run

run(self, agent: RealtimeAgent) -> 
Run the observer with the agent.
When implementing, be sure to call self._ready_event.set() when the observer is ready to process events.
Parameters:
NameDescription
agentThe realtime agent attached to the observer.

Type: RealtimeAgent

run_loop

run_loop(self) -> None
Run the adapter loop.

send_mark

send_mark(self) -> None
Send a mark of audio interruption to the Twilio websocket.

wait_for_ready

wait_for_ready(self) -> None
Get the event that is set when the observer is ready.