Skip to main content

A project to integrate Google Ads API with MongoDB session management

Project description

ADK MongoDB Session

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, allowing you to store and manage session state in a MongoDB database.

Installation

To install the package, you can use pip to install it directly from the source:

pip install .

For development, you can install it in editable mode with the testing dependencies:

pip install -e ".[test]"

Usage

First, you need to instantiate the MongodbSessionService with your MongoDB connection details.

from adk-mongodb-session.adk.mongodb.sessions.mongodb_session_service import MongodbSessionService
# Initialize the service
session_service = MongodbSessionService(
    db_url="mongodb://localhost:27017/",
    database="my_adk_app",
    collection_prefix="sessions"
)

Now you can use the service to create, retrieve, list, and delete sessions.

Creating a Session

import asyncio

async def main():
    # Create a new session
    new_session = await session_service.create_session(
        app_name="my_app",
        user_id="user_123",
        state={"initial_key": "initial_value"}
    )
    print(f"Created session with ID: {new_session.id}")

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

Retrieving a Session

import asyncio

async def main():
    # Get a session by ID
    session = await session_service.get_session(
        app_name="my_app",
        user_id="user_123",
        session_id="<your_session_id>"
    )
    if session:
        print(f"Retrieved session state: {session.state}")

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

Tiered State Management

The service automatically handles the three-tiered state (app, user, and session). When you create or update a session, you can provide state keys with the prefixes app: and user: to store data at the corresponding level.

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

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)
    # {
    #     'app:theme': 'dark',
    #     'user:language': 'en',
    #     'session_specific_data': 'foo'
    # }

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

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-0.1.0.tar.gz (6.0 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-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: adk_mongodb_session-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a992357fdf2249341f7cebb0060349bfca87573dac2da2d374634831457ba6e9
MD5 2e421493cdea4c6cf9db8de0c01e701b
BLAKE2b-256 49bcbd79ce98c09645fd84f81f7d0fba23fe79017e4ce50dcfa12921ca751ee3

See more details on using hashes here.

Provenance

The following attestation bundles were made for adk_mongodb_session-0.1.0.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-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for adk_mongodb_session-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e207b2ede7cc1d833e43b606638a0aa0c8be21fc8785407f35a0c59954a05bc
MD5 6fc3888e60e64faf96dbf971fef5f78a
BLAKE2b-256 768aa06f59714127ecf69cc379c75917fd6714a23a7d2983419da0c36bf8f9cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for adk_mongodb_session-0.1.0-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