Skip to content

Mongodb memory session #1364

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DamyanBG
Copy link

@DamyanBG DamyanBG commented Aug 4, 2025

✨ Add MongoDB-Based Async Session Store

This PR adds an asynchronous session store implementation using MongoDB and the new async API available in pymongo >=4.13. The issue is #1362.

🔧 Summary

  • Implements MongoDBSession, conforming to the existing SessionABC interface.
  • Fully asynchronous using pymongo’s async client (AsyncMongoClient).
  • Schema-less but initializes collections and indexes if they do not already exist.
  • Provides full support for:
    • Session initialization
    • Adding, retrieving, and removing session messages
    • Clearing session data
    • Context-managed resource cleanup via __aenter__ / __aexit__

📦 Dependencies

This implementation relies on:

pymongo>=4.13  # or compatible async version

Users can install this with:

pip install 'openai-agents[pymongo]'

✅ Next Steps

  • I will add unit tests once the overall implementation pattern is validated.
  • I plan to align with the other session store PRs (PostgreSQL, Redis, SQLAlchemy) once they are finalized and merged to ensure consistency in naming, structure, and behavior.

🧠 Notes

  • Indexes are created on session_id for agent_sessions, and on session_id + created_at for agent_messages.
  • A _initialized flag ensures index creation runs only once per session instance.
  • The code gracefully skips invalid JSON entries in the database.
  • I’ve used json.dumps() and json.loads() for storing/retrieving items, matching the format used by other stores.

- Implemented MongoDBSession class for session storage using pymongo.
- Added methods for initializing the database, retrieving, adding, and clearing session items.
- Included async context manager support for MongoDBSession.
- Updated pyproject.toml to include pymongo as a dependency.
- Updated uv.lock to reflect the addition of pymongo and its dependencies.
@seratch seratch added enhancement New feature or request feature:sessions labels Aug 4, 2025
@seratch
Copy link
Member

seratch commented Aug 4, 2025

Thanks for sending this. We cannot tell whether we will have MongoDB support as a built-in one, but let me share a few quick comments:

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

Successfully merging this pull request may close these issues.

2 participants