when using session.generate_reply with OpenAI real...
# ask-ai
a
when using session.generate_reply with OpenAI realtime model, are instructions replacing the system instructions instead of appending them for that specific speech handle?
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.
You're asking whether
instructions
passed to
session.generate_reply
with the OpenAI realtime model replace the agent/model system instructions or are appended for that single speech handle. They do not replace the system instructions.
instructions
are transient directives applied only to that single reply: they guide the LLM for that turn, are combined with the existing system context, and do not overwrite the agent or model's core instructions. If you provide
user_input
instead, that text is added to chat history as a user message;
instructions
are used internally and not recorded in history. Sources: Agent speech and audio | LiveKit docs
a
if i wanted to use session.say, could i do it with the realtime?
t
You're asking if
session.say
can be used with the OpenAI Realtime model. Yes — you can use
session.say
with Realtime, but you must configure a TTS plugin in your
AgentSession
.
session.say
relies on a TTS instance to synthesize audio; the Realtime model itself doesn't provide built-in script-to-speech for
say
. Example:
Copy code
session = AgentSession(
  llm=openai.realtime.RealtimeModel(voice="marin"),
  tts="cartesia/sonic-3",
)

session.say("Hello")
If you prefer not to attach a TTS plugin, use
session.generate_reply(instructions="...")
to have the Realtime model produce the reply content directly. Sources: Agent speech and audio | LiveKit docs | Turn detection and interruptions | LiveKit Docs
a
and if added tts but i want it only for session.say (all the rest, let the Realtiem model produce replies, is it possible?)
t
Yes — you can. Configure a TTS plugin on your AgentSession so
session.say()
uses that TTS, and let the OpenAI Realtime model handle other replies. The TTS is only used for
session.say()
calls; other responses (e.g.,
generate_reply()
or normal realtime replies) are produced by the Realtime model’s built-in speech generation. Example:
Copy code
session = AgentSession(
  llm=openai.realtime.RealtimeModel(voice="marin"),  # realtime handles normal replies
  tts="cartesia/sonic-3",                            # used only for session.say()
)

await session.say("This uses the TTS plugin")
# other replies remain handled by the Realtime model
Would you like an example in your language/runtime or guidance on TTS voice selection? Sources: Realtime model plugins | LiveKit Docs
a
i need a way to update the chat context even of the realtime session
t
You're asking how to update the chat context of a Realtime session. I can provide a concrete example and exact steps — I just need a few quick details to give the right code and recommendations: 1. Which SDK and version are you using (Python, Node, Agents package version)? 2. Are you using
AgentSession
with
openai.realtime.RealtimeModel
or a lower-level realtime connection? 3. Which kind of update do you want: ◦ persistent system-level context for future turns, ◦ a temporary per-turn instruction, or ◦ appending a user/assistant message into visible chat history? 1. Do you need the update to occur mid-stream (while Realtime is actively responding) or only between replies? 2. Any runtime preferences (sync vs async) or need to include TTS/`session.say` behavior? Tell me these and I’ll send a targeted Python (or Node) example plus notes on constraints and expected behavior. Sources: Agents v0.x migration guide - Node.js | LiveKit Docs | Agents v0.x migration guide - Python | LiveKit docs | Agents and handoffs | LiveKit docs
a
1. python sdk 2. yes 3. persistent 4. mid-stream
t
You're asking how to persistently update chat context mid-stream in an active Python
AgentSession
using
openai.realtime.RealtimeModel
. Yes — you can. Use
update_chat_ctx()
from your Agent (or AgentSession) to persist changes so they're used by subsequent turns, even while the realtime session is active. Quick example (Python):
Copy code
from livekit.agents import Agent, ChatContext, ChatMessage

class MyAgent(Agent):
    async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
        # Example: add persistent system-level info or RAG result
        rag_content = await my_rag_lookup(new_message.text_content())
        turn_ctx.add_message(role="system", content=f"RAG: {rag_content}")

        # Persist the updated context so future realtime replies use it
        await self.update_chat_ctx(turn_ctx)
Notes and best practices • Enable text modality on the Realtime model (
modalities=["text"]
) to ensure reliable text access and transcription when doing mid-stream updates. • Prefer modifying the provided
turn_ctx
and calling
update_chat_ctx()
rather than mutating global state. • Use lifecycle hooks like
on_user_turn_completed()
to inject context before the agent generates the next reply. • Test mid-stream behavior (concurrent streams) to ensure timing meets your requirements. Would you like a complete runnable example (including session init with
RealtimeModel(modalities=["text"])
) or guidance for handling concurrent updates? Sources: OpenAI Realtime API plugin guide | LiveKit docs | Agents v0.x migration guide - Python | LiveKit docs | Azure OpenAI Realtime API and LiveKit | LiveKit docs | Using the Testing Framework with Realtime Models