Skip to content

Commit de680e8

Browse files
Merge pull request #357 from dvonthenen/implement0-unhandled-events
Implement Unhandled Events
2 parents 5dee108 + a6ca9af commit de680e8

File tree

17 files changed

+152
-71
lines changed

17 files changed

+152
-71
lines changed

deepgram/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
MetadataResponse,
2424
SpeechStartedResponse,
2525
UtteranceEndResponse,
26-
ErrorResponse,
2726
CloseResponse,
27+
UnhandledResponse,
28+
ErrorResponse,
2829
)
2930

3031
# prerecorded

deepgram/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
MetadataResponse,
2525
SpeechStartedResponse,
2626
UtteranceEndResponse,
27-
ErrorResponse,
2827
CloseResponse,
28+
ErrorResponse,
29+
UnhandledResponse,
2930
)
3031

3132
# prerecorded

deepgram/clients/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
MetadataResponse,
1717
SpeechStartedResponse,
1818
UtteranceEndResponse,
19-
ErrorResponse,
2019
CloseResponse,
20+
ErrorResponse,
21+
UnhandledResponse,
2122
)
2223

2324
# prerecorded

deepgram/clients/listen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535

3636
# responses
3737
from .live import (
38+
OpenResponse,
3839
LiveResultResponse,
3940
MetadataResponse,
4041
SpeechStartedResponse,
4142
UtteranceEndResponse,
43+
CloseResponse,
4244
ErrorResponse,
45+
UnhandledResponse,
4346
)
4447

4548

deepgram/clients/live/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
MetadataResponse,
1313
SpeechStartedResponse,
1414
UtteranceEndResponse,
15-
ErrorResponse,
1615
CloseResponse,
16+
ErrorResponse,
17+
UnhandledResponse,
1718
)
1819

1920
from ...options import DeepgramClientOptions

deepgram/clients/live/client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
MetadataResponse as MetadataResponseLatest,
1313
SpeechStartedResponse as SpeechStartedResponseLatest,
1414
UtteranceEndResponse as UtteranceEndResponseLatest,
15-
ErrorResponse as ErrorResponseLatest,
1615
CloseResponse as CloseResponseLatest,
16+
ErrorResponse as ErrorResponseLatest,
17+
UnhandledResponse as UnhandledResponseLatest,
1718
)
1819

1920
"""
@@ -72,6 +73,14 @@ class UtteranceEndResponse(UtteranceEndResponseLatest):
7273
pass
7374

7475

76+
class CloseResponse(CloseResponseLatest):
77+
"""
78+
pass through for CloseResponse based on API version
79+
"""
80+
81+
pass
82+
83+
7584
class ErrorResponse(ErrorResponseLatest):
7685
"""
7786
pass through for ErrorResponse based on API version
@@ -80,9 +89,9 @@ class ErrorResponse(ErrorResponseLatest):
8089
pass
8190

8291

83-
class CloseResponse(CloseResponseLatest):
92+
class UnhandledResponse(UnhandledResponseLatest):
8493
"""
85-
pass through for CloseResponse based on API version
94+
pass through for UnhandledResponse based on API version
8695
"""
8796

8897
pass

deepgram/clients/live/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ class LiveTranscriptionEvents(str, Enum):
1717
UtteranceEnd: str = "UtteranceEnd"
1818
SpeechStarted: str = "SpeechStarted"
1919
Error: str = "Error"
20+
Unhandled: str = "Unhandled"
2021
Warning: str = "Warning"

deepgram/clients/live/v1/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
MetadataResponse,
1313
SpeechStartedResponse,
1414
UtteranceEndResponse,
15-
ErrorResponse,
1615
CloseResponse,
16+
ErrorResponse,
17+
UnhandledResponse,
1718
)

deepgram/clients/live/v1/async_client.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
MetadataResponse,
1919
SpeechStartedResponse,
2020
UtteranceEndResponse,
21-
ErrorResponse,
2221
CloseResponse,
22+
ErrorResponse,
23+
UnhandledResponse,
2324
)
2425
from .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()

deepgram/clients/live/v1/client.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
MetadataResponse,
2121
SpeechStartedResponse,
2222
UtteranceEndResponse,
23-
ErrorResponse,
2423
CloseResponse,
24+
ErrorResponse,
25+
UnhandledResponse,
2526
)
2627
from .options import LiveOptions
2728

@@ -244,14 +245,6 @@ 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("ErrorResponse: %s", result)
250-
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 @@ 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("ErrorResponse: %s", result)
259+
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-
self._emit(LiveTranscriptionEvents.Error, error=error)
275279

276280
except websockets.exceptions.ConnectionClosedOK as e:
277281
self.logger.notice(f"_listening({e.code}) exiting gracefully")
@@ -483,13 +487,17 @@ def _signal_exit(self) -> None:
483487
except Exception as e:
484488
self.logger.error(f"_signal_exit - Exception: {str(e)}")
485489

486-
time.sleep(0.5)
487-
488490
# push close event
489-
self._emit(
490-
LiveTranscriptionEvents.Close,
491-
CloseResponse(type=LiveTranscriptionEvents.Close.value),
492-
)
491+
try:
492+
self._emit(
493+
LiveTranscriptionEvents.Close,
494+
CloseResponse(type=LiveTranscriptionEvents.Close.value),
495+
)
496+
except Exception as e:
497+
self.logger.error(f"_signal_exit - Exception: {str(e)}")
498+
499+
# wait for task to send
500+
time.sleep(0.5)
493501

494502
# signal exit
495503
self._exit_event.set()

0 commit comments

Comments
 (0)