how to filter words in entire session before TTS. ...
# ask-ai
l
how to filter words in entire session before TTS. All agents and AgentTasks
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your request about filtering words for the entire session before TTS for all agents and AgentTasks. Short answer: implement the filtering in each agent’s LLM output stage by overriding the agent’s
llm_node
so you can inspect and modify every streaming chunk before it goes to TTS. If your agents share a common base class, put the override there so it applies to all agents and tasks. What this looks like (conceptual steps) 1. Intercept the LLM streaming output inside
llm_node
. 2. For each chunk, extract the text content safely. 3. Apply your filtering/replacement rules (e.g., replace words, mask profanity, remove internal tags). 4. Yield the modified chunk so downstream nodes (including TTS) receive the cleaned text. Example pattern (Python-like pseudocode)
Copy code
async def llm_node(self, chat_ctx, tools, model_settings=None):
    async def process_stream():
        async with self.llm.chat(chat_ctx=chat_ctx, tools=tools, tool_choice=None) as stream:
            async for chunk in stream:
                if chunk is None:
                    continue

                # Safely extract content from the chunk
                content = getattr(chunk.delta, 'content', None) if hasattr(chunk, 'delta') else str(chunk)
                if content is None:
                    yield chunk
                    continue

                # Filtering logic - replace or mask unwanted words
                processed = content.replace("unwanted_word", "filtered")
                # (Use regex or word-boundary checks for robust matching)

                # Put processed text back into the chunk
                if processed != content:
                    if hasattr(chunk, 'delta') and hasattr(chunk.delta, 'content'):
                        chunk.delta.content = processed
                    else:
                        chunk = processed

                yield chunk

    return process_stream()
Important notes and tips • Apply this override in a shared base Agent class if you want the behavior across all agents without copying code. • AgentTasks use the agent’s runtime behavior — ensure the agent instance handling the AgentTask uses the filtered
llm_node
. • Use robust matching (regex with word boundaries, case-insensitive) to avoid accidental replacements (e.g., “class” matching “ass”). • Consider whether you want simple replace/mask, a blocklist, or context-aware filtering; context-aware filtering may require post-processing that preserves meaning while removing sensitive terms. • Also filter/remove internal reasoning tokens or tags (like
<tool_call>
or “having thought”) if they appear in model output before TTS. A couple of clarifying questions so I can provide a targeted code sample: • Are you using a custom Agent base class already, or do you have multiple independent Agent implementations? • Do you want simple replacement/masking, or more advanced/contextual filtering (e.g., profanity detection with exceptions)? • Which language/runtime are you implementing this in (Python, JS/TS, other)? Tell me those details and I’ll give a concrete implementation you can drop into your agents (including regex examples and a recommended shared base-class approach). Sources: LLM Output Replacement | LiveKit Documentation | Simple Content Filter | LiveKit Documentation