Expected commit_user_turn() behaviour

Would be interested in livekit dev thoughts on Return the `SpeechHandle` from `commit_user_turn` or ensure await session.commit_user_turn(...) commits to chat context on resolve · Issue #7585 · livekit/agents · GitHub

It is an observation I have that after await commit_user_turn(skip_reply=True), the user message is added only after one or more of these waits:

  • the end-of-turn task gets a turn on the event loop and finishes any endpointing delay;
  • the previous turn’s task finishes;
  • background speeches are interrupted.

A generate_reply() called right after the await can therefore read the chat context before the message is in it - I am curious if it’s intended behaviour? As it means we need to work carefully around this with custom boilerplate.

@Benjamin_Lowe, Your read is accurate. commit_user_turn() returns an asyncio.Future[str] that resolves at fut.set_result(transcript), which runs after end-of-turn detection is scheduled, not after the message lands (audio_recognition.py#L1107). The user message is written later by chat_ctx.add_message(role="user", ...) inside the end-of-turn task, when the user_turn span ends (#L1525, noted at #L1159). So the future is not a commit barrier; a generate_reply() right after the await can race the write, exactly as you saw.

Whether that should be the contract is the open question in your #7585 (enhancement, no PR yet), so that intent call belongs to the team on the issue. The structural reason today is that the commit lives in the end-of-turn pipeline, not in the future resolution.

For now the reliable signal that the turn is in context is the conversation_item_added event (events.py#L295), not the returned future. Drive the reply from that instead of sequencing after the await:

@session.on("conversation_item_added")
def _on_item(ev):
    if getattr(ev.item, "role", None) == "user":
        session.generate_reply()   # chat context now holds the turn

@Muhammad_Usman_Bashir thanks for your reply, and for the pointers.

We wait on conversation_item_added too but the part I cannot use as written is treating the first user item as “this commit”.

As far as I understand, LiveKit appends a turn’s user message only after the previous turn’s on_user_turn_completed task finishes. If I register the listener and then await commit_user_turn(skip_reply=True), an earlier turn can still emit conversation_item_added before “this commit’s message” exists. Calling generate_reply() from that event reads a chat context that does not yet contain the transcript the future just returned, which is is the stale reply bug we are trying to avoid.

Matching the event text against the returned transcript does not close it either. The future resolves with a snapshot taken before the endpointing delay, and a late STT final can extend the line that actually gets appended, so the chat text can be longer than the string the future returned. A prefix check covers that, and then fails in another way in that a short opening speech can very well be a prefix of an earlier turn’s line, so the wait ends on the wrong message.

The future only returns str. The ChatMessage, including its id, is created later in _user_turn_completed_impl, so there is nothing on the event that ties it to this commit_user_turn call. The chat_ctx.add_message in _run_eou_detection is a copy for turn detection. The session append, and the event, happen in that later task, which awaits the previous turn’s task first. That wait is why the first user event is not necessarily this commit.

If the contract stays as it is, the missing piece is a public way to know that this call’s item has been added. Either the future resolving after the append, or the event carrying something that identifies this commit.

@Benjamin_Lowe, Confirmed from the source: on the skip_reply=True path, _user_turn_completed_impl appends the message and emits conversation_item_added, then returns before on_user_turn_completed (agent_activity.py#L2762-L2774). So that hook never fires for a skipped-reply commit, and the append itself runs only after the previous turn’s task (#L2730). The event carries the ChatMessage with its id, but nothing public ties that id to your specific commit_user_turn() call. Your reading holds, and #7585 is the right place for the fix: the future resolving after the append, or a correlation id.

The one correlated handle today is on the non-skip path. With skip_reply=False, on_user_turn_completed(turn_ctx, new_message) fires with this turn’s committed ChatMessage, in order, before the reply (#L2816). If your between-commit-and-reply logic can live there, you get the message with no text matching and no cross-turn ambiguity. Raise StopResponse to suppress the reply, or shape it:

class MyAgent(Agent):
    async def on_user_turn_completed(self, turn_ctx, new_message):
        # new_message is THIS turn's committed message, id included
        if not ready_to_reply(new_message):
            raise StopResponse()          # no reply this turn
        # else fall through: the normal reply runs against the committed turn

If you genuinely need the external commit-then-reply split rather than gating inside the hook, there is no public correlation today, and #7585 is the gap to close.