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
16 changes: 10 additions & 6 deletions src/google/adk/artifacts/gcs_artifact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ 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.metadata = {**(blob.metadata or {}), "_adk_is_text": "true"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid magic strings, consider defining "_adk_is_text" as a module-level constant, as it's used in both _save_artifact and _load_artifact.

For example, at the top of the file:

_IS_TEXT_METADATA_KEY = "_adk_is_text"

You can then use this constant here and in _load_artifact.

blob.upload_from_string(
data=artifact.text,
content_type="text/plain",
Expand Down Expand Up @@ -260,15 +261,18 @@ 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(

if blob.metadata and blob.metadata.get("_adk_is_text") == "true":
return types.Part(text=artifact_bytes.decode("utf-8"))

return types.Part.from_bytes(
data=artifact_bytes, mime_type=blob.content_type
)
return artifact

def _list_artifact_keys(
self, app_name: str, user_id: str, session_id: Optional[str]
Expand Down
71 changes: 71 additions & 0 deletions tests/unittests/artifacts/test_artifact_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,74 @@ async def test_file_save_artifact_rejects_absolute_path_within_scope(tmp_path):
filename=str(absolute_in_scope),
artifact=part,
)


@pytest.mark.asyncio
@pytest.mark.parametrize(
"service_type",
[
ArtifactServiceType.IN_MEMORY,
ArtifactServiceType.GCS,
ArtifactServiceType.FILE,
],
)
@pytest.mark.parametrize(
"text_content",
['{"key": "value"}', "some other text"],
)
async def test_save_load_text_artifact(
service_type, artifact_service_factory, text_content
):
"""Tests that text artifacts retain .text after round-trip save/load."""
artifact_service = artifact_service_factory(service_type)
artifact = types.Part.from_text(text=text_content)

await artifact_service.save_artifact(
app_name="app0",
user_id="user0",
session_id="123",
filename="data.json",
artifact=artifact,
)
loaded = await artifact_service.load_artifact(
app_name="app0",
user_id="user0",
session_id="123",
filename="data.json",
)
assert loaded is not None
assert loaded.text == text_content
assert loaded.inline_data is None


@pytest.mark.asyncio
@pytest.mark.parametrize(
"service_type",
[
ArtifactServiceType.GCS,
ArtifactServiceType.FILE,
],
)
async def test_save_load_empty_text_artifact(
service_type, artifact_service_factory
):
"""Tests that empty text artifacts survive round-trip save/load."""
artifact_service = artifact_service_factory(service_type)
artifact = types.Part.from_text(text="")

await artifact_service.save_artifact(
app_name="app0",
user_id="user0",
session_id="123",
filename="empty.txt",
artifact=artifact,
)
loaded = await artifact_service.load_artifact(
app_name="app0",
user_id="user0",
session_id="123",
filename="empty.txt",
)
assert loaded is not None
assert loaded.text == ""
assert loaded.inline_data is None
Loading