Skip to content

Python: Export default history provider source_id ("memory") as a public constant #3941

@pamelafox

Description

@pamelafox

Summary

When the Agent auto-injects an InMemoryHistoryProvider, it uses the hardcoded string "memory" as the source_id:

# _agents.py, line ~1031
self.context_providers.append(InMemoryHistoryProvider("memory"))

Any external code that needs to interact with the default session history (e.g., middleware that compacts/summarizes conversation history) must also hardcode this same string to access session.state["memory"]["messages"]. This creates a fragile coupling — if the framework ever changes this string, external code silently breaks.

Proposal

Export a public constant for the default history provider source ID, e.g.:

# In agent_framework/_sessions.py or agent_framework/__init__.py
DEFAULT_HISTORY_SOURCE_ID = "memory"

Then use it internally:

# _agents.py
self.context_providers.append(InMemoryHistoryProvider(DEFAULT_HISTORY_SOURCE_ID))

And external consumers can reference it reliably:

from agent_framework import DEFAULT_HISTORY_SOURCE_ID

class SummarizationMiddleware(AgentMiddleware):
    def _get_history(self, session: AgentSession) -> list[Message]:
        state = session.state.get(DEFAULT_HISTORY_SOURCE_ID, {})
        return list(state.get("messages", []))

Use case

I'm building an agent-level summarization middleware that monitors cumulative token usage and, when a threshold is crossed, summarizes the conversation history and replaces the stored messages with a compact summary. This requires reading from and writing to session.state["memory"]["messages"] — the same location used by InMemoryHistoryProvider. Having a constant would make this safer and more discoverable.

Alternatives considered

  • Defining the constant in my own code (HISTORY_STATE_KEY = "memory") — works but still relies on an undocumented implementation detail.
  • Using a BaseContextProvider instead of middleware — gives access to state dict via before_run/after_run, but summarization is conceptually a cross-cutting concern better suited to middleware.

Metadata

Metadata

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions