Skip to content

Commit 079b844

Browse files
authored
fix(voice): raise the builtin TimeoutError from _wait_for_event on 3.10 (#5075)
1 parent f23da76 commit 079b844

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/agents/voice/models/openai_stt.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ async def _wait_for_event(
9797
remaining = timeout - (monotonic() - start_time)
9898
if remaining <= 0:
9999
raise TimeoutError(f"Timeout waiting for event(s): {expected_types}")
100-
evt = await asyncio.wait_for(event_queue.get(), timeout=remaining)
100+
try:
101+
evt = await asyncio.wait_for(event_queue.get(), timeout=remaining)
102+
except asyncio.TimeoutError as e:
103+
# On Python 3.10 asyncio.wait_for raises asyncio.TimeoutError, which is not
104+
# the builtin TimeoutError the callers catch to wrap the failure as
105+
# STTWebsocketConnectionError. The two became one class in 3.11. Raise the
106+
# builtin so every timeout leaves this function as the same type.
107+
raise TimeoutError(f"Timeout waiting for event(s): {expected_types}") from e
101108
if isinstance(evt, ErrorSentinel):
102109
raise _ListenerError("Websocket listener failed") from evt.error
103110
evt_type = evt.get("type", "")

‎tests/voice/test_openai_stt.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,48 @@ def fake_time_func():
678678
await session.close()
679679

680680

681+
@pytest.mark.asyncio
682+
async def test_wait_for_event_raises_builtin_timeout_error_on_real_clock() -> None:
683+
"""The asyncio timeout inside _wait_for_event must surface as the builtin TimeoutError.
684+
685+
On Python 3.10 asyncio.wait_for raises asyncio.TimeoutError, a different class from
686+
the builtin; the callers only catch the builtin. This test uses the real clock so the
687+
asyncio timeout path runs, unlike the deadline test that patches monotonic.
688+
"""
689+
queue: asyncio.Queue[dict[str, str]] = asyncio.Queue()
690+
691+
with pytest.raises(TimeoutError, match="Timeout waiting for event"):
692+
await _wait_for_event(queue, ["session.created"], timeout=0.01)
693+
694+
695+
@pytest.mark.asyncio
696+
async def test_real_clock_session_creation_timeout_is_wrapped(monkeypatch: pytest.MonkeyPatch):
697+
"""A session.created that never arrives is reported as STTWebsocketConnectionError
698+
when the timeout comes from asyncio.wait_for rather than the patched deadline clock.
699+
"""
700+
monkeypatch.setattr("agents.voice.models.openai_stt.SESSION_CREATION_TIMEOUT", 0.01)
701+
mock_ws = create_mock_websocket([])
702+
703+
with patch("websockets.connect", return_value=mock_ws):
704+
audio_input = await StreamedAudioInputFactory.get(count=2)
705+
session = OpenAISTTTranscriptionSession(
706+
input=audio_input,
707+
client=create_mock_openai_client(),
708+
model="whisper-1",
709+
settings=STTModelSettings(),
710+
trace_include_sensitive_data=False,
711+
trace_include_sensitive_audio_data=False,
712+
)
713+
714+
with pytest.raises(STTWebsocketConnectionError) as exc_info:
715+
async for _ in session.transcribe_turns():
716+
pass
717+
718+
assert "Timeout waiting for transcription_session.created event" in str(exc_info.value)
719+
720+
await session.close()
721+
722+
681723
@pytest.mark.asyncio
682724
async def test_session_error_event(monkeypatch: pytest.MonkeyPatch):
683725
"""

0 commit comments

Comments
 (0)