LLM to TTS
Interruption Handling
Cancel in-flight TTS playback immediately when the user starts speaking — essential for natural conversational agents.
How it works
- Wrap TTS streaming in an
asyncio.Taskso it can be cancelled from outside. - Use an
asyncio.Eventas an interrupt signal. - When user speech is detected, set the event and cancel the task.
- 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())