Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Connect

Connect to Live without query parameters. Send session.start with model and session configuration, then wait for session.started. Follow the Live prompting guide before writing instructions.

WebSocket wss://api.openai.com/v1/live/sessions

Connect

import asyncio
from openai import AsyncOpenAI


async def observe() -> None:
    # New primary session; model belongs in session.start, not the URL.
    async with AsyncOpenAI() as client:
        async with client.live.connect() as connection:
            await connection.session.start(session={"model": "gpt-live-1"})
            close_requested = False
            async for event in connection:
                if event.type == "error":
                    raise RuntimeError("Live reported a session error.")
                if event.type == "session.started":
                    # Wait for startup before closing the session this example owns.
                    print("Session ready:", event.session.id, event.session.model)
                    await connection.session.close()
                    close_requested = True
                elif event.type == "session.closed":
                    if not close_requested or event.reason != "close_requested":
                        raise RuntimeError(
                            f"Session ended unexpectedly: {event.reason}; usage={event.usage}"
                        )
                    print("Final usage:", event.usage)
                    return
            raise RuntimeError(
                "Connection closed before session.closed; final usage unconfirmed"
            )


async def main() -> None:
    # Stop waiting if the peer never sends the expected events.
    await asyncio.wait_for(observe(), timeout=60)


asyncio.run(main())