From 59e80524ffcffc41eb723f040e3fe39d4a379681 Mon Sep 17 00:00:00 2001 From: Rohan Mehta Date: Thu, 17 Jul 2025 14:42:49 -0400 Subject: [PATCH] [realtime] Add direct access to model layer from session --- docs/realtime/guide.md | 11 +++++++++++ src/agents/realtime/session.py | 5 +++++ uv.lock | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/realtime/guide.md b/docs/realtime/guide.md index 9ea2525cf..cefa5d688 100644 --- a/docs/realtime/guide.md +++ b/docs/realtime/guide.md @@ -138,6 +138,17 @@ Send audio to the session using [`session.send_audio(audio_bytes)`][agents.realt For audio output, listen for `audio` events and play the audio data through your preferred audio library. Make sure to listen for `audio_interrupted` events to stop playback immediately and clear any queued audio when the user interrupts the agent. +## Direct model access + +You can access the underlying model to add custom listeners or perform advanced operations: + +```python +# Add a custom listener to the model +session.model.add_listener(my_custom_listener) +``` + +This gives you direct access to the [`RealtimeModel`][agents.realtime.model.RealtimeModel] interface for advanced use cases where you need lower-level control over the connection. + ## Examples For complete working examples, check out the [examples/realtime directory](https://github.com/openai/openai-agents-python/tree/main/examples/realtime) which includes demos with and without UI components. diff --git a/src/agents/realtime/session.py b/src/agents/realtime/session.py index 83e56d5fc..82ff924b8 100644 --- a/src/agents/realtime/session.py +++ b/src/agents/realtime/session.py @@ -107,6 +107,11 @@ def __init__( self._guardrail_tasks: set[asyncio.Task[Any]] = set() + @property + def model(self) -> RealtimeModel: + """Access the underlying model for adding listeners or other direct interaction.""" + return self._model + async def __aenter__(self) -> RealtimeSession: """Start the session by connecting to the model. After this, you will be able to stream events from the model and send messages and audio to the model. diff --git a/uv.lock b/uv.lock index 2e14dd804..bcd61fe4a 100644 --- a/uv.lock +++ b/uv.lock @@ -1482,7 +1482,7 @@ wheels = [ [[package]] name = "openai-agents" -version = "0.2.1" +version = "0.2.2" source = { editable = "." } dependencies = [ { name = "griffe" },