Skip to content

Session history duplication after PR #1550 merge #1699

@habema

Description

@habema

I ran into this while merging main into a PR's branch (#1662). The session saving changes from #1550 are causing session history to be duplicated.

The Problem: Session items get saved twice, causing incorrect conversation turn counts and duplicated history.

Root Cause: In the second Runner.run() call, _prepare_input_with_session() creates combined_input = [history + new_user_input], but then _save_result_to_session(session, combined_input, []) re-saves the entire history instead of just the new user input.

Simple repro:

import asyncio

from agents import Agent, Runner
from agents.memory import SQLiteSession
from tests.fake_model import FakeModel
from tests.test_responses import get_text_message


async def main():
    agent = Agent(name="test", model=FakeModel())
    session = SQLiteSession("test")
    agent.model.set_next_output([get_text_message("Response 1")])

    # First run - works fine
    await Runner.run(agent, "Question 1", session=session)
    print(f"After run 1: {len(await session.get_items())} items")  # 2 items ✓

    # Second run - duplicates history
    agent.model.set_next_output([get_text_message("Response 2")])
    await Runner.run(agent, "Question 2", session=session)
    print(f"After run 2: {len(await session.get_items())} items")  # 6 items ❌ (should be 4)

if __name__ == "__main__":
    asyncio.run(main())

Expected: 4 items (2 turns × 2 items each)
Actual: 6 items (history gets re-saved)

The fix should distinguish between original_user_input (new) and combined_input (history + new) when calling _save_result_to_session().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions