Skip to content

Pass RunContextWrapper[T] to Session methods #2072

Closed

Description

@MoothyWhite

Feature Request: Pass RunContextWrapper[T] to Session methods

Summary

Right now, Session methods (get_items, add_items, pop_item, etc.) don’t receive the agent’s RunContextWrapper[T].
Passing the wrapper directly would let a custom session manage both conversation history and context together.


Proposed Change

Add wrapper as an optional argument to session methods:

async def get_items(self, limit: int | None = None, wrapper: RunContextWrapper[T] | None = None) -> list[dict]:
    ...

async def add_items(self, items: list[dict], wrapper: RunContextWrapper[T] | None = None) -> None:
    ...

This allows custom sessions to decide how to store or update the wrapper along with messages.


Benefits

  • Gives custom sessions full control over context and history
  • Makes multi-turn, stateful agents easier to implement

Question / Concern

In the non-streaming interface, I noticed that add_items is only called after handoffs are complete.
This means if an agent is switched mid-session, the session can’t save info like the agent’s name or other context until the very end.

Is there a recommended way to handle this, or should add_items be triggered more incrementally to capture agent-specific information during handoff?

Activity

  1. seratch commented on Nov 18, 2025

    @seratch
    Member

    Adding the second parameter makes sense but we need to keep backward-compatibility for all existing sessions store implementations. If we deliver without breaking changes for existing code, this would be a good addition.

  2. added a commit that references this issue on Feb 14, 2026
    f6cf6a5
  3. nileshpatil6 commented on Apr 17, 2026

    @nileshpatil6
    Contributor

    Hi! I'd love to tackle this. Based on seratch's comment about backward-compatibility, I'm planning to add wrapper: RunContextWrapper[T] | None = None as an optional keyword-only argument to get_items, add_items, and pop_item on the Session base class — defaulting to None so all existing session implementations keep working with zero changes. Would a PR in that direction be welcome?

  4. added a commit that references this issue on Jun 21, 2026
    a5a2cd7
  5. fede-kamel commented on Jun 25, 2026

    @fede-kamel

    Following up on the backward-compatibility concern raised above — here's a concrete approach that adds RunContextWrapper access without breaking any existing Session implementation, before I open a PR:

    Mechanism: opt-in via introspection, Session protocol left unchanged.

    • The runtime passes run_context to a session method only if that method's signature accepts it (a run_context parameter, or **kwargs). Existing sessions — including the built-ins and any third-party Session — keep their current signatures and are called exactly as today.

    • Custom sessions opt in simply by adding the parameter:

      async def add_items(
          self, items: list[TResponseInputItem], *, run_context: RunContextWrapper[Any] | None = None
      ) -> None:
          ...
    • The Session Protocol / SessionABC signatures stay as-is, so this introduces no new structural-typing requirement on existing implementations (adding the param to the Protocol itself would break mypy conformance for every current custom session, which is what we want to avoid).

    A couple of decisions I'd like to confirm before implementing:

    1. Parameter name — run_context (holds the RunContextWrapper)? Or do you prefer wrapper as in the original request?
    2. Scope — wire it into all four methods (get_items / add_items / pop_item / clear_session), or start with get_items + add_items where the context is most useful?

    If this approach looks right, I'm happy to put up the PR (with tests covering both an opt-in session and an existing session that ignores the new arg).

  6. sujikathir commented on Jul 27, 2026

    @sujikathir

    I checked current main and found no open PR for this issue. I’d like to take this forward with the compatibility constraint from @seratch as the primary requirement.

    I’ll keep the existing Session protocol and legacy method call shapes working unchanged. Context-aware session implementations will opt in via an optional keyword-only run_context, and the runner will pass it only where the implementation supports it. I’ll cover both streamed and non-streamed runs, plus regression tests proving existing custom sessions still work without modification.

    I’ll keep the change focused on making the current run context available to session operations; incremental persistence timing and new session semantics will remain out of scope.

  7. added this to the 0.20.x milestone on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions