is given TTS failed is any fallback method availab...
# ask-ai
r
is given TTS failed is any fallback method available ? how to set fallback TTS
t
Thanks for reaching out, Av. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit offers a handy fallback method for TTS failures through the
FallbackAdapter
class in its Agents framework. This is designed to help you set up multiple TTS instances and smoothly switch to a backup provider if the primary one encounters any issues. Here’s how you can set up fallback TTS: 1. Create multiple TTS instances using different providers (like OpenAI, ElevenLabs, Cartesia). 2. Initialize the
FallbackAdapter
with a list of these TTS instances. 3. Use this adapter in your agent configuration instead of relying on just one TTS instance. The
FallbackAdapter
will automatically retry failed requests with the next available provider, mark any failed providers as unavailable, and regularly check if they’ve recovered. It supports both streaming and non-streaming TTS modes and even manages sample rate conversion if needed. If you’d like, I’m here to help you through the implementation or answer any questions you might have! Sources: Events and error handling | LiveKit Docs | Events and error handling | LiveKit Docs | livekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/tts.py
r
give me code sample
t
Here is a code sample for setting up fallback TTS using the
FallbackAdapter
in LiveKit:
Copy code
from livekit.agents import tts
from livekit.plugins import elevenlabs, groq  # Example plugins, replace with your providers

# Create instances of different TTS providers
tts1 = elevenlabs.TTS()  # Primary TTS provider
tts2 = groq.TTS()        # Fallback TTS provider

# Initialize the FallbackAdapter with detailed configuration
fallback_tts = tts.FallbackAdapter(
    tts=[tts1, tts2],
    max_retry_per_tts=2,           # Number of retries per provider before marking as unavailable
    sample_rate=24000              # Desired output sample rate; defaults to max of provider rates if None
)

# Handle availability changes
@fallback_tts.on("tts_availability_changed")
def on_tts_availability_changed(event):
    """Listen for TTS availability changes"""
    if event.available:
        print(f"TTS provider {event.tts.label} has recovered")
    else:
        print(f"TTS provider {event.tts.label} is now unavailable")

# Error handling with connection options
async def synthesize_with_options():
    try:
        # Use custom connection options for the request
        async with fallback_tts.synthesize(
            "Hello, this is a test",
            conn_options=tts.APIConnectOptions(
                timeout=15.0,      # Timeout for the request
                max_retry=0,       # Retry attempts per provider (handled by fallback)
                retry_interval=1.0 # Interval between retries
            )
        ) as stream:
            async for audio in stream:
                # Process synthesized audio
                pass
    except tts.APIConnectionError as e:
        print(f"All TTS providers failed: {e}")
        # Handle complete failure scenario
This setup ensures: • Automatic failover to the next provider if the current one fails • Background recovery of failed providers • Consistent audio quality with resampling • Event emission for monitoring provider availability If you have any questions or need a bit more help with this, please don’t hesitate to reach out—I’m here to support you! Sources: Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/stt/__init__.py