From 18b944f301ff00149c9b6993b5f616e99cefe1cd Mon Sep 17 00:00:00 2001 From: pragnyanramtha Date: Sat, 16 May 2026 20:55:29 +0000 Subject: [PATCH] fix(artifacts): preserve empty GCS text artifacts --- .../adk/artifacts/gcs_artifact_service.py | 8 ++--- .../artifacts/test_artifact_service.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/google/adk/artifacts/gcs_artifact_service.py b/src/google/adk/artifacts/gcs_artifact_service.py index f8706dedbd..99afd9172e 100644 --- a/src/google/adk/artifacts/gcs_artifact_service.py +++ b/src/google/adk/artifacts/gcs_artifact_service.py @@ -224,7 +224,7 @@ def _save_artifact( data=artifact.inline_data.data, content_type=artifact.inline_data.mime_type, ) - elif artifact.text: + elif artifact.text is not None: blob.upload_from_string( data=artifact.text, content_type="text/plain", @@ -263,11 +263,11 @@ def _load_artifact( blob_name = self._get_blob_name( app_name, user_id, filename, version, session_id ) - blob = self.bucket.blob(blob_name) + blob = self.bucket.get_blob(blob_name) + if not blob: + return None artifact_bytes = blob.download_as_bytes() - if not artifact_bytes: - return None artifact = types.Part.from_bytes( data=artifact_bytes, mime_type=blob.content_type ) diff --git a/tests/unittests/artifacts/test_artifact_service.py b/tests/unittests/artifacts/test_artifact_service.py index 8b82397097..3ff4f8f499 100644 --- a/tests/unittests/artifacts/test_artifact_service.py +++ b/tests/unittests/artifacts/test_artifact_service.py @@ -598,6 +598,35 @@ async def test_get_artifact_version_out_of_index( ) +@pytest.mark.asyncio +async def test_gcs_save_and_load_empty_text_artifact( + artifact_service_factory, +): + """GcsArtifactService should treat empty text as stored content.""" + artifact_service = artifact_service_factory(ArtifactServiceType.GCS) + artifact = types.Part.from_text(text="") + + version = await artifact_service.save_artifact( + app_name="app0", + user_id="user0", + session_id="123", + filename="empty.txt", + artifact=artifact, + ) + + assert version == 0 + loaded_artifact = await artifact_service.load_artifact( + app_name="app0", + user_id="user0", + session_id="123", + filename="empty.txt", + ) + + assert loaded_artifact == types.Part.from_bytes( + data=b"", mime_type="text/plain" + ) + + @pytest.mark.asyncio async def test_file_metadata_camelcase(tmp_path, artifact_service_factory): """Ensures FileArtifactService writes camelCase metadata without newlines."""