Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/google/adk/sessions/redis_memory_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@

DEFAULT_EXPIRATION = 60 * 60 # 1 hour

def _json_serializer(obj):
"""Fallback serializer to handle non-JSON-compatible types."""
import datetime
import uuid
import math
from decimal import Decimal

if isinstance(obj, set):
return list(obj)
if isinstance(obj, bytes):
try:
return obj.decode("utf-8")
except Exception:
return repr(obj)
if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
if isinstance(obj, uuid.UUID):
return str(obj)
if isinstance(obj, Decimal):
return float(obj)
if isinstance(obj, float):
if math.isnan(obj):
return "NaN"
if math.isinf(obj):
return "Infinity" if obj > 0 else "-Infinity"
return str(obj)


class RedisMemorySessionService(BaseSessionService):
"""A Redis-backed implementation of the session service."""
Expand Down Expand Up @@ -292,5 +319,5 @@ async def _load_sessions(self, app_name: str, user_id: str) -> dict[str, dict]:

async def _save_sessions(self, app_name: str, user_id: str, sessions: dict[str, Any]):
key = f"{State.APP_PREFIX}{app_name}:{user_id}"
await self.cache.set(key, json.dumps(sessions, default=str))
await self.cache.set(key, json.dumps(sessions, default=_json_serializer))
await self.cache.expire(key, self.expire)