Skip to content

Commit 0b7ad38

Browse files
fix(example): refresh realtime push_to_talk_app session types (#2926)
## Summary - fix `Session` import in `examples/realtime/push_to_talk_app.py` to use `openai.types.realtime.session_update_event` - guard access to `turn_detection` for session variants that may not expose that attribute - annotate `session.id` access with `type: ignore` and comment, since the field is present at runtime but missing in generated typing ## Why `openai.types.realtime.session` no longer exists, so the example currently fails on import with recent SDK versions. Fixes #2715 ## Validation - `python3 -m py_compile examples/realtime/push_to_talk_app.py` - `python3 -m ruff check examples/realtime/push_to_talk_app.py` --------- Co-authored-by: Rishi Randhawa <rishirandhawa@users.noreply.github.com> Co-authored-by: Marcus Wood <marcuswood@openai.com>
1 parent 90ac74c commit 0b7ad38

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎examples/realtime/push_to_talk_app.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
from textual.containers import Container
4242

4343
from openai import AsyncOpenAI
44-
from openai.types.realtime.session import Session
4544
from openai.resources.realtime.realtime import AsyncRealtimeConnection
45+
from openai.types.realtime.session_created_event import Session
4646

4747

4848
class SessionDisplay(Static):
@@ -179,8 +179,10 @@ async def handle_realtime_connection(self) -> None:
179179
if event.type == "session.created":
180180
self.session = event.session
181181
session_display = self.query_one(SessionDisplay)
182-
assert event.session.id is not None
183-
session_display.session_id = event.session.id
182+
# The session ID is returned as an extra field on the session configuration.
183+
session_id = getattr(event.session, "id", None)
184+
assert isinstance(session_id, str)
185+
session_display.session_id = session_id
184186
continue
185187

186188
if event.type == "session.updated":
@@ -275,7 +277,12 @@ async def on_key(self, event: events.Key) -> None:
275277
self.should_send_audio.clear()
276278
status_indicator.is_recording = False
277279

278-
if self.session and self.session.turn_detection is None:
280+
if (
281+
self.session
282+
and self.session.audio
283+
and self.session.audio.input
284+
and self.session.audio.input.turn_detection is None
285+
):
279286
# The default in the API is that the model will automatically detect when the user has
280287
# stopped talking and then start responding itself.
281288
#

0 commit comments

Comments
 (0)