Skip to main content

A project to integrate Google Ads API with MongoDB session management

Project description

ADK MongoDB Session Service

PyPI version Python Package CI

A session management library for the Google ADK framework that uses MongoDB as a backend.

This package provides a MongodbSessionService that implements the google.adk.sessions.base_session_service.BaseSessionService abstract base class. It allows you to store and manage session state in a MongoDB database, fully replicating the three-tiered state management (app, user, and session) found in the core ADK session services.

Features

  • MongoDB Backend: Persist session and state data in a MongoDB database.
  • Tiered State Management: Automatically manage state at the application, user, and session levels.
  • ADK Integration: Implements the BaseSessionService for seamless integration with the Google ADK framework.
  • Async Support: All service methods are asynchronous.

Installation

You can install the package from PyPI:

pip install adk-mongodb-session

For development, clone the repository and install it in editable mode with the testing dependencies:

git clone https://github.com/your-username/adk-mongodb-session.git
cd adk-mongodb-session
pip install -e ".[test]"

Usage

First, instantiate the MongodbSessionService with your MongoDB connection details.

import asyncio
from adk_mongodb_session.mongodb.sessions import MongodbSessionService

# Initialize the service
session_service = MongodbSessionService(
    db_url="mongodb://localhost:27017/",
    database="my_adk_app",
    collection_prefix="sessions"  # This will create sessions_sessions, sessions_app_states, etc.
)

async def main():
    # Use the service to create, retrieve, and manage sessions
    new_session = await session_service.create_session(
        app_name="my_app",
        user_id="user_123",
    )
    print(f"Created session with ID: {new_session.id}")

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

Tiered State Management

The service automatically handles the three-tiered state. When you create a session, you can provide state keys with the prefixes app: and user: to store data at the corresponding level. This state is automatically merged and made available in the session.state attribute.

import asyncio
from google.adk.sessions.state import State

# ... (service initialization)

async def main():
    # Create a session with tiered state
    session = await session_service.create_session(
        app_name="my_app",
        user_id="user_123",
        state={
            f"{State.APP_PREFIX}theme": "dark",
            f"{State.USER_PREFIX}language": "en",
            "session_specific_data": "foo"
        }
    )
    
    # The session object will have a merged view of the state
    print(session.state)
    # Expected output:
    # {
    #     'app:theme': 'dark',
    #     'user:language': 'en',
    *     'session_specific_data': 'foo'
    # }

    # Retrieve the session later
    retrieved_session = await session_service.get_session(
        app_name="my_app",
        user_id="user_id_123",
        session_id=session.id
    )
    print(retrieved_session.state) # Will also contain the merged state

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

Running Tests

To run the tests, first install the testing dependencies:

pip install -e ".[test]"

Then, run the tests using unittest:

python -m unittest discover tests

Contributing

Contributions are welcome! Please see the CONTRIBUTING.md file for details on how to report bugs, suggest enhancements, and submit pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adk_mongodb_session-1.0.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adk_mongodb_session-1.0.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file adk_mongodb_session-1.0.2.tar.gz.

File metadata

  • Download URL: adk_mongodb_session-1.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for adk_mongodb_session-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a6725ffed1e717d9041108fcbda083e97670d0542bf11e65a65ff54284b89bb8
MD5 1ebb6b0a0ae279c92bd6c5410e7c36a0
BLAKE2b-256 98598343ee7a561ba299c9f1472369267e015ad8c7d92aac7f2156b1ba847c0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for adk_mongodb_session-1.0.2.tar.gz:

Publisher: publish-to-pypi.yml on SergeySetti/adk-mongodb-session

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file adk_mongodb_session-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for adk_mongodb_session-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53ab7e735f56e095f71a4dc83e6d58c2abc8efaf4b1e096080a0ae5a332cb844
MD5 0be9f455b1ad72ccd9e0ec00cc52b14c
BLAKE2b-256 1c3080cf8cc2b19cf117e0494134802abcc3b9cc46eb97e679820174d18a16dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for adk_mongodb_session-1.0.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on SergeySetti/adk-mongodb-session

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page