88from typing import Any
99
1010from .._public import preserve_module_identity as _preserve_module_identity
11- from ..observation import EventGap , OutputTextDelta , TurnStarted
1211from ..session import Session
1312from ..turn import Turn
1413
1514
16- class VoiceEventGapError (RuntimeError ):
17- """Native output observations were lost before they could be spoken."""
18-
19-
2015@dataclass
2116class _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
3732class 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