From 2eb244c80a28470200f3f004b6c8ec0a93905649 Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Tue, 24 Jun 2025 10:50:44 -0400 Subject: [PATCH] Send context_id when available in ElevenLabsTTSService keepalive message --- src/pipecat/services/elevenlabs/tts.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pipecat/services/elevenlabs/tts.py b/src/pipecat/services/elevenlabs/tts.py index f48ba5552..ccd9b5b3f 100644 --- a/src/pipecat/services/elevenlabs/tts.py +++ b/src/pipecat/services/elevenlabs/tts.py @@ -428,9 +428,21 @@ class ElevenLabsTTSService(AudioContextWordTTSService): while True: await asyncio.sleep(10) try: - # Send an empty message to keep the connection alive if self._websocket and self._websocket.open: - await self._websocket.send(json.dumps({})) + if self._context_id: + # Send keepalive with context ID to keep the connection alive + keepalive_message = { + "text": "", + "context_id": self._context_id, + } + logger.trace(f"Sending keepalive for context {self._context_id}") + else: + # It's possible to have a user interruption which clears the context + # without generating a new TTS response. In this case, we'll just send + # an empty message to keep the connection alive. + keepalive_message = {"text": ""} + logger.trace("Sending keepalive without context") + await self._websocket.send(json.dumps(keepalive_message)) except websockets.ConnectionClosed as e: logger.warning(f"{self} keepalive error: {e}") break