Skip to content

Conversation

@yvesemmanuel
Copy link
Contributor

@yvesemmanuel yvesemmanuel commented Sep 13, 2025

Redis Session Service Implementation

This pull request addresses the issue described in #5.

This PR migrates the Redis-backed session service implementation proposed in google/adk-python#789. I've excluded the changes to the Event and Session classes, opting to use Pydantic's default serialization and deserialization methods instead. While credit goes to the original pull request from the main repository, I have completely refactored the implementation to align with this community repository's standards.

What's Changed

New Redis-backed session service:

  • Added a new RedisMemorySessionService class that provides a Redis-backed implementation of session management, including methods for creating, retrieving, listing, and deleting sessions, as well as managing session state and events (src/google/adk_community/sessions/redis_memory_session_service.py)
  • Updated __init__.py in the sessions module to include RedisMemorySessionService in the exports (src/google/adk_community/sessions/__init__.py)

FastAPI CLI integration:

  • Added FastAPI CLI module to support Redis session service integration (src/google/adk_community/cli/fast_api.py)
  • I needed to move the browser/ dependencies dir for the dev-ui/ endpoint web rendering.

Dependency updates:

  • Added redis>=5.0.0, <6.0.0 as a new dependency in pyproject.toml to support the Redis-based session service
  • Added development dependencies for testing: pytest>=8.2.2 and pytest-asyncio>=0.23.7

Project infrastructure:

  • Added comprehensive .gitignore file for Python projects with IDE, testing, and build artifacts exclusions
  • Added unit tests for the Redis session service (tests/unittests/sessions/test_redis_session_service.py)

Testing

Test Setup and Dependencies

The implementation has been thoroughly tested using the academic-research sample from https://github.com/google/adk-samples/tree/main/python/agents/academic-research.

Installation:

pip install git+https://github.com/yvesemmanuel/adk-python-community.git@feature/redis-session-service

Test Implementation

The following FastAPI implementation demonstrates the Redis database session service integration:

import os
import uvicorn
from fastapi import FastAPI
from google.adk_community.cli.fast_api import get_fast_api_app

AGENT_DIR = os.path.dirname(os.path.abspath(__file__)) + "/agents"
SESSION_SERVICE_URI = "redis://127.0.0.1:6379/0"
ALLOWED_ORIGINS = ["http://localhost", "http://localhost:8080", "*"]
SERVE_WEB_INTERFACE = True
ARTIFACT_SERVICE_URI = ""

app: FastAPI = get_fast_api_app(
    agents_dir=AGENT_DIR,
    session_service_uri=SESSION_SERVICE_URI,
    session_db_kwargs=dict(expire=60 * 60 * 1), # 1 hour of Time To Live
    artifact_service_uri=ARTIFACT_SERVICE_URI,
    allow_origins=ALLOWED_ORIGINS,
    web=SERVE_WEB_INTERFACE,
)

@app.get("/health")
async def get_health():
    return {"status": "ok"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Test Results and Verification

The testing process validates both the web interface functionality and Redis data persistence:

Web Interface Testing

The web UI successfully demonstrates conversation functionality with the academic research agent, confirming that:

  • User interactions are properly handled through the FastAPI interface
Screenshot 2025-09-13 at 12 54 34

Redis Data Persistence Verification

Redis Insight shows the successfully written session data and conversation history, confirming that:

  • Session data is correctly serialized and stored in Redis
  • Conversation history is properly maintained across requests
  • Data structure aligns with expected Redis key-value patterns
Screenshot 2025-09-13 at 12 54 55

@google-cla
Copy link

google-cla bot commented Sep 13, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @yvesemmanuel, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the ADK community extensions by introducing a robust Redis-backed session service. This new service provides a scalable and persistent solution for managing user sessions and their associated states, moving away from in-memory solutions for more demanding applications. The changes include core service implementation, integration with the existing FastAPI CLI, necessary dependency updates, and thorough unit testing to ensure reliability.

Highlights

  • New Redis-backed Session Service: Introduced RedisMemorySessionService for persistent session management, offering methods for creating, retrieving, listing, and deleting sessions, as well as managing session state and events.
  • FastAPI CLI Integration: Integrated the new Redis session service into the FastAPI CLI module, allowing redis:// URIs for session service configuration.
  • Dependency Updates: Added redis>=6.0.0 as a core dependency and pytest>=8.4.2 and pytest-asyncio>=1.2.0 as development dependencies for testing.
  • Project Infrastructure: Added a comprehensive .gitignore file for Python projects and included unit tests for the new Redis session service.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Redis-backed session service, which is a valuable addition for enabling persistent session management in the ADK. The implementation is generally solid, with good use of async Redis operations and a comprehensive set of unit tests. However, I've identified a few critical issues with dependency versions that will prevent the package from being installed or the development environment from being set up. I also have a significant concern regarding the scalability of the current Redis storage strategy, which could lead to performance problems in production. Additionally, there are several opportunities to improve code clarity, efficiency, and test coverage. My detailed comments are provided below.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch 3 times, most recently from 39c5588 to 1c83953 Compare September 13, 2025 04:22
@yvesemmanuel
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable Redis-backed session service. The overall structure is sound, leveraging redis-py for asynchronous operations and providing a clear API. However, I've identified several critical issues related to performance and correctness in the session management logic. The current implementation inefficiently loads and saves all of a user's sessions for operations that should only affect a single session, which can lead to significant performance degradation and race conditions under concurrent use. I have provided specific, refactored code suggestions for each affected method to resolve these issues by using atomic, single-key Redis operations. Additionally, there is a critical bug in src/google/adk_community/__init__.py that will prevent the package from being imported, and some of the new unit tests use incorrect mocking, which I've also flagged for correction. Addressing these points will make the new service robust, performant, and reliable.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch 3 times, most recently from 0829e70 to d0f89f7 Compare September 13, 2025 14:12
@yvesemmanuel
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great addition, introducing a Redis-backed session service for more robust session management. The implementation is solid, effectively using Redis features like pipelines and expiration. The tests are comprehensive and cover the new functionality well. I've identified a few areas for improvement related to code quality, performance, and a potential bug in a utility function. My detailed comments are provided below.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from d0f89f7 to 9ccb00d Compare September 13, 2025 14:28
@yvesemmanuel
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Redis-backed session service, which is a great addition for more persistent session management. The implementation is solid and includes comprehensive unit tests. I've found a critical issue regarding argument handling in the service's constructor that would cause a crash, and a high-severity issue with the use of asyncio.run() in the synchronous helper methods. I've also included a couple of medium-severity suggestions for code quality and performance improvement. Overall, great work on this feature.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 9ccb00d to 96f69fb Compare September 13, 2025 14:39
@yvesemmanuel
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Redis-backed session service, which is a valuable addition for enabling persistent sessions in ADK applications. The implementation is well-structured, using Redis pipelines for efficiency and including a good set of unit tests. I've identified a couple of areas for improvement related to data consistency with key expirations and the cleanup of stale session IDs. My review comments provide specific suggestions to enhance the robustness of the new service. Overall, this is a solid contribution.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 96f69fb to 13f1b14 Compare September 13, 2025 14:46
@yvesemmanuel
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Redis-backed session service, which is a valuable addition for enabling scalable session management. The implementation is solid, effectively using Redis pipelines for efficiency and atomicity. The code is well-structured, and the inclusion of unit tests is commendable. My review provides feedback on a few areas for improvement, focusing on code clarity, best practices, and test correctness to further enhance the quality of this new feature.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch 4 times, most recently from b892d9e to 0c51c1e Compare September 13, 2025 16:11
@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 0c51c1e to 2370467 Compare September 16, 2025 16:17
@boyangsvl boyangsvl requested a review from hangfei September 16, 2025 18:28
@boyangsvl
Copy link
Collaborator

@hangfei could you help review? I wonder which folder these files should live. We probably shouldn't have 'google' in the path.

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 2370467 to 9c03cf2 Compare September 16, 2025 23:11
@danielefranceschi
Copy link

What is the right way to pass authentication parameters to the RedisMemorySessionService constructor?

I've tried uri="redis://mypassword@myhost:6379/" and all the possible combinations with kwargs following the redis documentation, but nothing.

@yvesemmanuel
Copy link
Contributor Author

redis://127.0.0.1:6379/0

Here I'm following this pattern for the URI: redis[s]://[[username][:password]@][host][:port][/db-number].

@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 9c03cf2 to 14ac183 Compare September 19, 2025 00:53
@yvesemmanuel yvesemmanuel force-pushed the feature/redis-session-service branch from 14ac183 to 9804815 Compare September 19, 2025 00:55
@hangfei
Copy link
Collaborator

hangfei commented Sep 24, 2025

Removed JS code for now. Overall LGTM.

"""A Redis-backed implementation of the session service."""

def __init__(
self,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use keyword arguments to be more clear?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems other session impls also not using it.

any reason we are not using it there?

@hangfei hangfei merged commit 8c69144 into google:main Sep 24, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants