Skip to content

fix(chat_ctx): preserve conversation order in merge() for realtime mo…#4927

Draft
RishbhaJain wants to merge 1 commit intolivekit:mainfrom
RishbhaJain:fix/taskgroup-chat-ctx-ordering
Draft

fix(chat_ctx): preserve conversation order in merge() for realtime mo…#4927
RishbhaJain wants to merge 1 commit intolivekit:mainfrom
RishbhaJain:fix/taskgroup-chat-ctx-ordering

Conversation

@RishbhaJain
Copy link

…dels

ChatContext.merge() was using find_insertion_index() to re-sort items by created_at when merging a completed AgentTask's context back into the TaskGroup. This broke conversation ordering for realtime models (e.g. Gemini native audio) where the assistant message's created_at is set to started_speaking_at -- which can be earlier than the user message's created_at (transcript finalisation time) even though the assistant is responding to that user message.

Fix: replace find_insertion_index() + insert() with append() in merge(), preserving the natural insertion order from other_chat_ctx. This is safe because merge() is only called in one place (AgentTask.await_impl), where other_chat_ctx is always a superset of self and new items are already in correct conversation order from prior append() calls.

Also add two regression tests:

  • test_merge_preserves_conversation_order_over_timestamps: directly replicates the Gemini timing scenario.
  • test_merge_deduplicates_existing_items: confirms deduplication still works correctly after the change.

…dels

ChatContext.merge() was using find_insertion_index() to re-sort items by
created_at when merging a completed AgentTask's context back into the
TaskGroup. This broke conversation ordering for realtime models (e.g.
Gemini native audio) where the assistant message's created_at is set to
started_speaking_at -- which can be earlier than the user message's
created_at (transcript finalisation time) even though the assistant is
responding to that user message.

Fix: replace find_insertion_index() + insert() with append() in merge(),
preserving the natural insertion order from other_chat_ctx. This is safe
because merge() is only called in one place (AgentTask.__await_impl__),
where other_chat_ctx is always a superset of self and new items are
already in correct conversation order from prior append() calls.

Also add two regression tests:
- test_merge_preserves_conversation_order_over_timestamps: directly
  replicates the Gemini timing scenario.
- test_merge_deduplicates_existing_items: confirms deduplication still
  works correctly after the change.
Copilot AI review requested due to automatic review settings February 23, 2026 20:47
@CLAassistant
Copy link

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a conversation ordering bug in ChatContext.merge() that affected realtime models like Gemini native audio. The issue occurred because merge() was sorting items by created_at timestamp using find_insertion_index(), but for realtime models, assistant messages use started_speaking_at as their timestamp, which can be earlier than the user message's transcript finalization time.

Changes:

  • Replace sorted insertion with append() in merge() to preserve natural conversation order
  • Update docstring to document the new ordering behavior
  • Add two regression tests to verify ordering preservation and deduplication

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
livekit-agents/livekit/agents/llm/chat_context.py Modified merge() to append items instead of inserting by timestamp, updated docstring
tests/test_chat_ctx.py Added two new tests: one for conversation order preservation and one for deduplication validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@RishbhaJain
Copy link
Author

Fixes #4792

@RishbhaJain RishbhaJain marked this pull request as draft February 25, 2026 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants