How can I update the TTS configuration when switch...
# ask-ai
w
How can I update the TTS configuration when switching agents?
t
Thanks for reaching out, Nathan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you want to update the TTS configuration while switching agents, you can easily do this using the
update_options
method from the TTS plugin. This handy feature lets you tweak settings like voice, model, language, speed, and more—without having to pause or restart your session. Keep in mind, the exact options you can change depend on which TTS provider you're using. Here are some examples for different providers to help you get started: • Cartesia TTS: You can update the model, language, voice, speed, and even emotion controls. For example:
Copy code
tts.update_options(
    model="sonic-2-2025-03-07",
    language="es",
    voice="new-voice-id",
    speed=1.2,
    emotion=["happy"]
)
• Google TTS: Adjust language, gender, voice name, speaking rate, and volume gain. Here's how:
Copy code
tts.update_options(
    language="es-ES", 
    gender="female",
    voice_name="es-ES-Neural2-A",
    speaking_rate=1.1,
    volume_gain_db=5.0
)
• Neuphonic TTS: Change voice_id, language code, speed, and sample rate like this:
Copy code
tts.update_options(
    voice_id="new-voice-id",
    lang_code="es",
    speed=1.3,
    sample_rate=24000
)
When you switch agents, a good approach is to use function tools that return a new agent instance with the TTS settings you want. You can use the
on_enter
method of the new agent to announce the change and set up the TTS configuration right away. Or, if you prefer, you can just update the TTS options on the existing agent using
update_options
. If you’d like a hand with the implementation or have any questions, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-playai/livekit/plugins/playai/tts.py | pipeline-tts/tts_comparison.py
w
I don't want to update the options, I want to update the tts itself. For example, switching from openai to cartesia and back
t
If you want to switch between TTS providers on the fly, a handy way is to create function tools that return a new agent instance set up with the TTS provider you want. For example, you could have different agent classes for each provider (like OpenAI or Cartesia) and switch between them whenever you need. Here’s a simple example to give you an idea:
Copy code
@function_tool
async def switch_to_cartesia(self):
    """Switch to Cartesia TTS voice"""
    return CartesiaAgent()  # Returns agent with Cartesia TTS configuration

@function_tool
async def switch_to_openai(self):
    """Switch to OpenAI TTS voice"""
    return OpenAIAgent()  # Returns agent with OpenAI TTS configuration
When you switch, just make sure each agent is properly set up with its own TTS settings. You can even use the
on_enter
method of the new agent to announce the change and get everything initialized smoothly. If you want me to walk you through this or have any questions, just give me a shout!
w
In my code I update it explicitly like this: new_agent = CharacterAgent(_chat_ctx_=fresh_chat_ctx) session.userdata.character_agent_ref = new_agent how can i also change the tts
t
Hey there! If you want to switch up the TTS provider when creating a new agent instance, you can totally set each agent with a specific TTS plugin and swap between them on the fly. Here’s a friendly walkthrough on how to do it: 1. Define Agents with Different TTS Providers Start by creating separate agent classes, each one using a particular TTS provider. For example:
Copy code
from livekit.plugins import cartesia, openai

class CartesiaAgent(CharacterAgent):
    def __init__(self, chat_ctx):
        super().__init__(chat_ctx=chat_ctx, tts=cartesia.TTS(model="sonic-preview", voice="voice-id"))

class OpenAIAgent(CharacterAgent):
    def __init__(self, chat_ctx):
        super().__init__(chat_ctx=chat_ctx, tts=openai.TTS(model="gpt-4"))
2. Switch Agents Dynamically Whenever you create a new agent instance, just pass in the TTS provider you want. Like this:
Copy code
new_agent = CartesiaAgent(chat_ctx=fresh_chat_ctx)
session.userdata.character_agent_ref = new_agent
And if you want to switch back, simply create the other agent:
Copy code
new_agent = OpenAIAgent(chat_ctx=fresh_chat_ctx)
session.userdata.character_agent_ref = new_agent
3. Use
on_enter
for Announcements
Each agent can use the
on_enter
method to let everyone know about the change:
Copy code
async def on_enter(self):
    await self.session.say("Hello! I am now using the Cartesia TTS voice.")
This way, you get smooth transitions between TTS providers without losing the session flow. If you want me to help you with anything else, just give me a shout! Sources: multi-agent/long_or_short_agent.py | OpenAI Realtime API integration guide | LiveKit Docs | Gemini Live API integration guide | LiveKit Docs | Text-to-speech (TTS) integrations | LiveKit Docs | LiveKit integration guides | LiveKit Docs