Skip to content

Memory (with sessions) Implementation #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
knowsuchagency opened this issue May 22, 2025 · 2 comments
Open

Memory (with sessions) Implementation #745

knowsuchagency opened this issue May 22, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@knowsuchagency
Copy link

knowsuchagency commented May 22, 2025

Please read this first

  • Have you read the docs?Agents SDK docs
    • yes
  • Have you searched for related issues? Others may have had similar requests
    • yes

Describe the feature

I've noticed it greatly improves developer experience to have an interface with a default implementation (or two) to handle conversation memory.

The docs suggest the following to handle memory:
async def main():
    agent = Agent(name="Assistant", instructions="Reply very concisely.")

    with trace(workflow_name="Conversation", group_id=thread_id):
        # First turn
        result = await Runner.run(agent, "What city is the Golden Gate Bridge in?")
        print(result.final_output)
        # San Francisco

        # Second turn
        new_input = result.to_input_list() + [{"role": "user", "content": "What state is it in?"}]
        result = await Runner.run(agent, new_input)
        print(result.final_output)
        # California

It's currently left to the user to explicitly manage session memory using result.to_input_list()

Alternatives

A good example of what this could look like is Google's ADK.

Another such example from my own AI abstraction library, Promptic

@knowsuchagency knowsuchagency added the enhancement New feature or request label May 22, 2025
@rm-openai
Copy link
Collaborator

Yeah fair feedback. Would love to discuss the interface in this issue and would even welcome a contribution once we are happy with it!

@knowsuchagency
Copy link
Author

knowsuchagency commented May 24, 2025

Cool! I went ahead and submitted #752 after some experimentation. I'm more than happy to workshop the API -- I just found coding and documentation to be the best way to shape my thinking.

Usage

from agents import Agent, Runner, RunConfig, SQLiteSessionMemory

agent = Agent(name="Assistant", instructions="Reply concisely.")
memory = SQLiteSessionMemory()
config = RunConfig(memory=memory, session_id="conversation_123")

await Runner.run(agent, "Hi, I'm planning a trip to Japan", run_config=config)
await Runner.run(agent, "What's the best time to visit?", run_config=config)
await Runner.run(agent, "How about cherry blossom season?", run_config=config)

SessionMemory Interface

The basic idea is to have an interface (typing.Protocol) that describes how to manage conversation memory. That would be passed to RunConfig along with a session_id. I included a SQLiteSessionMemory implementation since that won't introduce any new dependencies. New backends could then be defined and used in the following way:

class CustomSessionMemory:
    async def get_messages(self, session_id: str) -> List[dict]: ...
    async def add_messages(self, session_id: str, messages: List[dict]) -> None: ...
    async def clear_session(self, session_id: str) -> None: ...

config = RunConfig(memory=CustomSessionMemory(), session_id="session_123")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants