is on error event able to catch 429 error?
# ask-ai
r
is on error event able to catch 429 error?
t
Thanks for reaching out, Riddhi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, the
onError
event in LiveKit can catch a 429 error. When a 429 error ("Too Many Requests") occurs, LiveKit emits an
ErrorEvent
containing an error object of type
ResourceExhausted
. This error type is passed to the
onError
handler, allowing you to handle it appropriately. For example, you can log the error or notify the user:
Copy code
from livekit.agents import AgentSession, ErrorEvent

session = AgentSession(...)

@session.on("error")
def on_error(event: ErrorEvent):
    if event.error.type == "ResourceExhausted":
        print("429 Error caught: Too many requests")
        # Implement retry logic or user notification here
LiveKit also implements automatic exponential backoff for recoverable errors like 429, but you can customize the retry logic if needed. Let me know if you need further assistance! Sources: replay/cloud_replay.twirp.go | livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs
r
how many types of speechsynthesizer error can occure?
t
Hey there! Just wanted to share some info about the kinds of SpeechSynthesizer errors you might run into with LiveKit: • TTSError: This one’s all about text-to-speech hiccups. It has a handy
recoverable
flag that tells you if the session will try the operation again. • API-Related Errors: ◦ APITimeoutError: Happens when a TTS request takes too long and times out. ◦ APIStatusError: Comes up when there’s an HTTP status code error. ◦ APIConnectionError: Covers general connection issues. Also, LiveKit offers a
FallbackAdapter
that’s pretty neat—it automatically retries failed requests using backup providers and marks any providers that aren’t doing well as unhealthy until they bounce back. If you want, I’m here to help you dive deeper or get this set up smoothly! Sources: Events and error handling | LiveKit Docs | livekit-plugins/livekit-plugins-resemble/livekit/plugins/resemble/tts.py
r
riddhi - non recoverable error : type='tts_error' timestamp=1754536657.6554651 label='livekit.plugins.azure.tts.TTS' error=APIStatusError('Unsupported voice en.') recoverable=True we are getting this error no status code how to make a spefic scenarios backup?
t
Hey there! If you're running into the
APIStatusError 'Unsupported voice en.'
error and want to set up some handy backups for your TTS, here are a few friendly tips to get you going: 1. Use FallbackAdapter for TTS Try using the
FallbackAdapter
to smoothly switch between different TTS providers if one hits a snag. Here's a quick example:
Copy code
from livekit.agents import tts
from livekit.plugins import elevenlabs, groq

# Initialize TTS providers
tts_providers = [
    elevenlabs.TTS(),
    groq.TTS()
]

# Create FallbackAdapter
fallback_tts = tts.FallbackAdapter(tts_providers)
This way, it keeps an eye on your providers and switches automatically if needed. 2. Handle TTS Errors Gracefully It's a good idea to catch errors gently and offer a fallback message or action. Here's how you can do that:
Copy code
from pathlib import Path
import os
from livekit.agents.voice import AgentSession
from livekit.agents.utils.audio import audio_frames_from_file

# Path to your fallback audio file
fallback_audio_path = os.path.join(Path(__file__).parent.absolute(), "fallback_message.ogg")

@session.on("error")
def on_error(event):
    if event.error.recoverable:
        return  # Session will retry automatically

    session.say(
        "I'm experiencing technical difficulties. Please try again later.",
        audio=audio_frames_from_file(fallback_audio_path),
        allow_interruptions=False
    )
3. Configure TTS Providers with Supported Voices Make sure all your TTS providers inside the
FallbackAdapter
support the voice and language you're aiming for. Here's a quick setup:
Copy code
from livekit.plugins import elevenlabs, groq

# Initialize providers with appropriate voices
tts1 = elevenlabs.TTS(voice="en")
tts2 = groq.TTS(voice="english")

fallback_tts = tts.FallbackAdapter([tts1, tts2])
If you want to chat more about this or need a hand with anything else, just let me know—I'm here to help! Sources: Events and error handling | LiveKit Docs | livekit-plugins/livekit-plugins-rime/livekit/plugins/rime/tts.py
r
how to identify which spefic error has occured as we do not have status coe
t
To help you pinpoint specific TTS errors when there’s no status code available, here are some handy approaches you can try out: 1. Exception Property Analysis Take a look at the properties of the exception that pops up during TTS operations:
Copy code
try:
    chunked_stream = tts.synthesize(text)
    async for audio in chunked_stream:
        pass
except APIStatusError as e:
    print(f"Message: {e.message}")
    print(f"Request ID: {e.request_id}")
    print(f"Body: {e.body}")
    print(f"Retryable: {e.retryable}")
except APIConnectionError as e:
    print(f"Connection Error: {e.message}")
    print(f"Retryable: {e.retryable}")
2. Error Event Monitoring You can subscribe to error events to catch more details that might help:
Copy code
def on_tts_error(error_event):
    print(f"Error timestamp: {error_event.timestamp}")
    print(f"TTS label: {error_event.label}")
    print(f"Recoverable: {error_event.recoverable}")
    print(f"Original exception: {error_event.error}")
    print(f"Exception type: {type(error_event.error).__name__}")

tts_instance.on("error", on_tts_error)
3. Response Body Analysis Checking out the response body can sometimes reveal extra info about the error:
Copy code
try:
    # TTS operation
    pass
except APIStatusError as e:
    if e.body:
        try:
            error_data = json.loads(e.body) if isinstance(e.body, str) else e.body
            print(f"Error details: {error_data}")
        except (json.JSONDecodeError, TypeError):
            print(f"Raw error body: {e.body}")
4. Enhanced Logging Turning on detailed logging can really help capture the full picture when errors happen:
Copy code
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("livekit.agents.tts")

class TTSErrorHandler:
    def __init__(self, tts_instance):
        self.tts = tts_instance
        self.tts.on("error", self._log_error_details)

    def _log_error_details(self, error_event):
        logger.error(
            "TTS Error Details",
            extra={
                "tts_label": error_event.label,
                "error_type": type(error_event.error).__name__,
                "error_message": str(error_event.error),
                "recoverable": error_event.recoverable,
                "timestamp": error_event.timestamp,
            }
        )
Hopefully, these tips make it easier for you to spot and understand TTS errors, even when status codes aren’t showing up. If you want to dive deeper or have any questions, just give me a shout—I’m here to help! Sources: livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/tts.py