Subject: Empty Text Response Sent to TTS Before Fu...
# ask-ai
e
Subject: Empty Text Response Sent to TTS Before Function Call Execution Question: We're using LiveKit Agents with OpenAI LLM and function calling (via @function_tool). When the LLM decides to call a function (e.g., end_interview_early()), it sometimes generates an empty text response (content: "") along with the function call. The Problem: The LiveKit Agent framework processes the text content of the LLM response and sends it to the TTS pipeline before executing the function call. When this text is empty or contains only punctuation, our TTS provider (Cartesia) rejects it with: "Invalid transcript: Transcript empty or contains only punctuation." Current Workaround: We've added validation in our tts_node override to detect and block empty/invalid text before it reaches TTS, but this feels like a workaround. Question: Is there a way to configure the Agent to: 1. Skip TTS processing when a function call is present - If the LLM response contains tool_calls, should the text content be sent to TTS at all? 2. Prioritize function calls over text content - Can we configure the Agent to execute function calls first, and only process text content if no function calls are present? 3. Filter empty text responses - Is there a built-in mechanism to filter out empty or whitespace-only text before it reaches TTS? Technical Details: • Using livekit-agents==1.2.1 • Using livekit.plugins.openai.LLM with model="gpt-4o" • Function tools created with @function_tool decorator • TTS provider: Cartesia (via livekit.plugins.cartesia.TTS) • We're overriding tts_node in our custom Agent class to add validation Example Scenario: 1. User says: "I have to go" 2. LLM generates response with: • content: "" (empty text) • tool_calls: [{function: "end_interview_early"}] 3. Agent framework sends empty text to TTS → Cartesia error 4. Function call executes → Interview ends correctly Desired Behavior: When a function call is present in the LLM response, we'd prefer that: • Empty text content is ignored (not sent to TTS) • Function call executes immediately • Only valid text content (when no function calls) goes to TTS Is there a recommended approach or configuration option for handling this scenario?
t
Thanks for reaching out, Jason. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I understand how important it is to get this right, so let's walk through some friendly tips to help you handle empty text responses being sent to TTS when function calls are involved: 1. Prioritize Function Calls Over Text Content: The LiveKit Agents are designed to prioritize function call execution before generating any TTS responses. If your LLM supports auto-generation of tool replies and no verbal reply is needed, the agent will skip TTS altogether. Just make sure that the
auto_tool_reply_generation
feature is enabled in your LLM configuration to take advantage of this. 2. Skip TTS for Empty or Non-Verbal Responses: You can customize the
tts_node
to prevent empty or whitespace-only text from being sent to the TTS engine. Here’s a helpful example you might find useful:
Copy code
def tts_node(self, text: str, **kwargs: Any) -> AsyncIterable[tts.SynthesizedAudio]:
    if not text.strip():
        raise StopResponse()  # Prevents empty text from being sent to TTS
    return Agent.default.tts_node(self, text, **kwargs)
3. Stop TTS Generation via Hooks: Another great way is to use the
on_user_turn_completed
hook to check incoming messages and stop speech generation when appropriate. For example:
Copy code
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
    if not new_message.text_content:
        raise StopResponse()  # Prevents agent from generating any response
These approaches will help ensure that function calls happen right away, empty text is gracefully ignored, and only meaningful content gets sent to TTS. If you’d like, I’m here to help you implement these or answer any questions you have along the way! Sources: Pipeline nodes and hooks | LiveKit docs | agents/src/voice/agent_activity.ts | livekit-agents/livekit/agents/voice/agent_activity.py | Tool definition and use | LiveKit docs
e
how do i ensure auto_tool_reply_generation is enabled?
t
Hi there! I wanted to clarify that the parameter
auto_tool_reply_generation
isn’t actually part of LiveKit Agents. But no worries—tool reply generation is managed through a few other helpful ways: 1. Tool Return Values: When a tool returns a value, the agent will automatically create a verbal reply. If nothing is returned, then no reply is generated. 2. Tool Choice Parameter: You can customize how the LLM uses tools with the
tool_choice
parameter. For instance:
Copy code
from livekit.plugins import openai

llm = openai.LLM(
    model="gpt-4o-mini",
    tool_choice="auto"  # This lets the model decide when to use tools
)
3. Manual Control: If you want to take charge, you can use the
function_tools_executed
event to manually override reply generation. Here’s an example:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev):
    ev.cancel_tool_reply()  # This stops automatic reply generation
These options give you great flexibility in deciding when and how replies are created after tools run. If anything’s unclear or you’d like a hand setting this up, just let me know—I’m here to help! Sources: examples/voice_agents/silent_function_call.py | OpenAI LLM plugin guide | LiveKit Docs