diff --git a/client/deepgram_transcription.py b/client/deepgram_transcription.py index 68b05a5..0feeba2 100644 --- a/client/deepgram_transcription.py +++ b/client/deepgram_transcription.py @@ -156,12 +156,23 @@ class DeepgramTranscriptionEngine: return True self._stop_event.clear() + self._ws_connected = threading.Event() self._is_recording = True # Start the asyncio event-loop thread (handles WS send/receive) self._thread = threading.Thread(target=self._run_event_loop, daemon=True) self._thread.start() + # Wait for the WebSocket to connect before starting audio capture. + # Without this, audio chunks arrive before the WS is open -> broken pipe. + if not self._ws_connected.wait(timeout=15): + logger.error("Timed out waiting for Deepgram WebSocket connection") + print("ERROR: Timed out waiting for Deepgram WebSocket connection") + self._last_error = "Timed out connecting to Deepgram" + self._is_recording = False + self._stop_event.set() + return False + # Start the audio capture stream try: self._start_audio_stream() @@ -285,6 +296,11 @@ class DeepgramTranscriptionEngine: if not await self._managed_handshake(): return + # Signal that the WebSocket is connected and ready + logger.info("WebSocket connected to Deepgram") + if hasattr(self, '_ws_connected'): + self._ws_connected.set() + # Run send and receive concurrently await asyncio.gather( self._send_loop(),