Streaming Audio — Error Handling

Handling Stream Errors & Reconnection

How to handle ERROR events and implement reconnection for long-running sessions.


Error handler

python
@conn.on(StreamingMessageType.ERROR)
async def on_error(msg):
    print(f"Stream error [{msg.code}]: {msg.message}")
    await conn.close()

Reconnection pattern

python
async def stream_with_reconnect(client, config, max_retries=3):
    for attempt in range(max_retries):
        try:
            conn = await client.asr.stream(config=config)
            # ... stream audio ...
            return conn
        except ConnectionError:
            wait = 2 ** attempt
            await asyncio.sleep(wait)
    raise ConnectionError("Max reconnection attempts exceeded")