Skip to content
Closed
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/google/adk_community/sessions/redis_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@

def _session_serializer(obj: Session) -> bytes:
"""Serialize ADK Session to JSON bytes."""
return orjson.dumps(obj.model_dump(), default=_json_serializer)
data = obj.model_dump()
if "state" in data:
data["state"] = {
k: v for k, v in data["state"].items()
if not k.startswith(State.TEMP_PREFIX)
}
return orjson.dumps(data, default=_json_serializer)


class RedisKeys:
Expand Down
15 changes: 14 additions & 1 deletion tests/unittests/sessions/test_redis_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async def test_session_state_management(self, redis_service):
assert session.state.get("app:key") == "app_value"
assert session.state.get("user:key1") == "user_value"
assert session.state.get("initial_key") == "updated_value"
assert session.state.get("temp:key") is None # Temp state filtered
assert session.state.get("temp:key") == "temp_value" # Ephemeral state kept in memory

pipeline_mock = redis_service.cache.pipeline.return_value
pipe_mock = await pipeline_mock.__aenter__()
Expand All @@ -264,6 +264,19 @@ async def test_session_state_management(self, redis_service):
"user:test_app:test_user", "key1", orjson.dumps("user_value")
)

# Verify temp state was filtered out from the serialized session saved to Redis
set_calls = pipe_mock.set.call_args_list
session_bytes = None
for call in set_calls:
args = call[0]
if args[0].startswith("session:"):
session_bytes = args[1]
break

assert session_bytes is not None
stored_session = orjson.loads(session_bytes)
assert "temp:key" not in stored_session.get("state", {})

@pytest.mark.asyncio
async def test_append_event_with_bytes(self, redis_service):
"""Test appending events with binary content and serialization roundtrip."""
Expand Down
Loading