1818 MetadataResponse ,
1919 SpeechStartedResponse ,
2020 UtteranceEndResponse ,
21- ErrorResponse ,
2221 CloseResponse ,
22+ ErrorResponse ,
23+ UnhandledResponse ,
2324)
2425from .options import LiveOptions
2526
@@ -244,14 +245,6 @@ async def _listening(self) -> None:
244245 utterance_end = result ,
245246 ** dict (self .kwargs ),
246247 )
247- case LiveTranscriptionEvents .Error .value :
248- result = ErrorResponse .from_json (message )
249- self .logger .verbose ("LiveTranscriptionEvents: %s" , result )
250- await self ._emit (
251- LiveTranscriptionEvents .Error ,
252- error = result ,
253- ** dict (self .kwargs ),
254- )
255248 case LiveTranscriptionEvents .Close .value :
256249 result = CloseResponse .from_json (message )
257250 self .logger .verbose ("CloseResponse: %s" , result )
@@ -260,18 +253,29 @@ async def _listening(self) -> None:
260253 close = result ,
261254 ** dict (self .kwargs ),
262255 )
256+ case LiveTranscriptionEvents .Error .value :
257+ result = ErrorResponse .from_json (message )
258+ self .logger .verbose ("LiveTranscriptionEvents: %s" , result )
259+ await self ._emit (
260+ LiveTranscriptionEvents .Error ,
261+ error = result ,
262+ ** dict (self .kwargs ),
263+ )
263264 case _:
264265 self .logger .warning (
265266 "Unknown Message: response_type: %s, data: %s" ,
266267 response_type ,
267268 data ,
268269 )
269- error = ErrorResponse (
270- type = "UnhandledMessage" ,
271- description = "Unknown message type" ,
272- message = f"Unhandle message type: { response_type } " ,
270+ unhandled = UnhandledResponse (
271+ type = LiveTranscriptionEvents .Unhandled .value ,
272+ raw = message ,
273+ )
274+ self ._emit (
275+ LiveTranscriptionEvents .Unhandled ,
276+ unhandled = unhandled ,
277+ ** dict (self .kwargs ),
273278 )
274- await self ._emit (LiveTranscriptionEvents .Error , error = error )
275279
276280 except websockets .exceptions .ConnectionClosedOK as e :
277281 self .logger .notice (f"_listening({ e .code } ) exiting gracefully" )
@@ -495,13 +499,17 @@ async def _signal_exit(self) -> None:
495499 except Exception as e :
496500 self .logger .error (f"_signal_exit - Exception: { str (e )} " )
497501
498- await asyncio .sleep (0.5 )
499-
500502 # push close event
501- await self ._emit (
502- LiveTranscriptionEvents .Close ,
503- CloseResponse (type = LiveTranscriptionEvents .Close .value ),
504- )
503+ try :
504+ await self ._emit (
505+ LiveTranscriptionEvents .Close ,
506+ CloseResponse (type = LiveTranscriptionEvents .Close .value ),
507+ )
508+ except Exception as e :
509+ self .logger .error (f"_signal_exit - Exception: { str (e )} " )
510+
511+ # wait for task to send
512+ await asyncio .sleep (0.5 )
505513
506514 # signal exit
507515 self ._exit_event .set ()
0 commit comments