From 39ebc2c9c1a3341c8a0ebb9982453bac766834f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Mon, 25 Aug 2025 14:09:03 -0700 Subject: [PATCH] CartesiaTTSService: reconnect on Cartesia's timeout --- CHANGELOG.md | 7 +++++++ src/pipecat/services/cartesia/tts.py | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 408a37ec0..f6f8754ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to **Pipecat** will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Fixed a `CartesiaTTSService` issue that was causing the application to hang + after Cartesia's 5 minutes timed out. + ## [0.0.81] - 2025-08-25 ### Added diff --git a/src/pipecat/services/cartesia/tts.py b/src/pipecat/services/cartesia/tts.py index a2b013f48..53208b53f 100644 --- a/src/pipecat/services/cartesia/tts.py +++ b/src/pipecat/services/cartesia/tts.py @@ -386,7 +386,7 @@ class CartesiaTTSService(AudioContextWordTTSService): await self._websocket.send(msg) self._context_id = None - async def _receive_messages(self): + async def _process_messages(self): async for message in self._get_websocket(): msg = json.loads(message) if not msg or not self.audio_context_available(msg["context_id"]): @@ -419,6 +419,14 @@ class CartesiaTTSService(AudioContextWordTTSService): else: logger.error(f"{self} error, unknown message type: {msg}") + async def _receive_messages(self): + while True: + await self._process_messages() + # Cartesia times out after 5 minutes of innactivity (no keepalive + # mechanism is available). So, we try to reconnect. + logger.debug(f"{self} Cartesia connection was disconnected (timeout?), reconnecting") + await self._connect_websocket() + @traced_tts async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: """Generate speech from text using Cartesia's streaming API.