LLM to TTS

Interruption Handling

Cancel in-flight TTS playback immediately when the user starts speaking — essential for natural conversational agents.


How it works

  1. Wrap TTS streaming in an asyncio.Task so it can be cancelled from outside.
  2. Use an asyncio.Event as an interrupt signal.
  3. When user speech is detected, set the event and cancel the task.
  4. Inside the streaming loop, check the event on every chunk and break early if set.

VoiceAgent example

Interruption handling
import asyncio
from shunyalabs.tts import TTSConfig

class VoiceAgent:
    def __init__(self):
        self.tts_task = None
        self.interrupted = asyncio.Event()

    async def on_user_speech_detected(self):
        self.interrupted.set()
        if self.tts_task:
            self.tts_task.cancel()

    async def speak(self, text: str, client):
        self.interrupted.clear()
        config = TTSConfig(model="zero-indic", voice="Varun", response_format="pcm")

        async def _stream():
            async for audio in await client.tts.stream(text, config=config):
                if self.interrupted.is_set():
                    break

        self.tts_task = asyncio.create_task(_stream())