From 3410eb82b37803e84576112e7ab9d88a48cdf91b Mon Sep 17 00:00:00 2001 From: filipi87 Date: Thu, 12 Feb 2026 15:26:49 -0300 Subject: [PATCH] Fixing CartesiaTTSService to reuse the same context when needed. --- src/pipecat/services/cartesia/tts.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/pipecat/services/cartesia/tts.py b/src/pipecat/services/cartesia/tts.py index 791c60a18..1fa9a026a 100644 --- a/src/pipecat/services/cartesia/tts.py +++ b/src/pipecat/services/cartesia/tts.py @@ -8,6 +8,7 @@ import base64 import json +import uuid import warnings from enum import Enum from typing import AsyncGenerator, List, Literal, Optional @@ -539,6 +540,20 @@ class CartesiaTTSService(AudioContextWordTTSService): await self._get_websocket().send(cancel_msg) self._context_id = None + def create_context_id(self) -> str: + """Generate a unique context ID for a TTS request in case we don't have one already in progress. + + Returns: + A unique string identifier for the TTS context. + """ + # If a context ID does not exist, create a new one. + # If an ID exists, continue using the current ID. + # When interruptions happen, user speech results in + # an interruption, which resets the context ID. + if not self._context_id: + return str(uuid.uuid4()) + return self._context_id + async def flush_audio(self): """Flush any pending audio and finalize the current context.""" if not self._context_id or not self._websocket: