Skip to content

Python: [Bug]: Colliding Ids are assigned to message history creating issues when using compaction #5237

Description

Description

_ensure_message_ids(messages) in agent_framework/_compaction.py fills each missing Message.message_id with f"msg_{index}", where index is the position in the list it receives. annotate_message_groups calls it indirectly via group_messages(messages[start_index:]) on a slice of the full list — so successive incremental calls collide with ids already assigned by earlier calls. The framework ends up with multiple distinct messages sharing the same message_id.

In the sample provided this was observed:

  0: role=user      id='msg_0'
  1: role=assistant id='msg_1'
  2: role=user      id='msg_1'  
  3: role=assistant id='msg_2'
  4: role=user      id='msg_1'
  5: role=assistant id='msg_2'
  6: role=user      id='msg_1'
  7: role=assistant id='msg_2'

Downstream consumers assume message_id is unique. The most visible failure today is in SummarizationStrategy: _group_id_for derives group_id from message_id, so colliding ids collapse distinct groups into one.

Code Sample

import asyncio
import os

from agent_framework import (
    Agent,
    CompactionProvider,
    InMemoryHistoryProvider,
    SlidingWindowStrategy,
)
from agent_framework.openai import OpenAIChatCompletionClient


async def main() -> None:
    agent = Agent(
        client=OpenAIChatCompletionClient(
            model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"]
        ),
        name="Repro",
        instructions="Reply in <= 4 words.",
        context_providers=[
            InMemoryHistoryProvider(),
            CompactionProvider(
                after_strategy=SlidingWindowStrategy(keep_last_groups=1000),
            ),
        ],
    )
    session = agent.create_session(session_id="repro")

    for text in ["Say one.", "Say two.", "Say three.", "Say four."]:
        await agent.run(text, session=session)

    for i, m in enumerate(session.state["in_memory"]["messages"]):
        print(f"  {i}: role={m.role:9} id={m.message_id!r}")

Error Messages / Stack Traces

Package Versions

agent-core-framework: 1.0.1

Python Version

Python 3.13

Additional Context

As a workaround I have been adding unique ids to messages outside of the agent loop

Metadata

Metadata

Labels

agentsUsage: [Issues, PRs], Target: Single agentbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)pythonUsage: [Issues, PRs], Target: Python

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions