Skip to content

Commit 1106e85

Browse files
Speak accepted native output and preserve terminal synthesis failures
1 parent 6ac6064 commit 1106e85

7 files changed

Lines changed: 478 additions & 338 deletions

File tree

‎examples/codex/realtime_voice.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Install the matching SDK's voice and classic-realtime extras and set
44
OPENAI_API_KEY. Run: python examples/codex/realtime_voice.py 'Your question' answer.wav
55
A text prompt starts the speech conversation; Realtime generates PCM audio.
6-
The two conversations stay separate. Application code must handle any native
6+
The two conversations stay separate. Delegation returns accepted final text
7+
after native validation and completion policies, without forwarding drafts. Application code must handle any native
78
Session interactions; this example grants no approvals. Realtime barge-in does
89
not automatically cancel native work. Scope exit explicitly cancels that work. Record for a bounded --seconds window;
910
a spoken preamble does not end the example before delegation completes.

‎examples/codex/voice.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Install the matching SDK's voice extra. Set OPENAI_API_KEY for speech and native
44
API requests. Run: python examples/codex/voice.py input.wav output.wav
55
Input must be mono PCM16 at 24 kHz. No microphone or speaker is opened.
6+
Speech begins after native completion accepts and validates the final text.
67
"""
78

89
import argparse

‎packages/codex/src/_agents_codex/voice/workflow.py‎

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88
from typing import Any
99

1010
from .._public import preserve_module_identity as _preserve_module_identity
11-
from ..observation import EventGap, OutputTextDelta, TurnStarted
1211
from ..session import Session
1312
from ..turn import Turn
1413

1514

16-
class VoiceEventGapError(RuntimeError):
17-
"""Native output observations were lost before they could be spoken."""
18-
19-
2015
@dataclass
2116
class _Invocation:
22-
submission: asyncio.Task[Turn[str]]
17+
submission: asyncio.Task[Turn[Any]]
2318
cancellation: asyncio.Task[None] | None = None
2419
finished: bool = False
2520

@@ -35,7 +30,11 @@ async def _join(task: asyncio.Task[Any]) -> None:
3530

3631

3732
class NativeSessionVoiceWorkflow:
38-
"""Speak native output while retaining the Session's history and policies.
33+
"""Speak accepted native output with the Session's history and policies.
34+
35+
Speech starts after native output validation and completion acceptance.
36+
Use a text-output Session; a structured result requires a custom workflow
37+
that selects and renders the fields the application wants spoken.
3938
4039
The application opens and closes the Session and handles its interactions.
4140
Each transcription submits a new root turn. Concurrent runs are rejected;
@@ -54,44 +53,31 @@ async def on_start(self) -> AsyncGenerator[str, None]:
5453
yield
5554

5655
async def run(self, transcription: str) -> AsyncGenerator[str, None]:
57-
"""Yield this transcription's native text; propagate terminal failures."""
56+
"""Yield only accepted final text after inherited output validation."""
5857
if not isinstance(transcription, str) or not transcription.strip():
5958
raise ValueError("transcription must be a nonempty string")
6059
if self._invocation is not None:
6160
raise RuntimeError("NativeSessionVoiceWorkflow already has an active run")
6261

6362
# Reserve before awaiting admission. Shielding retains the exact Turn if
6463
# cancellation races with submit returning its locally issued handle.
65-
invocation = _Invocation(
66-
asyncio.create_task(self._session.submit(transcription, output_type=str))
67-
)
64+
invocation = _Invocation(asyncio.create_task(self._session.submit(transcription)))
6865
self._invocation = invocation
6966
completed = False
7067
primary_error: BaseException | None = None
7168
try:
7269
turn = await asyncio.shield(invocation.submission)
73-
started = False
74-
preceding_gap = False
75-
async for event in turn.events():
76-
if invocation.cancellation is not None:
77-
raise asyncio.CancelledError
78-
if isinstance(event, TurnStarted) and event.submission_id == turn.submission_id:
79-
started = True
80-
preceding_gap = False
81-
elif isinstance(event, EventGap):
82-
if started:
83-
raise VoiceEventGapError("Native voice output observations were lost")
84-
preceding_gap = True
85-
elif isinstance(event, OutputTextDelta):
86-
if preceding_gap:
87-
raise VoiceEventGapError("Native voice turn start or output was lost")
88-
yield event.delta
89-
if preceding_gap:
90-
raise VoiceEventGapError("Native voice turn observations were lost")
91-
await turn.get_result()
70+
result = await turn.get_result()
9271
if invocation.cancellation is not None:
9372
raise asyncio.CancelledError
9473
completed = True
74+
if not isinstance(result.final_output, str):
75+
raise TypeError(
76+
"NativeSessionVoiceWorkflow requires a string final_output; "
77+
"use a text-output Session or a custom workflow to render structured output"
78+
)
79+
if result.final_output:
80+
yield result.final_output
9581
except BaseException as error:
9682
primary_error = error
9783
raise

0 commit comments

Comments
 (0)