diff --git a/src/pipecat/services/elevenlabs/tts.py b/src/pipecat/services/elevenlabs/tts.py index bbe05f9dc..61025e585 100644 --- a/src/pipecat/services/elevenlabs/tts.py +++ b/src/pipecat/services/elevenlabs/tts.py @@ -33,10 +33,7 @@ from pipecat.frames.frames import ( TTSStoppedFrame, ) from pipecat.processors.frame_processor import FrameDirection -from pipecat.services.tts_service import ( - AudioContextWordTTSService, - WordTTSService, -) +from pipecat.services.tts_service import AudioContextWordTTSService, WordTTSService from pipecat.transcriptions.language import Language, resolve_language from pipecat.utils.tracing.service_decorators import traced_tts @@ -347,6 +344,7 @@ class ElevenLabsTTSService(AudioContextWordTTSService): # Context management for v1 multi API self._context_id = None + self._last_closed_context_id = None self._receive_task = None self._keepalive_task = None @@ -586,6 +584,8 @@ class ElevenLabsTTSService(AudioContextWordTTSService): except Exception as e: logger.error(f"{self} exception: {e}") await self.push_error(ErrorFrame(error=f"{self} error: {e}")) + # Store the context ID before clearing it so we can identify delayed messages + self._last_closed_context_id = self._context_id self._context_id = None self._started = False self._partial_word = "" @@ -612,6 +612,14 @@ class ElevenLabsTTSService(AudioContextWordTTSService): f"Received a delayed message, recreating the context: {self._context_id}" ) await self.create_audio_context(self._context_id) + elif self._last_closed_context_id == received_ctx_id: + # This message belongs to a context we recently closed due to interruption. + # The audio was already generated by ElevenLabs but arrived after we closed + # the context, so we should discard it to avoid playing interrupted speech. + logger.debug( + f"Discarding delayed audio from recently closed context: {received_ctx_id}" + ) + continue else: # This can happen if a message is received _after_ we have closed a context # due to user interruption but _before_ the `isFinal` message for the context @@ -720,6 +728,8 @@ class ElevenLabsTTSService(AudioContextWordTTSService): # an interruption, which resets the context ID. if not self._context_id: self._context_id = str(uuid.uuid4()) + # Clear the last closed context when starting a new one + self._last_closed_context_id = None if not self.audio_context_available(self._context_id): await self.create_audio_context(self._context_id)