Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,17 @@ async def _connect_ws(self, timeout: float) -> aiohttp.ClientWebSocketResponse:
)

async def _close_ws(self, ws: aiohttp.ClientWebSocketResponse) -> None:
await ws.close()
try:
# Send CloseStream message to ensure Deepgram processes all remaining audio
# before closing the connection, as per Deepgram's API requirements
await ws.send_str(SynthesizeStream._CLOSE_MSG)

# Wait briefly for any final messages from Deepgram
await asyncio.sleep(0.1)
except Exception as e:
logger.warning(f"Error sending CloseStream message: {e}")
finally:
await ws.close()

def _ensure_session(self) -> aiohttp.ClientSession:
if not self._session:
Expand Down Expand Up @@ -220,6 +230,8 @@ async def _run(self, output_emitter: tts.AudioEmitter) -> None:


class SynthesizeStream(tts.SynthesizeStream):
_CLOSE_MSG: str = json.dumps({"type": "CloseStream"})

def __init__(self, *, tts: TTS, conn_options: APIConnectOptions):
super().__init__(tts=tts, conn_options=conn_options)
self._tts: TTS = tts
Expand Down