Skip to content

Commit 496f8cd

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: output file uploading to artifact service should handle both base64 encoded and raw bytes
PiperOrigin-RevId: 824590321
1 parent f7e2a7a commit 496f8cd

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/google/adk/flows/llm_flows/_code_execution.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ async def _post_process_code_execution_result(
465465
session_id=invocation_context.session.id,
466466
filename=output_file.name,
467467
artifact=types.Part.from_bytes(
468-
data=base64.b64decode(output_file.content),
468+
data=get_content_as_bytes(output_file.content),
469469
mime_type=output_file.mime_type,
470470
),
471471
)
@@ -480,6 +480,25 @@ async def _post_process_code_execution_result(
480480
)
481481

482482

483+
def get_content_as_bytes(output_content: str | bytes) -> bytes:
484+
"""Converts output_content to bytes.
485+
486+
- If output_content is already bytes, it's returned as is.
487+
- If output_content is a string: convert base64-decoded to bytes.
488+
489+
Args:
490+
output_content: The content, which can be a str or bytes.
491+
492+
Returns:
493+
The content as a bytes object.
494+
"""
495+
if isinstance(output_content, bytes):
496+
# Already bytes, no conversion needed.
497+
return output_content
498+
499+
return base64.b64decode(output_content)
500+
501+
483502
def _get_data_file_preprocessing_code(file: File) -> Optional[str]:
484503
"""Returns the code to explore the data file."""
485504

0 commit comments

Comments
 (0)