#6000·pipecat

SarvamSTTService never reconnects after its websocket drops (reconnect half of #3699)

Author: Bot-Dev-RPACreated Oct 1, 2026Updated Oct 1, 2026

pipecat version

1.11.0, also reproduced on main at 49dea682f (2026-09-30). services/sarvam/stt.py is unchanged in 1.12.0 and main.

Python version

3.14.7

Operating System

Linux (Debian 13, aarch64). sarvamai 0.1.28 (the version pipecat pins), websockets 16.0. 8 kHz mono PCM input (telephony).

Issue description

Once the websocket of SarvamSTTService drops mid-session (a network blip, a load balancer reset, a server-side close such as 1011), the service never reconnects. Transcription stops for the rest of the session, and every following audio chunk produces a non-fatal ErrorFrame ("Error sending audio to Sarvam: ..."), one per 20 ms frame.

#3699 reported this together with the missing keepalive. The keepalive half shipped in #3730 and #3699 was closed, but the reconnect half is still open on main.

Why it happens:

  • SarvamSTTService is a plain STTService, so the WebsocketService reconnect loop does not apply, and it does not implement STTService._do_reconnect(), so the base _reconnect() machinery (audio buffering and replay included) is never used.
  • sarvamai's start_listening() catches the websockets close exception itself (it emits EventType.ERROR, which the service does not subscribe to) and returns normally. _receive_task_handler (stt.py:643-657) then just returns: nothing is logged and no error is pushed at the moment the socket dies.
  • _socket_client still points at the dead client, so run_stt (stt.py:488-524) keeps calling transcribe() on it and yields an ErrorFrame for every chunk.
  • A failed initial connect is not retried either: _connect() pushes one error and run_stt silently drops audio for the whole session.

Proposed fix: treat start_listening() returning (or raising) outside an intentional disconnect as a dropped socket and reconnect through the existing STTService._reconnect(): implement _do_reconnect() as _disconnect() + _connect() (raising when no socket came back, like DeepgramSTTService._do_reconnect), start it from a separate task (since _disconnect() cancels the receive task), and bound it with QuickFailureTracker and exponential_backoff_time, pushing force_treat_as_permanent=True once it gives up. Give a failed initial connect the same bounded retry. DeepgramSTTService._connection_handler (deepgram/stt.py:652), which also wraps a Fern SDK's start_listening(), is a close precedent. A local sketch along these lines makes the example below recover: a second connection, a second transcript, no error frames.

Reproduction steps

  1. Save the script below as repro.py.
  2. pip install "pipecat-ai[sarvam]". No API key or network is needed: a local websocket server stands in for Sarvam's /speech-to-text/ws, and the real sarvamai SDK connects to it.
  3. python repro.py. The fake server answers the first 200 ms of audio on each connection with a transcript, then drops connection 1. Audio keeps flowing for 3 s.

Minimal reproducible example

python
import asyncio
import json

from sarvamai import AsyncSarvamAI
from sarvamai.environment import SarvamAIEnvironment
from websockets.asyncio.server import serve

from pipecat.frames.frames import ErrorFrame, InputAudioRawFrame, TranscriptionFrame
from pipecat.pipeline.worker import PipelineParams
from pipecat.services.sarvam.stt import SarvamSTTService
from pipecat.tests.utils import SleepFrame, run_test

connections = 0


async def fake_sarvam(ws):
    """Local stand-in for /speech-to-text/ws: one transcript per connection,
    then connection 1 drops."""
    global connections
    connections += 1
    n, audio_messages = connections, 0
    async for raw in ws:
        if "audio" not in json.loads(raw):
            continue
        audio_messages += 1
        if audio_messages == 10:  # 200 ms of audio
            data = {
                "request_id": f"r{n}",
                "transcript": f"hello from connection {n}",
                "metrics": {"audio_duration": 0.2, "processing_latency": 0.05},
            }
            await ws.send(json.dumps({"type": "data", "data": data}))
            if n == 1:
                ws.transport.abort()  # network drop; `await ws.close(1011)` behaves the same
                return


async def main():
    async with serve(fake_sarvam, "127.0.0.1", 0) as server:
        port = server.sockets[0].getsockname()[1]
        stt = SarvamSTTService(api_key="test", sample_rate=8000)
        stt._sarvam_client = AsyncSarvamAI(
            api_subscription_key="test",
            environment=SarvamAIEnvironment(
                base=f"http://127.0.0.1:{port}", production=f"ws://127.0.0.1:{port}"
            ),
        )
        frames = []
        for _ in range(150):  # 3 s of 8 kHz audio in 20 ms frames
            frames += [
                InputAudioRawFrame(audio=b"\0\0" * 160, sample_rate=8000, num_channels=1),
                SleepFrame(sleep=0.02),
            ]
        down, up = await run_test(
            stt, frames_to_send=frames, pipeline_params=PipelineParams(audio_in_sample_rate=8000)
        )
    errors = [f.error for f in up if isinstance(f, ErrorFrame)]
    print("connections :", connections)
    print("transcripts :", [f.text for f in down if isinstance(f, TranscriptionFrame)])
    print("error frames:", len(errors), set(errors))


asyncio.run(main())

Expected behavior

The service notices the dropped socket, reconnects with a bounded backoff and keeps transcribing: a second connection and transcripts : ['hello from connection 1', 'hello from connection 2']. If it cannot reconnect, it reports a permanent error once, so the pipeline (or a ServiceSwitcher) can react.

Actual behavior

connections : 1
transcripts : ['hello from connection 1']
error frames: 140 {'Error sending audio to Sarvam: no close frame received or sent'}

With await ws.close(1011, "internal server error") instead of the abort the result is the same, with "received 1011 (internal error) ..." in the 140 errors. Refusing the first handshake (HTTP 503) gives one "Sarvam API error" frame and then no transcripts and no retry for the rest of the session.

Logs

bash
ERROR    | pipecat.processors.frame_processor:push_error_frame:1000 - SarvamSTTService#0 exception (websockets/legacy/protocol.py:916): Error sending audio to Sarvam: no close frame received or sent
WARNING  | pipecat.pipeline.worker:_source_push_frame:1482 - PipelineWorker#0: Something went wrong: ErrorFrame#0(error: Error sending audio to Sarvam: no close frame received or sent, fatal: False)
... the same pair for every 20 ms audio frame until the session ends

Nothing is logged at the moment the socket drops.

Before you submit

  • I have personally reproduced this issue on the latest Pipecat release (or main).
  • I have searched existing issues for this bug.
  • If AI tools helped write this report, I have verified the behavior myself and will personally answer follow-up questions.