tell me about ErrorEvent event from AgentSession
# ask-ai
b
tell me about ErrorEvent event from AgentSession
t
Thanks for reaching out, Jupiter. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking about the
ErrorEvent
emitted by
AgentSession
. Below is a concise summary of what it contains and how to handle it. • What it is:
ErrorEvent
is emitted when an error occurs during an
AgentSession
. It gives details so you can decide whether to recover automatically or take action. • Key properties: ◦
model_config
(dict): the current model configuration. ◦
error
(LLMError | STTError | TTSError | RealtimeModelError): the specific error object. It includes a
recoverable
boolean: ▪︎
recoverable: True
— informational; the session continues automatically. ▪︎
recoverable: False
— requires intervention (for example, using
.say()
to inform the user) after retries are exhausted. ◦
source
(LLM | STT | TTS | RealtimeModel): which component raised the error. • Recommended handling: ◦ If
recoverable
is True: log or monitor; no immediate action required. ◦ If
recoverable
is False: run a recovery flow (notify the user, switch behavior, or safely terminate the session). ◦ For STT/LLM/TTS, use
FallbackAdapter
(Python) to enable automatic failover to backup providers.
FallbackAdapter
will: ▪︎ Resubmit failed requests to backups. ▪︎ Mark failed providers as unhealthy. ▪︎ Continue using backups until the primary recovers and periodically check primary health. • Example (Python):
Copy code
from livekit.agents import llm, stt, tts
from livekit.plugins import assemblyai, deepgram, elevenlabs, openai, groq

session = AgentSession(
    stt=stt.FallbackAdapter([assemblyai.STT(), deepgram.STT()]),
    llm=llm.FallbackAdapter([openai.LLM(model="gpt-4o"), openai.LLM.with_azure(model="gpt-4o", ...)]),
    tts=tts.FallbackAdapter([elevenlabs.TTS(...), groq.TTS(...)]),
)
If you want, I can: • Show a code snippet demonstrating subscribing to
ErrorEvent
and handling recoverable vs. unrecoverable errors. • Explain how to surface a user-facing message via
.say()
when
recoverable
is false. Which would you like next? Sources: Events and error handling | LiveKit docs
b
Show a code snippet demonstrating subscribing to
ErrorEvent
and handling recoverable vs. unrecoverable errors.
t
Below is a concise Python example showing how to subscribe to
ErrorEvent
on
AgentSession
and handle recoverable vs. unrecoverable errors, including logging and using
.say()
to notify the user for unrecoverable errors.
Copy code
from livekit.agents import AgentSession
from livekit.agents.voice.events import ErrorEvent
import logging

logger = logging.getLogger(__name__)

# Example handler function
def on_error(event: ErrorEvent):
    # Inspect model config and error details
    model_cfg = event.model_config
    err = event.error
    source = event.source

    logger.info("AgentSession ErrorEvent from %s: %s", source, getattr(err, "message", repr(err)))
    logger.debug("Model config at error time: %s", model_cfg)

    # Handle recoverable vs unrecoverable
    if getattr(err, "recoverable", False):
        # Informational: session will continue automatically
        logger.warning("Recoverable error occurred. No user action required.")
        # Optionally record metrics, notify monitoring, etc.
    else:
        # Unrecoverable: take explicit action (e.g., inform the user, fallback behavior)
        logger.error("Unrecoverable error - initiating recovery flow.")
        # If you have access to the AgentSession instance, use .say() to notify the user
        try:
            # `session` must be in scope or captured by closure
            session.say("Sorry, there was an internal error. Please try again later.")
        except Exception as e:
            logger.exception("Failed to notify user via .say(): %s", e)
        # Optionally stop the session or trigger alternate flow:
        try:
            session.stop()
        except Exception:
            # stopping may be optional depending on your flow
            pass

# Create session (example; fill adapters/config as needed)
session = AgentSession(...)

# Subscribe to ErrorEvent
session.on("error", on_error)

# Start session as usual
session.start()
If you want, I can: • Provide a version that uses async/await (if your session is async). • Add metric/reporting hooks or show how to integrate
FallbackAdapter
usage in the same snippet. Which would you prefer?
b
when recoverable is True or False, do we have any conditions
t
You're asking what conditions cause an `ErrorEvent`'s
recoverable
flag to be True or False. Here's a concise, practical summary and examples. • True — transient, retryable failures ◦ Temporary network or API outages ◦ Rate limits or brief service interruptions ◦ Short-lived provider errors where automatic retries or a fallback provider can succeed ◦ Action: log/monitor; session continues and retries automatically • False — persistent, non-recoverable failures ◦ Invalid credentials or misconfiguration ◦ Exhausted retries for a request or provider marked unhealthy ◦ Permanent errors from the provider (e.g., unsupported operation, quota permanently exceeded) ◦ Action: notify the user (for example with
.say()
), run a recovery flow, switch workflow, or stop the session If you want, I can: • Show concrete examples mapping specific error types (LLMError, STTError, TTSError, RealtimeModelError) to recoverable vs unrecoverable cases. • Provide an async code snippet that inspects
err.recoverable
and implements logging, fallback, and
.say()
notification. Which would be more helpful? Sources: Events and error handling | LiveKit docs