Skip to content

Return the SpeechHandle from commit_user_turn or ensure await session.commit_user_turn(...) commits to chat context on resolve #7585

Description

@bml1g12

Feature Type

Would make my life easier

Feature Description

session.generate_reply() returns a SpeechHandle. Awaiting it waits for playout, including later tool steps on that handle. session.commit_user_turn() does not. On livekit-agents 1.8.2 it returns asyncio.Future[str]. That future resolves with the transcript once speech-to-text is finished and end-of-turn detection has been scheduled. It does not wait for the transcript to be in the chat context, for the reply speech to exist, or for the agent to finish speaking.

Firstly, a question to livekit developers - is it intentional design choice that await commit_user_turn() does not wait for the transcript to be in the chat context? This seems like an unintuitive design choice to me and has bit me here.

But then assuming it is intentional. There is a problem for the manual turn control shown in Turn detection & interruptions. The documented end_turn RPC calls commit_user_turn() and returns. The documented start_turn RPC calls session.interrupt() immediately. That fits push-to-talk, where the next button press should cut the agent off. It does not fit the other common manual pattern: the client owns the turn boundary, and the next turn must not start until the agent has finished speaking. An end-turn button is that pattern. If the next end_turn or start_turn is accepted while the reply is still playing, the new turn races the speech that commit_user_turn just scheduled.

generate_reply() already gives you a handle to await before you allow that switch. The reply created by commit_user_turn(skip_reply=False) does not. Please return that SpeechHandle when a reply is scheduled, so await handle means the same thing as awaiting generate_reply().

A non-breaking shape is fine:

result = await session.commit_user_turn(...)
result.transcript      # str, same moment as today's future
result.speech_handle   # SpeechHandle | None

speech_handle should be None when skip_reply=True, and when no reply is scheduled (StopResponse, scheduling failure, or an interrupt before speech is created). It should not hang. When it is set, awaiting it should wait for playout.

Workarounds / Alternatives

Subscribe to speech_created before awaiting commit_user_turn, take the first handle, and await it. The listener has to be registered first, because the commit future resolves before speech_created fires. A timeout is required, because a failure or StopResponse means the event never arrives, and waiting forever blocks the next turn. The listener can also observe an unrelated speech_created.

loop = asyncio.get_running_loop()
speech_ready: asyncio.Future[SpeechHandle] = loop.create_future()

def _on_speech_created(ev: SpeechCreatedEvent) -> None:
    if not speech_ready.done():
        speech_ready.set_result(ev.speech_handle)

session.on("speech_created", _on_speech_created)
try:
    transcript = await session.commit_user_turn(
        transcript_timeout=transcript_timeout,
        stt_flush_duration=stt_flush_duration,
        skip_reply=False,
    )
    try:
        speech = await asyncio.wait_for(speech_ready, timeout=5.0)
    except TimeoutError:
        speech = None
    else:
        await speech  # only then allow the next turn switch
finally:
    session.off("speech_created", _on_speech_created)

commit_user_turn(skip_reply=True) followed by generate_reply() looks like the way to get a handle, and it is the wrong workaround. The commit future resolves before on_end_of_turn appends the transcript, so the follow-up generate_reply() can run on a chat context that does not yet contain the line the user just said. Passing that line as user_input fixes the model request, but skip_reply=True still appends it, so the history gets the line twice. Leaving skip_reply=False is the generation path that puts the transcript in the same task as _generate_reply. That path already emits speech_created with source="generate_reply". There is just no public way to get the handle back.

Polling session.current_speech has the same race and the same timeout problem.

A major problem with the above workaround is we are unable to specify the arguments for the generate_reply (as we cannot call generate_reply directly);

Additional Context

In https://docs.livekit.io/agents/logic/turns/#manual we assume that start_turn interrupts current speech. end_turn commits and does not wait - I assume many users may wish to use turn taking without agent interruptions like us.

The later "Capture the turn transcript" example awaits commit_user_turn(), but that await only waits for the transcript future, not for playout. In many manual turn-taking sessions you need the opposite of start_turn's interrupt where you want to reject or queue the next turn switch until the reply from this commit has finished. generate_reply() supports that via awaiting the returned speech handle. commit_user_turn(skip_reply=False) should too I think, without a second reply call.

I also made a thread to discuss: https://community.livekit.io/t/expected-commit-user-turn-behaviour/2194/3

Activity

  1. added
    enhancement
    on Oct 2, 2026
  2. changed the title [-]Return the `SpeechHandle` from `commit_user_turn`[/-] [+]Return the `SpeechHandle` from `commit_user_turn` or ensure await session.commit_user_turn(...) commits to chat context on resolve[/+] on Oct 5, 2026
  3. icebear02 commented on Oct 9, 2026

    @icebear02

    Hi, I'd like to work on this. I've traced the current path from AgentSession.commit_user_turn() through AudioRecognition._commit_user_turn() to AgentActivity.on_end_of_turn(). The returned transcript future is resolved immediately after scheduling end-of-turn handling, before the async turn-completion task has committed the user message or created the reply SpeechHandle.

    I'll keep the change scoped and add hermetic regression coverage for manual turns, including skip_reply=True and the no-reply/StopResponse path. Before locking the public API shape: would maintainers prefer preserving Future[str] and delaying its resolution until the user message is committed, or returning a result that also exposes the exact SpeechHandle | None created for that committed turn? I can implement either without relying on the global speech_created event.

  4. bml1g12 commented on Oct 9, 2026

    @bml1g12
    ContributorAuthor

    Thanks, I think personally exposing the speech handle is more elegant (more control to developers, which tends to be the spirit of a framework like livekit) but I am not a maintainer.

    I should note that whilst working on workaround for this, we realised a related issue, that if you want to use manual turn taking to commit a user turn, then generate some speech with custom generate_reply() arguments ensuring the last message is in chat context context (which we need to do in some cases, e.g. to provide custom instructions etc.), even if my feature request was addressed that would not be possible without a lot of hackery.

    If we wanted to address the root of the issue, I think it would be to make the code more intuitive such that await commit_user_turn() will actually wait for the user turn to be committed to chat context before proceeding - this would require no changes to public API shape presumably, and solve the root problem. I wonder if that's feasible though? If that was the case, developers could just do the naive

    await commit_user_turn(skip_reply=True)
    await generate_reply(xxx)
  5. self-assigned this
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions