wonderful-wolf-51568
12/06/2025, 12:53 PMtall-airline-16756
12/06/2025, 12:53 PMtall-airline-16756
12/06/2025, 12:53 PMtts_node in your Agent, preprocess the incoming AsyncIterable[str] to remove or replace words/sentences, then pass the filtered text to the default TTS pipeline. Below is a concise example you can adapt.
import re
from livekit.agents import Agent, ModelSettings
from livekit import rtc
from typing import AsyncIterable
class MyAgent(Agent):
async def tts_node(
self,
text: AsyncIterable[str],
model_settings: ModelSettings
) -> AsyncIterable[rtc.AudioFrame]:
words_to_filter = ["confidential", "internal", "ignore this"]
sentence_patterns_to_remove = [r"\btest sentence\b", r"placeholder"]
async def filtered_text():
async for chunk in text:
# remove whole-word matches
for w in words_to_filter:
chunk = re.sub(rf'\b{re.escape(w)}\b', '', chunk, flags=re.IGNORECASE)
# remove sentence patterns
for p in sentence_patterns_to_remove:
chunk = re.sub(p, '', chunk, flags=re.IGNORECASE)
# collapse whitespace and yield only non-empty chunks
chunk = re.sub(r'\s+', ' ', chunk).strip()
if chunk:
yield chunk
# forward filtered text to the built-in TTS implementation
async for frame in Agent.default.tts_node(self, filtered_text(), model_settings):
yield frame
Notes:
• Use re.escape() for dynamic words to avoid regex issues.
• Patterns let you remove full sentences or phrases; words list removes single-word matches.
• You can replace filtered words with placeholders (e.g., "[redacted]") instead of removing them by changing the replacement in re.sub.
• This works with streamed text chunks — ensure your patterns handle partial chunks if streaming splits words.
Would you like an example that replaces words with a spoken placeholder (e.g., "[redacted]") or one that preserves timing for streamed partial chunks?
Sources: Pipeline nodes and hooks | LiveKit docs