-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(artifacts): Preserve .text on GcsArtifactService load (#3157) #4541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"} | ||
| blob.upload_from_string( | ||
| data=artifact.text, | ||
| content_type="text/plain", | ||
|
|
@@ -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 | ||
|
||
| 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] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve maintainability and avoid magic strings, consider defining
"_adk_is_text"as a module-level constant, as it's used in both_save_artifactand_load_artifact.For example, at the top of the file:
You can then use this constant here and in
_load_artifact.