Skip to content
Open
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
18 changes: 9 additions & 9 deletions src/google/adk/artifacts/gcs_artifact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions src/google/adk/artifacts/in_memory_artifact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions tests/unittests/artifacts/test_artifact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading