Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Openai Openai python Realtime Event Processing

From Leeroopedia
Knowledge Sources
Domains Realtime_Communication, Event_Processing
Last Updated 2026-02-15 00:00 GMT

Overview

An asynchronous event loop pattern that consumes and dispatches typed server events from a persistent Realtime API WebSocket connection.

Description

The Realtime API server sends a stream of typed events over the WebSocket connection, including text deltas, audio deltas, session updates, response lifecycle events, and error notifications. The client iterates over these events asynchronously, dispatching each based on its discriminated type field. Over 30 event types exist, covering the full lifecycle of sessions, conversations, and responses.

Usage

Use this principle to process all server-side events in a Realtime session. Implement an async event loop with async for event in connection and dispatch based on event.type.

Theoretical Basis

The pattern follows an Event Loop with Discriminated Dispatch:

# Async event consumption loop
async for event in connection:
    match event.type:
        case "session.updated":
            handle_session_update(event)
        case "response.text.delta":
            accumulate_text(event.delta)
        case "response.audio.delta":
            play_audio(base64_decode(event.delta))
        case "response.function_call_arguments.done":
            result = call_function(event)
            send_function_output(result)
        case "response.done":
            finalize_response(event.response)
        case "error":
            handle_error(event.error)

The discriminated union type RealtimeServerEvent ensures type safety when accessing event-specific fields.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment