Skip to content

Model

RealtimeModelListener

Bases: ABC

A listener for realtime transport events.

Source code in src/agents/realtime/model.py
class RealtimeModelListener(abc.ABC):
    """A listener for realtime transport events."""

    @abc.abstractmethod
    async def on_event(self, event: RealtimeModelEvent) -> None:
        """Called when an event is emitted by the realtime transport."""
        pass

on_event abstractmethod async

on_event(event: RealtimeModelEvent) -> None

Called when an event is emitted by the realtime transport.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
async def on_event(self, event: RealtimeModelEvent) -> None:
    """Called when an event is emitted by the realtime transport."""
    pass

RealtimeModelConfig

Bases: TypedDict

Options for connecting to a realtime model.

Source code in src/agents/realtime/model.py
class RealtimeModelConfig(TypedDict):
    """Options for connecting to a realtime model."""

    api_key: NotRequired[str | Callable[[], MaybeAwaitable[str]]]
    """The API key (or function that returns a key) to use when connecting. If unset, the model will
    try to use a sane default. For example, the OpenAI Realtime model will try to use the
    `OPENAI_API_KEY`  environment variable.
    """

    url: NotRequired[str]
    """The URL to use when connecting. If unset, the model will use a sane default. For example,
    the OpenAI Realtime model will use the default OpenAI WebSocket URL.
    """

    initial_model_settings: NotRequired[RealtimeSessionModelSettings]
    """The initial model settings to use when connecting."""

api_key instance-attribute

api_key: NotRequired[
    str | Callable[[], MaybeAwaitable[str]]
]

The API key (or function that returns a key) to use when connecting. If unset, the model will try to use a sane default. For example, the OpenAI Realtime model will try to use the OPENAI_API_KEY environment variable.

url instance-attribute

url: NotRequired[str]

The URL to use when connecting. If unset, the model will use a sane default. For example, the OpenAI Realtime model will use the default OpenAI WebSocket URL.

initial_model_settings instance-attribute

initial_model_settings: NotRequired[
    RealtimeSessionModelSettings
]

The initial model settings to use when connecting.

RealtimeModel

Bases: ABC

Interface for connecting to a realtime model and sending/receiving events.

Source code in src/agents/realtime/model.py
class RealtimeModel(abc.ABC):
    """Interface for connecting to a realtime model and sending/receiving events."""

    @abc.abstractmethod
    async def connect(self, options: RealtimeModelConfig) -> None:
        """Establish a connection to the model and keep it alive."""
        pass

    @abc.abstractmethod
    def add_listener(self, listener: RealtimeModelListener) -> None:
        """Add a listener to the model."""
        pass

    @abc.abstractmethod
    def remove_listener(self, listener: RealtimeModelListener) -> None:
        """Remove a listener from the model."""
        pass

    @abc.abstractmethod
    async def send_event(self, event: RealtimeModelSendEvent) -> None:
        """Send an event to the model."""
        pass

    @abc.abstractmethod
    async def close(self) -> None:
        """Close the session."""
        pass

connect abstractmethod async

connect(options: RealtimeModelConfig) -> None

Establish a connection to the model and keep it alive.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
async def connect(self, options: RealtimeModelConfig) -> None:
    """Establish a connection to the model and keep it alive."""
    pass

add_listener abstractmethod

add_listener(listener: RealtimeModelListener) -> None

Add a listener to the model.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
def add_listener(self, listener: RealtimeModelListener) -> None:
    """Add a listener to the model."""
    pass

remove_listener abstractmethod

remove_listener(listener: RealtimeModelListener) -> None

Remove a listener from the model.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
def remove_listener(self, listener: RealtimeModelListener) -> None:
    """Remove a listener from the model."""
    pass

send_event abstractmethod async

send_event(event: RealtimeModelSendEvent) -> None

Send an event to the model.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
async def send_event(self, event: RealtimeModelSendEvent) -> None:
    """Send an event to the model."""
    pass

close abstractmethod async

close() -> None

Close the session.

Source code in src/agents/realtime/model.py
@abc.abstractmethod
async def close(self) -> None:
    """Close the session."""
    pass