From e2b9b2962f14b1e21d070f671908f03f522672e5 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Sat, 17 Oct 2026 19:29:54 +0530 Subject: [PATCH] fix(artifacts): treat session_id=None as the user namespace BaseArtifactService documents session_id=None as the user-scoped namespace, and FileArtifactService behaves that way: an artifact saved without a session is the one sessions reach through the "user:" prefix. InMemoryArtifactService and GcsArtifactService raised InputValidationError for any filename without the prefix instead, so the same call worked against local files and failed in memory and on GCS. Route a session-less filename to the user namespace in both services, as if it carried the "user:" prefix. --- .../adk/artifacts/gcs_artifact_service.py | 18 ++--- .../artifacts/in_memory_artifact_service.py | 18 ++--- .../artifacts/test_artifact_service.py | 74 +++++++++++++++++++ 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/src/google/adk/artifacts/gcs_artifact_service.py b/src/google/adk/artifacts/gcs_artifact_service.py index 86bd77be3b6..8863cd32f2a 100644 --- a/src/google/adk/artifacts/gcs_artifact_service.py +++ b/src/google/adk/artifacts/gcs_artifact_service.py @@ -246,11 +246,11 @@ def _get_blob_prefix( artifact_util.validate_path_segment(user_id, "user_id") if self._file_has_user_namespace(filename): return f"{app_name}/{user_id}/user/{filename}" - if session_id is None: - raise InputValidationError( - "Session ID must be provided for session-scoped artifacts." - ) + # No session means the user-scoped namespace, the one the "user:" + # prefix selects. + return f"{app_name}/{user_id}/user/user:{filename}" + artifact_util.validate_path_segment(session_id, "session_id") return f"{app_name}/{user_id}/{session_id}/{filename}" @@ -289,11 +289,11 @@ def _save_artifact( ) -> int: from google.cloud import exceptions # pylint: disable=g-import-not-at-top - if not self._file_has_user_namespace(filename): - if session_id is None: - raise InputValidationError( - "Session ID must be provided for session-scoped artifacts." - ) + if session_id is None: + # No session means the user-scoped namespace. + if not self._file_has_user_namespace(filename): + filename = f"user:{filename}" + elif not self._file_has_user_namespace(filename): artifact_util._validate_session_id_for_flat_storage(session_id) artifact = ensure_part(artifact) diff --git a/src/google/adk/artifacts/in_memory_artifact_service.py b/src/google/adk/artifacts/in_memory_artifact_service.py index d7e40256d01..6932c910288 100644 --- a/src/google/adk/artifacts/in_memory_artifact_service.py +++ b/src/google/adk/artifacts/in_memory_artifact_service.py @@ -91,11 +91,11 @@ def _artifact_path( artifact_util.validate_path_segment(user_id, "user_id") if self._file_has_user_namespace(filename): return f"{app_name}/{user_id}/user/{filename}" - if session_id is None: - raise InputValidationError( - "Session ID must be provided for session-scoped artifacts." - ) + # No session means the user-scoped namespace, the one the "user:" + # prefix selects. + return f"{app_name}/{user_id}/user/user:{filename}" + artifact_util.validate_path_segment(session_id, "session_id") return f"{app_name}/{user_id}/{session_id}/{filename}" @@ -110,11 +110,11 @@ async def save_artifact( session_id: Optional[str] = None, custom_metadata: Optional[dict[str, Any]] = None, ) -> int: - if not self._file_has_user_namespace(filename): - if session_id is None: - raise InputValidationError( - "Session ID must be provided for session-scoped artifacts." - ) + if session_id is None: + # No session means the user-scoped namespace. + if not self._file_has_user_namespace(filename): + filename = f"user:{filename}" + elif not self._file_has_user_namespace(filename): artifact_util._validate_session_id_for_flat_storage(session_id) artifact = ensure_part(artifact).model_copy(deep=True) path = self._artifact_path(app_name, user_id, filename, session_id) diff --git a/tests/unittests/artifacts/test_artifact_service.py b/tests/unittests/artifacts/test_artifact_service.py index b70527bb0e7..f184eb856cd 100644 --- a/tests/unittests/artifacts/test_artifact_service.py +++ b/tests/unittests/artifacts/test_artifact_service.py @@ -1000,6 +1000,80 @@ async def test_list_keys_preserves_user_prefix( assert sorted(user_only_keys) == ["user:document.pdf", "user:image.png"] +@pytest.mark.asyncio +@pytest.mark.parametrize( + "service_type", + [ + ArtifactServiceType.IN_MEMORY, + ArtifactServiceType.GCS, + ArtifactServiceType.FILE, + ], +) +async def test_no_session_id_addresses_user_namespace( + service_type, artifact_service_factory +): + """session_id=None reaches the user namespace, as the "user:" prefix does.""" + artifact_service = artifact_service_factory(service_type) + app_name = "app0" + user_id = "user0" + + version = await artifact_service.save_artifact( + app_name=app_name, + user_id=user_id, + session_id=None, + filename="profile.txt", + artifact=types.Part.from_bytes(data=b"v0", mime_type="text/plain"), + ) + assert version == 0 + + # A session reads it back through the "user:" prefix... + loaded = await artifact_service.load_artifact( + app_name=app_name, + user_id=user_id, + session_id="123", + filename="user:profile.txt", + ) + assert loaded.inline_data.data == b"v0" + + # ...and saving through the prefix adds a version to the same artifact. + await artifact_service.save_artifact( + app_name=app_name, + user_id=user_id, + session_id="123", + filename="user:profile.txt", + artifact=types.Part.from_bytes(data=b"v1", mime_type="text/plain"), + ) + assert await artifact_service.list_versions( + app_name=app_name, + user_id=user_id, + session_id=None, + filename="profile.txt", + ) == [0, 1] + loaded = await artifact_service.load_artifact( + app_name=app_name, + user_id=user_id, + session_id=None, + filename="profile.txt", + ) + assert loaded.inline_data.data == b"v1" + + await artifact_service.delete_artifact( + app_name=app_name, + user_id=user_id, + session_id=None, + filename="profile.txt", + ) + assert ( + await artifact_service.load_artifact( + app_name=app_name, + user_id=user_id, + session_id="123", + filename="user:profile.txt", + ) + is None + ) + + @pytest.mark.asyncio @pytest.mark.parametrize( "service_type", [ArtifactServiceType.IN_MEMORY, ArtifactServiceType.GCS]