8000 feat: ✨ redis session class by BloodBoy21 · Pull Request #789 · google/adk-python · GitHub
[go: up one dir, main page]

Skip to content

feat: ✨ redis session class #789

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 36 commits into
base: main
Choose a base branch
from

Conversation

BloodBoy21
Copy link
Contributor

This pull request introduces a Redis-backed session service implementation, along with various enhancements and adjustments to improve consistency, functionality, and maintainability across the codebase. The most significant changes include the addition of the RedisMemorySessionService, updates to the Event and Session classes to support serialization and deserialization, and the inclusion of Redis as a dependency.

New Redis-backed session service:

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

Serialization and deserialization enhancements:

  • Added to_dict and from_dict methods to the Event class to enable serialization and deserialization of event objects. (src/google/adk/events/event.py)
  • Added to_dict and from_dict methods to the Session class for similar functionality, enabling seamless handling of session data in the Redis service. (src/google/adk/sessions/session.py)

Dependency updates:

  • Added redis>=6.0.0 as a new dependency in pyproject.toml to support the Redis-based session service. (pyproject.toml)

Consistency improvements:

  • Updated string literals in the Event and Session classes to use double quotes for consistency. (src/google/adk/events/event.py) [1] [2] (src/google/adk/sessions/session.py) [3]

@BloodBoy21 BloodBoy21 changed the title feat : ✨ redis session class feat: ✨ redis session class May 19, 2025
@seanzhou1023 seanzhou1023 requested a review from DeanChensj May 21, 2025 00:24
@boyangsvl
Copy link
Collaborator

Thanks for the contribution! What's the benefit of redis in-memory session vs the current implementation?

8000

@BloodBoy21
Copy link
Contributor Author

Thanks for the contribution! What's the benefit of redis in-memory session vs the current implementation?

In my job, we develop conversational bots and are now implementing agents. For text-based interactions, we use Google Cloud Run to deploy our backend due to the asynchronous nature of conversations and the stateless nature of Cloud Run. However, with our current implementation, we noticed it can lead to a loop where session context is lost. By using Redis, we’re able to share session context across any number of instances, maintaining continuity and coherence throughout the conversation flow.

return {}
return json.loads(raw.decode())

def _save_sessions(

Choose a reason for hiding this comment

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

Here is my 2cents:
Potential Race Condition on Session Saving

Lines 179–183 (_save_sessions) and usage in 69, 123, 147:
The _save_sessions method overwrites the entire set of sessions for a user+app key in Redis. If multiple requests attempt to create or update sessions for the same user/app at the same time, this can result in lost data due to concurrent updates overwriting each other.
Suggestion: Consider using Redis transactions, locks, or more granular session storage to avoid this risk.

@hangfei
Copy link
Collaborator
hangfei commented May 30, 2025

By using Redis, we’re able to share session context across any number of instances, maintaining continuity and coherence throughout the conversation flow.

Thanks for the contribution.

I don't particularly understand this part: "By using Redis, we’re able to share session context across any number of instances, maintaining continuity and coherence throughout the conversation flow."

Why does a shared memory can avoid the problems you mentioned. Could you help elaborate?

@BloodBoy21
Copy link
Contributor Author

By using Redis, we’re able to share session context across any number of instances, maintaining continuity and coherence throughout the conversation flow.

Thanks for the contribution.

I don't particularly understand this part: "By using Redis, we’re able to share session context across any number of instances, maintaining continuity and coherence throughout the conversation flow."

Why does a shared memory can avoid the problems you mentioned. Could you help elaborate?

Sure
image
In the first scenario, we have multiple instances of our application running on Cloud Run. Since requests can be routed to any instance, the user context can be lost between messages — potentially causing a looping interaction.

By integrating Redis, we can persist session data and share it across all instances. This allows us to maintain the current session state for each user. Additionally, we can use TTL (Time To Live) to automatically remove expired session data and keep the system clean.

This solution helps in the case of Artificial Nerds to scale agent bots and avoid looped conversations caused by missed context.

@whhlct
Copy link
whhlct commented Jun 6, 2025

Any update on the status of this PR?

@boyangsvl
Copy link
Collaborator

Hi, we are preparing a new repo to house community contributions which will give more freedom to the community to add new features that extend ADK's features. Please stay tuned.

@semoal
Copy link
semoal commented Jun 21, 2025

Hi, we are preparing a new repo to house community contributions which will give more freedom to the community to add new features that extend ADK's features. Please stay tuned.

Hi Bo! Are there any timeline available for that? We would love to add Redis session storage in our services.

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.

7 participants
0