Skip to content

Commit a5db346

Browse files
Tinnitussenboyangsvl
authored andcommitted
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. Merge #5841 Change-Id: I3278c205bba7c7e287308c2625112789593bb7ef
1 parent 03ef3f6 commit a5db346

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/google/adk/integrations/firestore/firestore_session_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ async def _append_txn(transaction: firestore.AsyncTransaction) -> int:
551551
for k, v in session.state.items()
552552
if not k.startswith(State.APP_PREFIX)
553553
and not k.startswith(State.USER_PREFIX)
554+
and not k.startswith(State.TEMP_PREFIX)
554555
}
555556
transaction.update(
556557
session_ref,

tests/unittests/integrations/firestore/test_firestore_session_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,15 @@ async def test_append_event_with_temp_state(mock_firestore_client):
386386
assert "temp:k1" not in event_data["actions"]["state_delta"]
387387
assert event_data["actions"]["state_delta"]["session_key"] == "session_val"
388388

389+
# 3. Verify temp keys are NOT written to session state in Firestore
390+
transaction.update.assert_called_once()
391+
update_args, _ = transaction.update.call_args
392+
persisted_state = update_args[1]["state"]
393+
assert (
394+
"temp:k1" not in persisted_state
395+
), "temp: keys must not be persisted to Firestore session state"
396+
assert "session_key" in persisted_state
397+
389398

390399
@pytest.mark.asyncio
391400
async def test_list_sessions_with_user_id(mock_firestore_client):

0 commit comments

Comments
 (0)