how do I intercept user's transcript, find specifi...
# ask-ai
a
how do I intercept user's transcript, find specific word and replace it before sending it to LLM?
✅ 1
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’ll show two ways to do this and ask one quick question. Which environment are you using: a custom LiveKit Agent (Python) or an external STT provider like Deepgram? If you’re using both, tell me which you prefer to modify (Agent pipeline vs STT config). If you’re using a custom Agent (Python): override the stt_node() and edit FINAL_TRANSCRIPT events before yielding them. Example:
Copy code
async def stt_node(self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings) -> Optional[AsyncIterable[stt.SpeechEvent]]:
    async def filtered_audio():
        async for frame in audio:
            yield frame

    async for event in Agent.default.stt_node(self, filtered_audio(), model_settings):
        if event.type == stt.SpeechEventType.FINAL_TRANSCRIPT:
            text = event.alternatives[0].text
            # replace word(s)
            text = text.replace("badword", "goodword")
            event.alternatives[0].text = text
        yield event
If you’re using Deepgram STT: use the built-in replace parameter via
extra_kwargs
(format "find:replace"). Example snippet conceptually: • Pass
extra_kwargs={"replace":"badword:goodword"}
in the STT config so Deepgram returns substituted transcripts. If you tell me which option you want (Agent override or Deepgram), and share the language/framework (Python/Node) you’re using, I’ll provide a tailored, copy-paste-ready example. Sources: Pipeline nodes and hooks | LiveKit Documentation, with interactive elements
a
I'm using python code and deepgram as my transcriber
t
Great — thanks for confirming. Below are two concise Python options you can use with Deepgram. Which do you prefer? (A) let Deepgram do replacements via its
replace
parameter, or (B) post-process transcripts in your Agent by overriding
stt_node()
? Tell me and I’ll give a copy-paste-ready snippet tuned to your setup. Option A — Deepgram replace (simpler, done by provider) • Pass
extra_kwargs={"replace":"find:replace,another:swap"}
in the STT config so Deepgram returns substituted transcripts. Option B — Agent-side post-processing (more control) • Override
stt_node()
and modify
FINAL_TRANSCRIPT
events before yielding them:
Copy code
async def stt_node(self, audio: AsyncIterable[rtc.AudioFrame], model_settings: ModelSettings) -> Optional[AsyncIterable[stt.SpeechEvent]]:
    async for event in Agent.default.stt_node(self, audio, model_settings):
        if event.type == stt.SpeechEventType.FINAL_TRANSCRIPT:
            text = event.alternatives[0].text
            # Example replacements
            replacements = {"badword":"goodword", "LiveKit":"Livekit"}
            for find, repl in replacements.items():
                text = text.replace(find, repl)
            event.alternatives[0].text = text
        yield event
If you want, tell me: • which option to expand, • whether replacements should be case-insensitive or use word boundaries, • and an example list of words to replace — I’ll return a ready-to-run Python snippet., with interactive elements