if i want to modify the text output from a "realti...
# ask-ai
b
if i want to modify the text output from a "realtime" model before sending to the "tts" model, how to do so?
t
Thanks for reaching out, Jura. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want to intercept the realtime model’s text, edit it, then pass it to TTS. You can do this by running the realtime model in text-only mode and post-processing before synthesis. To set this up: 1. Configure the realtime model to respond with text only (e.g.,
modalities = ["text"]
) and include a TTS instance in your AgentSession. This routes the model’s text into your TTS. Realtime models, TTS models, OpenAI Realtime plugin 2. Modify the text before TTS by overriding one of these nodes in your Agent: ◦
transcription_node
: adjust/post-process text segments (cleanup, replacements, add SSML) before forwarding to TTS. ◦
tts_node
: control chunking and apply provider-specific formatting/SSML right before synthesis. Pipeline nodes and hooks Would you like a short code sketch (Node or Python) showing a
transcription_node
or
tts_node
override that rewrites the text before TTS? Sources: Realtime model plugins | LiveKit Docs | Text-to-speech (TTS) models | LiveKit Docs | Pipeline nodes and hooks | LiveKit docs | OpenAI Realtime API plugin guide | LiveKit docs | AI Agents Framework | livekit/livekit_composite | DeepWiki
b
yes, and i'm using TTS Cartesia
python plz
t
You’re using Cartesia TTS and want to edit the realtime model’s text before synth. Configure the realtime model for text-only, then intercept/clean text in a node override before it reaches TTS. Example (Python):
Copy code
from livekit import agents, llm

class CleanOutputAgent(agents.Agent):
    async def llm_node(self, chat_ctx):
        # Call default LLM node, intercept streamed chunks, clean them, forward to TTS
        async for chunk in agents.Agent.default.llm_node(self, chat_ctx):
            text = chunk if isinstance(chunk, str) else getattr(chunk, "text", "")
            if not text:
                yield chunk
                continue

            # Example edits: strip tags, add SSML, fix phrasing, etc.
            cleaned = (
                text.replace("<think>", "")
                    .replace("</think>", "")
                    .replace("uh,", "")
            )

            if isinstance(chunk, llm.ChatChunk):
                yield llm.ChatChunk(text=cleaned, meta=getattr(chunk, "meta", None))
            else:
                yield cleaned

async def main():
    # Realtime model should respond with text only
    rt_model = {
        "provider": "openai-realtime",   # example provider
        "modalities": ["text"],
        # ... provider-specific config (model, api key via env, etc.)
    }

    # Cartesia TTS configuration (model + voice; add extras if desired)
    cartesia_tts = {
        "model": "cartesia/sonic-3",
        "voice": "YOUR_CARTESIA_VOICE_ID",
        # Optional extras (if supported): "language": "en-US", "extra_kwargs": {"speed": 1.0}
    }

    agent = CleanOutputAgent()
    session = agents.AgentSession(
        agent=agent,
        llm=rt_model,
        tts=cartesia_tts,
    )

    await session.start()
    # ... connect your media/IO as usual

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())
• Set the realtime model to text-only so the pipeline can modify text before TTS Realtime models. • Intercept and edit text in
llm_node
(or use `transcription_node`/`tts_node`) before synthesis Pipeline nodes. • Configure Cartesia via the session
tts
descriptor (model, voice, optional params) Cartesia TTS. Want a variant that applies SSML or moves the logic into
tts_node
for chunking/pronunciation? Sources: Pipeline nodes and hooks | LiveKit docs | Cartesia TTS | LiveKit docs | Realtime model plugins | LiveKit Docs | Cartesia TTS plugin guide | LiveKit Docs | Modifying LLM output before TTS | LiveKit Docs