when using session.say with a TTS node in conjunct...
# ask-ai
a
when using session.say with a TTS node in conjunction with a realtime model, that message is note being added to the Realtime context as it never existed
t
Thanks for reaching out, Sebastián. 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 point about messages from
session.say()
with a TTS node not appearing in the realtime model’s context. Why this happens: •
session.say()
only synthesizes and plays audio via TTS; it does not send a text event into the realtime model’s input stream. Realtime models keep their own internal transcript and only record outputs they generate or inputs they receive, so the spoken text from
say()
isn’t added to the model’s context. What you can do (choose one): • Use the realtime model to generate the reply so it’s recorded:
Copy code
await session.generate_reply(instructions="Say hello to the user")
This ensures the model produces the text (and it becomes part of its internal transcript). • Configure the realtime model for text-only output and run a separate TTS node:
Copy code
session = AgentSession(
      llm=openai.realtime.RealtimeModel(modalities=["text"]),
      tts="cartesia/sonic-3",
  )
Let the model produce text, then send that text to your TTS so the model’s context includes the reply. • If you must use
session.say()
, explicitly add the same assistant message into the model’s context after speaking:
Copy code
await session.say("Hello, user!")
  await session.add_message(role="assistant", content="Hello, user!")
(Use the correct API method your agent session exposes to inject messages.) If you’d like, share your AgentSession config and which realtime model/plugin you’re using (and whether you already try any of the approaches above) and I’ll give a targeted code example. Sources: Realtime model plugins | LiveKit Docs | Agent speech and audio | LiveKit docs | OpenAI Realtime API plugin guide | LiveKit docs
a
im using OpenAI Realtime
t
Thanks — responding about OpenAI Realtime specifically. Quick recap:
session.say()
only plays TTS audio and does not add a text event into the OpenAI Realtime model’s internal context, so the model won’t see or log that spoken text. Options to fix this for OpenAI Realtime: • Have the realtime model generate the reply so it’s recorded:
Copy code
await session.generate_reply(instructions="Say hello to the user")
• Configure the realtime model for text output and use a separate TTS node (model keeps context; you synthesize speech separately):
Copy code
session = AgentSession(
      llm=openai.realtime.RealtimeModel(modalities=["text"]),
      tts="cartesia/sonic-3",
  )
• If you must use
session.say()
, after speaking, inject the same assistant message into the realtime context:
Copy code
await session.say("Hello, user!")
  await session.add_message(role="assistant", content="Hello, user!")
Use the exact API method your AgentSession exposes to add messages. Tell me your current
AgentSession
config (modalities for OpenAI Realtime and how TTS is attached) and which approach you prefer, and I’ll provide a tailored code example.
a
i need to use the session.say
t
Thanks — responding to your requirement to keep using
session.say()
with OpenAI Realtime. Short answer: keep using
session.say()
for audio, then immediately inject the same assistant message into the realtime model’s conversation so the model’s internal context includes what was spoken. Example options: 1) Python
Copy code
await session.say("Hello, I'm your assistant.", allow_interruptions=False)
await session.add_message(role="assistant", content="Hello, I'm your assistant.")
2) TypeScript
Copy code
await session.say("Hello, I'm your assistant.", { allowInterruptions: false })
await session.addMessage({ role: "assistant", content: "Hello, I'm your assistant." })
If your SDK uses
append_message
/
appendMessage
instead of
add_message
/
addMessage
, use that method name. Tell me your AgentSession config (modalities, TTS plugin, SDK version) if you want a tailored snippet or if you want the model to acknowledge the message automatically via
generate_reply()
after
say()
. Sources: Agent speech and audio | LiveKit docs | OpenAI Realtime API plugin guide | LiveKit docs
a
the realtime session isnt having any context of the TTS say
t
You're right — OpenAI Realtime won't automatically see or record audio produced by
session.say()
. What to do (use one of these): • Inject the spoken text into the realtime context immediately after `session.say()`: Python
Copy code
await session.say("Hello, I'm your assistant.", allow_interruptions=False)
  await session.add_message(role="assistant", content="Hello, I'm your assistant.")
TypeScript
Copy code
await session.say("Hello, I'm your assistant.", { allowInterruptions: false });
  await session.addMessage({ role: "assistant", content: "Hello, I'm your assistant." });
• Or have the realtime model produce the reply (so it is recorded) and then TTS it:
Copy code
await session.generate_reply(instructions="Say: 'Hello, I'm your assistant.'")
Tell me which SDK method names your session supports (
add_message
vs
append_message
/
addMessage
vs
appendMessage
) and share your AgentSession config (modalities, TTS plugin). I’ll provide the exact snippet for your setup.
a
when using generate_reply, all system prompt context is flushed away
t
You're correct —
generate_reply()
can overwrite or sidestep the Realtime model's system instructions if those instructions aren't set as the model's persistent
instructions
at session init. Responding to your point about preserving system prompt context while still using `session.say()`: What to do now (pick one): • Persist system instructions on RealtimeModel initialization (best if you must use generate flows)
Copy code
session = AgentSession(
    llm=openai.realtime.RealtimeModel(
      instructions="You are a helpful assistant who always X, Y, Z",
      voice="alloy"
    ),
    tts="cartesia/sonic-3"
  )
Set system-level instructions once here and then avoid re-sending system prompts in
generate_reply()
. • Keep using
session.say()
and inject the spoken text into the model context afterward
Copy code
await session.say("Hello, I'm your assistant.", allow_interruptions=False)
  await session.add_message(role="assistant", content="Hello, I'm your assistant.")
(Use your SDK's exact method name:
add_message
/
append_message
/
addMessage
/
appendMessage
.) • Avoid
generate_reply()
resetting context by sending only user-role inputs (not system prompts)
Copy code
await session.generate_reply(user_input="User silent for 10s — prompt to ask if they need help")
This treats the call as a user turn and leaves the model's initial system instructions intact. If none of these fit your flow, tell me: • your AgentSession config (how you set
instructions
,
modalities
, and TTS), • which exact SDK method names you have for injecting messages, • and your SDK version. I’ll provide a precise snippet matching your setup. Sources: Agent speech and audio | LiveKit docs | OpenAI Realtime API plugin guide | LiveKit docs | Realtime model plugins | LiveKit Docs