Skip to content

Gemini Realtime - Order of chats not preserved after agent handoff #4792

@CyberEzpertz

Description

@CyberEzpertz

Bug Description

After a task finishes in a TaskGroup, succeeding AgentTasks have the wrong order of items in its chat_ctx. This currently happens with the gemini-2.5-flash-native-audio-preview-12-2025 model.

In the first agent, the order of chats is like so:

ChatMessage(
    id="GR_ff3bcea3c4ee",
    type="message",
    role="assistant",
    content=["Hey there! I'm Angel. What's your name, and where are you from?"],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={
        "started_speaking_at": 1770838023.9426954,
        "stopped_speaking_at": 1770838027.7167685,
    },
    created_at=1770838023.9426954,
    hash=None,
),
ChatMessage(
    id="GI_2617638e3273",
    type="message",
    role="user",
    content=["Hey, I'm John. I'm from X."],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={},
    created_at=1770838036.8671007,
    hash=None,
),
ChatMessage(
    id="GR_1a2e1c01c207",
    type="message",
    role="assistant",
    content=["Oh, cool! I haven't heard of X. What's it like there?"],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={
        "started_speaking_at": 1770838032.7668004,
        "stopped_speaking_at": 1770838036.8719482,
    },
    created_at=1770838032.7668004,
    hash=None,
),

However, after the agent handoff, the order of chats become switched like so:

ChatMessage(
    id="GR_ff3bcea3c4ee",
    type="message",
    role="assistant",
    content=["Hey there! I'm Angel. What's your name, and where are you from?"],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={
        "started_speaking_at": 1770838023.9426954,
        "stopped_speaking_at": 1770838027.7167685,
    },
    created_at=1770838023.9426954,
    hash=None,
),
ChatMessage(
    id="GR_1a2e1c01c207",
    type="message",
    role="assistant",
    content=["Oh, cool! I haven't heard of X. What's it like there?"],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={
        "started_speaking_at": 1770838032.7668004,
        "stopped_speaking_at": 1770838036.8719482,
    },
    created_at=1770838032.7668004,
    hash=None,
),
ChatMessage(
    id="GI_2617638e3273",
    type="message",
    role="user",
    content=["Hey, I'm John. I'm from X."],
    interrupted=False,
    transcript_confidence=None,
    extra={},
    metrics={},
    created_at=1770838036.8671007,
    hash=None,
),

I believe this happens because of the created_at property being used to sort chat_ctx somewhere after being copied, even though the order of speech in the first agent is correct. Due to this issue, the agent thinks that they haven't responded yet to the previous reply from the user, resulting in them re-acknowledging it.

Expected Behavior

The order of items in chat_ctx should be preserved between agent handoffs.

Reproduction Steps

1. Create an agent with model `gemini-2.5-flash-native-audio-preview-12-2025`
2. Create a TaskGroup within the agent
3. Populate it with at least 2 AgentTasks
4. Log chat_ctx.items on_exit of the first task, and on_enter of the second task

Operating System

Windows 11

Models Used

gemini-2.5-flash-native-audio-preview-12-2025

Package Versions

livekit==1.0.23
livekit-agents=1.3.12

Session/Room/Call IDs

No response

Proposed Solution

Additional Context

No response

Screenshots and Recordings

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions