how to set the tts node after the agentsession has...
# ask-ai
c
how to set the tts node after the agentsession has been created?
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.
Just a heads up: once the
AgentSession
is created, you can’t set or change the TTS node. You’ll want to define the TTS node when you’re creating the
Agent
or
AgentSession
itself. If you want to customize how the TTS behaves, a great way is to override the
tts_node
method in a custom
Agent
subclass. This lets you tweak or preprocess the text before it goes to the TTS engine. Here’s a quick example:
Copy code
from livekit.agents.voice import Agent
from livekit.plugins import rime, openai

class CustomTtsAgent(Agent):
    async def tts_node(self, text: AsyncIterable[str], model_settings):
        async def process_text():
            async for chunk in text:
                # Modify text before TTS
                modified_chunk = chunk.replace("lol", "<laugh>")
                yield modified_chunk

        return Agent.default.tts_node(self, process_text(), model_settings)
When you’re ready to start the session, just use your custom agent like this:
Copy code
session = AgentSession()
await session.start(agent=CustomTtsAgent(), room=ctx.room)
If you have any questions or want to chat about a specific use case, I’m here to help! Sources: examples/drive-thru/drivethru_agent.py | pipeline-tts/short_replies_only.py | Pipeline nodes and hooks | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | Text-to-speech (TTS) integrations | LiveKit Docs
c
can I intercept the tts_node and just use a TTS object rather than using the Agent.default.tts_node
t
Absolutely, you can intercept the
tts_node
and plug in your own custom TTS object instead of using
Agent.default.tts_node
. By overriding the
tts_node
method in your
Agent
subclass, you get full control to tailor the TTS pipeline exactly how you want. Here’s a quick example of how you might use a custom TTS object directly:
Copy code
from livekit.plugins import rime

class CustomTtsAgent(Agent):
    async def tts_node(self, text: AsyncIterable[str], model_settings):
        # Use a custom TTS object
        custom_tts = rime.TTS(voice="custom-voice")
        return custom_tts.stream(text, model_settings)
Just make sure your custom TTS object supports streaming through a
stream()
method that returns an async iterable of audio frames. If it doesn’t support streaming out of the box, you might need to wrap it with a stream adapter. If you want me to walk you through this or if anything’s unclear, just give me a shout—I’m here to help! Sources: examples/voice_agents/structured_output.py | Agent speech and audio | LiveKit Docs | pipeline-tts/short_replies_only.py
c
yea let's say I want to use the elevenlabs tts plugin. please provide a working example
t
Here’s a handy example of how you can use the ElevenLabs TTS plugin with a custom
tts_node
implementation: Sources: livekit-agents/livekit/agents/voice/agent.py | pipeline-tts/tts_node.py | Pipeline nodes and hooks | LiveKit Docs | plugins/elevenlabs/src/tts.test.ts | ElevenLabs TTS integration guide | LiveKit Docs
c
so If I wanted to have a multi-agent system, where each agent had its own voice, how would I do it?
t
Hey there! If you’re looking to set up a multi-agent system in LiveKit where each agent has its own unique voice, you’re in the right place. You can give each agent a distinct Text-to-Speech (TTS) voice and smoothly switch between them using LiveKit’s agent handoff feature. Here’s a friendly step-by-step on how to do it: 1. Define Voice-Specific Agents Start by creating separate agent classes, each with its own TTS setup. Like this:
Copy code
from livekit.agents import Agent
from livekit.plugins import elevenlabs, openai

class AgentA(Agent):
    def __init__(self):
        super().__init__(
            tts=elevenlabs.TTS(voice_id="voice_id_A", model="eleven_multilingual_v2")
        )

class AgentB(Agent):
    def __init__(self):
        super().__init__(
            tts=openai.TTS(voice="shimmer")
        )
2. Use Agent Handoff for Smooth Transitions You can switch between agents dynamically using a function tool or by updating the session, like this:
Copy code
from livekit.agents import function_tool

@function_tool
async def switch_to_agent_b(self, context):
    return AgentB(), "Switching to Agent B."
Or simply update the session directly:
Copy code
session.update_agent(AgentB())
3. Share Context Between Agents To keep the conversation flowing naturally, pass along
userdata
or
chat_ctx
like this:
Copy code
next_agent = AgentB(chat_ctx=context.session.chat_ctx)
4. Optional: Let Agents Hear Each Other If you want your agents to pick up on each other’s speech, just configure
RoomInputOptions
like so:
Copy code
room_input_options=RoomInputOptions(participant_kinds=[rtc.ParticipantKind.PARTICIPANT_KIND_AGENT])
5. A Quick Example Imagine a storytelling app where a lead agent gathers user input and then hands off to specialist agents with their own unique voices depending on the story. For example:
Copy code
class LeadAgent(Agent):
    @function_tool
    async def transfer_to_storyteller(self, context):
        return StoryAgent(), "Let me connect you with our storyteller."
Does this sound like what you need? I’d be happy to help with a more detailed example tailored to your specific use case or preferred TTS provider! Sources: multi-agent/long_or_short_agent.py | main.py | How to Enable Agent-to-Agent Speech Detection in LiveKit | agent/main.py | AI Agents Framework | livekit/livekit_composite | DeepWiki