3131from .session import Session
3232from .state import State
3333
34+ import base64
35+
3436logger = logging .getLogger ('google_adk.' + __name__ )
3537
3638DEFAULT_EXPIRATION = 60 * 60 # 1 hour
@@ -46,7 +48,7 @@ def _json_serializer(obj):
4648 return list (obj )
4749 if isinstance (obj , bytes ):
4850 try :
49- return obj .decode ("utf-8 " )
51+ return base64 . b64encode ( obj ) .decode ("ascii " )
5052 except Exception :
5153 return repr (obj )
5254 if isinstance (obj , (datetime .datetime , datetime .date )):
@@ -62,6 +64,19 @@ def _json_serializer(obj):
6264 return "Infinity" if obj > 0 else "-Infinity"
6365 return str (obj )
6466
67+ def _restore_bytes (obj ):
68+ if isinstance (obj , dict ):
69+ return {k : _restore_bytes (v ) for k , v in obj .items ()}
70+ elif isinstance (obj , list ):
71+ return [_restore_bytes (v ) for v in obj ]
72+ elif isinstance (obj , str ):
73+ try :
74+ # intenta decodificar base64
75+ data = base64 .b64decode (obj , validate = True )
76+ return data
77+ except Exception :
78+ return obj
79+ return obj
6580
6681class RedisMemorySessionService (BaseSessionService ):
6782 """A Redis-backed implementation of the session service."""
@@ -315,7 +330,8 @@ async def _load_sessions(self, app_name: str, user_id: str) -> dict[str, dict]:
315330 raw = await self .cache .get (key )
316331 if not raw :
317332 return {}
318- return json .loads (raw .decode ())
333+ raw_data = json .loads (raw .decode ())
334+ return raw_data
319335
320336 async def _save_sessions (self , app_name : str , user_id : str , sessions : dict [str , Any ]):
321337 key = f"{ State .APP_PREFIX } { app_name } :{ user_id } "
0 commit comments