agents icon indicating copy to clipboard operation
agents copied to clipboard

Realtime generate_reply drops the extra of a ChatMessage user_input

Open Darshak03 opened this issue 1 week ago • 1 comments

Bug Description

AgentSession.generate_reply(user_input=...) accepts an llm.ChatMessage (#4808). With an LLM, the message is stored as passed. With a RealtimeModel, only its text is used: AgentActivity._generate_reply passes user_message.raw_text_content to _realtime_reply_task, which builds a new message from that string.

So the extra the caller set never reaches agent.chat_ctx or the conversation_item_added event. The code still carries the note TODO(theomonnom): support llm.ChatMessage for the realtime model from #1943.

This blocks a caller that tags a message (for example extra={"source": "app"}) and decides what to do in conversation_item_added: the tag arrives with an LLM and is missing with a RealtimeModel.

Expected Behavior

As with an LLM, the user message stored in agent.chat_ctx and emitted in conversation_item_added keeps the caller's extra. The id, text-only content and everything sent to the provider can stay as they are today.

Reproduction Steps

from livekit.agents import AgentSession, llm

# session = AgentSession(llm=<any RealtimeModel>), started with an Agent

def on_item(ev):
    if isinstance(ev.item, llm.ChatMessage) and ev.item.role == "user":
        print(ev.item.extra)

session.on("conversation_item_added", on_item)

msg = llm.ChatMessage(role="user", content=["hello"], extra={"source": "app"})
session.generate_reply(user_input=msg)

# realtime model: prints {}
# LLM:            prints {'source': 'app'}

Operating System

macOS 27.2

Models Used

Any RealtimeModel (reproduced with the test suite's fake realtime model; the path is shared by every realtime plugin)

Package Versions

livekit-agents==1.8.13 (main at d65958017)
Python 3.14

Proposed Solution

# In _generate_reply, copy the message's extra where its text is already read,
# pass it to _realtime_reply_task, and add it to the add_message(role="user") call
# that builds the turn. The id, content and provider payload stay as they are.

Fix with tests: #8783

Additional Context

No realtime path sends a message's extra to the provider, so only the local turn and the event need it. Keeping the caller's id is left out: every realtime plugin sends the item id to its provider, and the id rules differ by provider.

Darshak03 avatar Nov 25 '26 14:11 Darshak03

@longcw @theomonnom could you take a look at this issue and review the fix in #8783 when you have a moment?

The change is small (+5/−1 in agent_activity.py, plus regression tests): with a RealtimeModel, generate_reply(user_input=ChatMessage) now keeps the message's extra on the user turn, so it reaches agent.chat_ctx and conversation_item_added, as it already does with an LLM. It covers the extra part of the TODO(theomonnom) in _generate_reply; the provider still receives the same text-only item with a fresh id, and no other field of the turn changes. Devin's review findings have been addressed, and its one remaining flag (values nested inside extra stay shared with the caller's message) is explained in the PR description.

Thank you, your feedback would be very helpful.

Darshak03 avatar Nov 25 '26 16:11 Darshak03