@@ -84,24 +84,20 @@ async def _process_audio_input(self, audio_input: AudioInput) -> str:
8484 )
8585
8686 async def _run_single_turn (self , audio_input : AudioInput ) -> StreamedAudioResult :
87- # Since this is single turn, we can use the TraceCtxManager to manage starting/ending the
88- # trace
89- with TraceCtxManager (
90- workflow_name = self .config .workflow_name or "Voice Agent" ,
91- trace_id = None , # Automatically generated
92- group_id = self .config .group_id ,
93- metadata = self .config .trace_metadata ,
94- tracing = self .config .tracing ,
95- disabled = self .config .tracing_disabled ,
96- ):
97- input_text = await self ._process_audio_input (audio_input )
98-
99- output = StreamedAudioResult (
100- self ._get_tts_model (), self .config .tts_settings , self .config
101- )
102-
103- async def stream_events ():
87+ output = StreamedAudioResult (self ._get_tts_model (), self .config .tts_settings , self .config )
88+
89+ async def stream_events ():
90+ # Keep the trace scope active for the entire async processing lifecycle.
91+ with TraceCtxManager (
92+ workflow_name = self .config .workflow_name or "Voice Agent" ,
93+ trace_id = None , # Automatically generated
94+ group_id = self .config .group_id ,
95+ metadata = self .config .trace_metadata ,
96+ tracing = self .config .tracing ,
97+ disabled = self .config .tracing_disabled ,
98+ ):
10499 try :
100+ input_text = await self ._process_audio_input (audio_input )
105101 async for text_event in self .workflow .run (input_text ):
106102 await output ._add_text (text_event )
107103 await output ._turn_done ()
@@ -111,37 +107,37 @@ async def stream_events():
111107 await output ._add_error (e )
112108 raise e
113109
114- output ._set_task (asyncio .create_task (stream_events ()))
115- return output
110+ output ._set_task (asyncio .create_task (stream_events ()))
111+ return output
116112
117113 async def _run_multi_turn (self , audio_input : StreamedAudioInput ) -> StreamedAudioResult :
118- with TraceCtxManager (
119- workflow_name = self .config .workflow_name or "Voice Agent" ,
120- trace_id = None ,
121- group_id = self .config .group_id ,
122- metadata = self .config .trace_metadata ,
123- tracing = self .config .tracing ,
124- disabled = self .config .tracing_disabled ,
125- ):
126- output = StreamedAudioResult (
127- self ._get_tts_model (), self .config .tts_settings , self .config
128- )
129-
130- try :
131- async for intro_text in self .workflow .on_start ():
132- await output ._add_text (intro_text )
133- except Exception as e :
134- logger .warning (f"on_start() failed: { e } " )
135-
136- transcription_session = await self ._get_stt_model ().create_session (
137- audio_input ,
138- self .config .stt_settings ,
139- self .config .trace_include_sensitive_data ,
140- self .config .trace_include_sensitive_audio_data ,
141- )
142-
143- async def process_turns ():
114+ output = StreamedAudioResult (self ._get_tts_model (), self .config .tts_settings , self .config )
115+
116+ async def process_turns ():
117+ # Keep the trace scope active for the full streamed session.
118+ with TraceCtxManager (
119+ workflow_name = self .config .workflow_name or "Voice Agent" ,
120+ trace_id = None ,
121+ group_id = self .config .group_id ,
122+ metadata = self .config .trace_metadata ,
123+ tracing = self .config .tracing ,
124+ disabled = self .config .tracing_disabled ,
125+ ):
126+ transcription_session = None
144127 try :
128+ try :
129+ async for intro_text in self .workflow .on_start ():
130+ await output ._add_text (intro_text )
131+ except Exception as e :
132+ logger .warning (f"on_start() failed: { e } " )
133+
134+ transcription_session = await self ._get_stt_model ().create_session (
135+ audio_input ,
136+ self .config .stt_settings ,
137+ self .config .trace_include_sensitive_data ,
138+ self .config .trace_include_sensitive_audio_data ,
139+ )
140+
145141 async for input_text in transcription_session .transcribe_turns ():
146142 result = self .workflow .run (input_text )
147143 async for text_event in result :
@@ -152,8 +148,9 @@ async def process_turns():
152148 await output ._add_error (e )
153149 raise e
154150 finally :
155- await transcription_session .close ()
151+ if transcription_session is not None :
152+ await transcription_session .close ()
156153 await output ._done ()
157154
158- output ._set_task (asyncio .create_task (process_turns ()))
159- return output
155+ output ._set_task (asyncio .create_task (process_turns ()))
156+ return output
0 commit comments