From 2ab8af339ae5abaeace6a3babd18beb1abb3469e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjell=20Reidar=20J=C3=B8ssang?= Date: Mon, 25 May 2026 23:54:20 +0200 Subject: [PATCH] fix: exclude temp: state keys from Firestore session writes FirestoreSessionService.append_event() writes the full session.state dict to Firestore, filtering app: and user: prefixed keys but not temp: prefixed keys. This causes crashes when temp state contains non-serializable objects (e.g. GroundingMetadata from GoogleSearchTool via AgentTool's propagate_grounding_metadata). The DatabaseSessionService and SqliteSessionService don't have this issue because they only persist state_deltas extracted via extract_state_delta(), which already filters temp: keys. Fix: add State.TEMP_PREFIX to the exclusion filter in session_only_state. Test: extend existing test_append_event_with_temp_state to assert temp keys are absent from the persisted session state dict. --- .../integrations/firestore/firestore_session_service.py | 1 + .../firestore/test_firestore_session_service.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/google/adk/integrations/firestore/firestore_session_service.py b/src/google/adk/integrations/firestore/firestore_session_service.py index 83b97c33c2..e90753be5c 100644 --- a/src/google/adk/integrations/firestore/firestore_session_service.py +++ b/src/google/adk/integrations/firestore/firestore_session_service.py @@ -551,6 +551,7 @@ async def _append_txn(transaction: firestore.AsyncTransaction) -> int: for k, v in session.state.items() if not k.startswith(State.APP_PREFIX) and not k.startswith(State.USER_PREFIX) + and not k.startswith(State.TEMP_PREFIX) } transaction.update( session_ref, diff --git a/tests/unittests/integrations/firestore/test_firestore_session_service.py b/tests/unittests/integrations/firestore/test_firestore_session_service.py index 1445bfe0ef..0a275c2cb8 100644 --- a/tests/unittests/integrations/firestore/test_firestore_session_service.py +++ b/tests/unittests/integrations/firestore/test_firestore_session_service.py @@ -386,6 +386,15 @@ async def test_append_event_with_temp_state(mock_firestore_client): assert "temp:k1" not in event_data["actions"]["state_delta"] assert event_data["actions"]["state_delta"]["session_key"] == "session_val" + # 3. Verify temp keys are NOT written to session state in Firestore + transaction.update.assert_called_once() + update_args, _ = transaction.update.call_args + persisted_state = update_args[1]["state"] + assert "temp:k1" not in persisted_state, ( + "temp: keys must not be persisted to Firestore session state" + ) + assert "session_key" in persisted_state + @pytest.mark.asyncio async def test_list_sessions_with_user_id(mock_firestore_client):