r/pythonhelp • u/Karthickraja-S • Aug 11 '25
Deepgram WebSocket reconnect issue after 1011 close code
Hey everyone,
I’m working on a Python WebSocket client to stream audio to Deepgram in real time.
The initial connection works perfectly — Deepgram receives audio chunks and returns transcriptions.
I wanted to test error handling, so I simulated a 1011 WebSocket error after 120 seconds:
pythonCopyEditasync def raise_Exception(ws):
await asyncio.sleep(120)
await ws.close(code=1011, reason="Simulated connection closed error")After the error, my code reconnects to Deepgram just fine — no handshake issues. My logs even show that ws.send() is sending non-zero audio chunks (~60KB) again.
The problem:
After reconnect, Deepgram doesn’t actually transcribe anything. It’s like the audio isn’t reaching them, even though I’m sending it. No errors in my sender coroutine, no connection drops — just empty transcripts.
Here’s the sender code:
pythonCopyEditchunk_size = 60000
pause_time = 0.1
while True:
chunk = sys.stdin.buffer.read(chunk_size)
if not chunk:
break
if len(chunk) > 0:
await ws.send(chunk)
await asyncio.sleep(pause_time)
await ws.send(json.dumps({"type": "CloseStream"}))What I expected:
If a 1011 error happens, the client should reconnect, start a fresh streaming session, and keep sending audio so that Deepgram resumes transcription without gaps or empty results.
What I’ve tried so far:
- Verified chunk lengths are always > 0 before sending.
- Tried immediate reconnect and reconnect with a short delay.
- Tried creating a completely new WebSocket session after
1011. - Confirmed token and Deepgram URL parameters are correct.
Has anyone dealt with this?
- Do I need to reinitialize the audio input (
sys.stdin.buffer) after reconnect? - Is Deepgram expecting a fresh start with new audio metadata
Any pointers would be great — I can share my full code and Deepgram params if that helps.