SarvamRealtimeSTTService: a dropped or failed connection ends transcription for the rest of the session
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). websockets 16.0. 8 kHz mono PCM input (telephony).
Issue description
SarvamRealtimeSTTService has no way to recover its socket, so any connection problem ends speech recognition for the rest of the session:
- A mid-session drop is final. The constructor rejects
reconnect_on_error(stt.py:1004-1008) and passesreconnect_on_error=Falseto the base class (stt.py:1041). When the receive loop ends, the override instt.py:1217-1232marks the service unusable for every close, whatever its code, and all further audio is dropped. That includes closes Sarvam documents as retryable: its realtime guide lists 1011 as "Internal server error, retry with backoff";session.endis sent on "inactivity timeout, max duration, or server error"; and its LiveKit production guide advises, for 1008 ("Session timed out or exceeded max duration"), to "Plan a reconnect strategy for long calls". A rate-limited connect (1003 "Rate limit exceeded") is also transient, since the limiter reacts to the rate of new connections. - A failed first connect is never retried, and the service stays usable.
_connect_websocketreports the failure as non-permanent on purpose (0c7d5bf48, "Let a failed Sarvam realtime connection stay retryable": "_try_reconnectskips a service that has stopped being usable, so reporting a socket that would not open as permanent bars the retry that could open it"). But nothing in this service ever calls_try_reconnect: reconnection is forced off, no receive task is started without a socket, and_send_jsondoes not go throughsend_with_retry, whichWebsocketService._maybe_try_reconnectnames as the on-demand path for services that turn reconnection off. Sorun_sttsilently discards every chunk for the whole session, and because the service is still usable aServiceSwitchernever moves on either.
Failing over through a ServiceSwitcher (the rationale in 8fad287ae) helps only pipelines that keep a second STT service connected; a pipeline with one STT service has no way back, even though a new connection would work.
Proposed fix:
- Stop forcing
reconnect_on_error=False: accept it and default it toTruelike other websocket STT services. The base receive loop then reconnects with backoff, andQuickFailureTrackerplus the_try_reconnectretry cap already give up withforce_treat_as_permanent=True(costing the service its usability, so aServiceSwitchercan still move on) when the server keeps refusing. - Complete the in-flight utterance on each drop (for example in
_reconnect_websocket), not only when the loop finally exits. - Report closes that no retry can fix as permanent at once: 4000 (bad parameters or account), and 1003 unless the reason says rate limit (invalid key, quota).
- Give a failed first connect the same bounded retry (or, at minimum, report it as permanent so a switcher can take over).
A reconnect needs no config replay, since _query_params() builds the URL from self._settings, which config.update already keeps current. A local sketch along these lines recovers in all four scenarios below.
Reproduction steps
- Save the script below as
repro.py. pip install "pipecat-ai[sarvam]". No API key or network is needed: a local websocket server stands in for/speech-to-text-realtime/ws.python repro.py drop: connection 1 answers 500 ms of audio with a final transcript and then closes with 1011. Audio keeps flowing for 3 s.python repro.py connect: the first handshake is refused with HTTP 503; any later one would succeed.
Minimal reproducible example
import asyncio
import json
import sys
from http import HTTPStatus
from websockets.asyncio.server import serve
from pipecat.frames.frames import InputAudioRawFrame, TranscriptionFrame
from pipecat.pipeline.worker import PipelineParams
from pipecat.services.sarvam.stt import SarvamRealtimeSTTService
from pipecat.tests.utils import SleepFrame, run_test
# "drop": connection 1 closes with 1011 after one transcript.
# "connect": the first handshake is refused with HTTP 503, later ones succeed.
SCENARIO = sys.argv[1] if len(sys.argv) > 1 else "drop"
handshakes = 0
def process_request(connection, request):
global handshakes
handshakes += 1
if SCENARIO == "connect" and handshakes == 1:
return connection.respond(HTTPStatus.SERVICE_UNAVAILABLE, "try again\n")
async def fake_sarvam(ws):
"""Local stand-in for /speech-to-text-realtime/ws: one final transcript per
connection."""
n, chunks = handshakes, 0
await ws.send(json.dumps({"event": "session.begin", "request_id": f"r{n}"}))
async for raw in ws:
if json.loads(raw).get("event") != "audio_input":
continue
chunks += 1
if chunks == 10: # 500 ms of audio
await ws.send(json.dumps({"event": "transcript.final", "text": f"hello from connection {n}"}))
if SCENARIO == "drop" and n == 1:
# Sarvam documents 1011 as "Internal server error, retry with backoff".
await ws.close(1011, "internal server error")
return
async def main():
async with serve(fake_sarvam, "127.0.0.1", 0, process_request=process_request) as server:
port = server.sockets[0].getsockname()[1]
stt = SarvamRealtimeSTTService(
api_key="test", base_url=f"ws://127.0.0.1:{port}/ws", sample_rate=8000
)
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, _ = await run_test(
stt, frames_to_send=frames, pipeline_params=PipelineParams(audio_in_sample_rate=8000)
)
print("handshakes :", handshakes)
print("transcripts:", [f.text for f in down if isinstance(f, TranscriptionFrame)])
print("is_usable :", stt.is_usable)
asyncio.run(main())Expected behavior
drop: the service reconnects with backoff and keeps transcribing (2 handshakes, a transcript from each connection), and stays usable.connect: the service retries the handshake and transcribes; if it cannot connect at all, it becomes unusable so the pipeline or aServiceSwitchercan react.
Actual behavior
$ python repro.py drop
handshakes : 1
transcripts: ['hello from connection 1']
is_usable : False
$ python repro.py connect
handshakes : 1
transcripts: []
is_usable : TrueThe same happens for a TCP drop (1006) and for a connection closed at once with 1003 "Rate limit exceeded": one handshake, is_usable False. SarvamRealtimeSTTService(api_key=..., reconnect_on_error=True) raises TypeError.
Logs
# drop
WARNING | pipecat.services.websocket_service:_maybe_try_reconnect:325 - SarvamRealtimeSTTService#0 connection closed, but with an error: received 1011 (internal error) internal server error; then sent 1011 (internal error) internal server error
ERROR | pipecat.processors.frame_processor:push_error_frame:1000 - SarvamRealtimeSTTService#0 exception (websockets/asyncio/connection.py:324): SarvamRealtimeSTTService#0 connection closed, but with an error: received 1011 (internal error) internal server error; ...
DEBUG | pipecat.processors.frame_processor:set_usable:353 - SarvamRealtimeSTTService#0: no longer usable
# connect (nothing else is logged for the rest of the session)
ERROR | pipecat.processors.frame_processor:push_error_frame:1000 - SarvamRealtimeSTTService#0 exception (websockets/client.py:144): Unable to connect to Sarvam realtime STT: server rejected WebSocket connection: HTTP 503Before 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.
Source: pipecat-ai/pipecat