why is calling `_self_.session.say` in the middle ...
# ask-ai
m
why is calling
_self_.session.say
in the middle of a conversation not sending any audio to the session?
t
Thanks for reaching out, Anas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! When you call
self.session.say
in the middle of a conversation, it’s supposed to send audio to the session—that’s exactly what it’s made for. If it’s not working as expected, no worries! Here are some friendly troubleshooting tips to help you out: • Make sure a TTS model is set up: The
say()
method needs a TTS plugin to work. Double-check that your agent has a valid TTS instance initialized. • Check that the session is active: Confirm that
session.start()
has been awaited properly and the session isn’t paused or closed. • Look out for interruptions: If another speech task is running, it might interrupt your audio. You can peek at
session.current_speech
to see what’s currently happening. • Keep an eye on events: Listen for
speech_created
and
conversation_item_added
events to make sure your
say()
call actually triggered speech. • Watch agent state changes: The
agent_state_changed
event should show the agent moving into “speaking” mode after you call
say()
. • Confirm session output is on: Double-check that audio output isn’t turned off at the session or room level. Hopefully, these tips help you get it working smoothly! If you hit any snags or want to chat more about it, just let me know—I’m here to help. Sources: home_assistant/homeautomation.py | Events and error handling | LiveKit Docs | Agents Core Architecture | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs
m
it seems like it's being skipped because another model (the main LLM) is still holding
current_speech
. Is there a way to have 2 models (one LLM and one SLM) to generate different speech after each other without clashing using the same TTS?
t
Managing two models (LLM and SLM) that generate speech one after the other without stepping on each other’s toes using the same TTS is totally doable with LiveKit’s handy scheduling tools like
SpeechHandle
and
AgentActivity
. Here’s a friendly rundown on how to make it work: • Use the same TTS instance: Make sure both models send their speech through the same TTS instance that’s set up in your agent. • Queue speech tasks: The speech outputs from both models get lined up and played one at a time automatically with
SpeechHandle
, so nothing overlaps. • Interrupt if necessary: If the SLM needs to jump in and take priority over the LLM, just call
agent_activity.interrupt()
to stop the current speech and switch to the SLM’s message. • Set priorities: You can give urgent SLM messages a higher priority using
SPEECH_PRIORITY_HIGH
when you schedule their speech. • Control interruptions: For SLM messages that really need to break in, use
allow_interruptions=True
so they can interrupt any ongoing LLM speech. Here’s a quick example to illustrate:
Copy code
session.agent_say("This is a long explanation from the LLM...")

# Interrupt and prioritize SLM speech
session.agent.interrupt()
session.agent_say("Emergency alert! Please respond now.", allow_interruptions=True)
This way, both models get to speak clearly and smoothly without any clashes. If you want, I’m here to help you set it up or answer any questions! Sources: livekit-plugins/livekit-plugins-rime/livekit/plugins/rime/tts.py
m
would I need to "uninterrupt" the agent after my SLM already generated its speech?
t
Hey, no worries—you don’t need to “uninterrupt” the agent after your SLM finishes speaking. LiveKit handles all the interruption stuff automatically behind the scenes. Once the SLM is done, the agent will smoothly switch back to the right state (like “listening” or “thinking”) depending on how the conversation is going. If anything’s still unclear or you have more questions, just let me know—I’m here to help! Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs
m
basically the idea is that, my LLM will take a couple of seconds to generate, so I want the SLM to take over the TTS during that time while the LLM is still generating, then handover to the LLM
t
To help your SLM start using TTS while the LLM is still generating and then smoothly hand over to the LLM, you can try preemptive generation combined with streamed LLM output. Here’s a friendly step-by-step on how to make this happen: • Enable preemptive generation: Just set
preemptive_generation=True
in your
AgentSession
. This lets the TTS begin as soon as some partial text is ready. • Use structured output: Have the LLM return structured JSON with fields like
voice_instructions
and
response
. This way, the SLM can start processing early instructions while the LLM keeps generating. • Process early output: You can override the
tts_node
to spot and apply
voice_instructions
right away, while streaming the spoken response to TTS. For example, the LLM might send
{"voice_instructions": "speak quickly", "response": "I found your answer..."}
, and the SLM can use those
voice_instructions
to adjust the TTS style before the full response is ready. This way, the SLM and LLM work together smoothly, making things feel quicker and more natural. If you want, I’m here to help you set this up or tweak it further—just let me know! Sources: livekit-agents/livekit/agents/voice/agent_activity.py