Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 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,20 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This check incorrectly handles empty artifacts. An artifact with empty content (e.g., an empty text file) is valid, but this code would cause load_artifact to return None instead of an empty Part object. Removing this check will allow empty artifacts to be loaded correctly.

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
32 changes: 32 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,35 @@ 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,
],
)
async def test_save_load_text_artifact(service_type, artifact_service_factory):
"""Tests that text artifacts retain .text after round-trip save/load."""
artifact_service = artifact_service_factory(service_type)
artifact = types.Part.from_text(text='{"key": "value"}')

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 == '{"key": "value"}'
assert loaded.inline_data is None
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 test coverage, consider parametrizing this test to include various text contents, especially an empty string. This would help catch edge cases like handling empty artifacts.

You can add another @pytest.mark.parametrize decorator above this function, like so:

@pytest.mark.parametrize(
    "text_content",
    ['{"key": "value"}', "some other text", ""],
)

Then, update the test function to accept and use the text_content parameter.

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