fix: download_file/download_files crash on binary content with UnicodeDecodeError#257
Merged
siwachabhi merged 1 commit intomainfrom Feb 9, 2026
Merged
Conversation
…eDecodeError
download_file() and download_files() crashed with UnicodeDecodeError
when the API returned binary content (PNG, JPEG, PDF) as base64-encoded
blobs, because .decode("utf-8") was called unconditionally on decoded
blob bytes.
Fix uses try/except to decode UTF-8 where possible (preserving str
return for backwards compatibility) and falls back to raw bytes for
true binary content. Return type annotations updated to
Union[str, bytes].
Add tests covering both the binary-returns-bytes and
utf8-blob-returns-str paths for download_file and download_files.
cf8cec3 to
c4fad16
Compare
sundargthb
approved these changes
Feb 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
download_file()anddownload_files()crashed withUnicodeDecodeErrorwhen the API returned binary content (PNG, JPEG, PDF) as base64-encoded blobs.decode("utf-8")was called unconditionally on decoded blob bytesstrreturn for backwards compatibility) and falls back to rawbytesfor true binary contentUnion[str, bytes]Test plan
uv run pytest tests/bedrock_agentcore/tools/test_code_interpreter_client.py -v)test_download_file_binary— PNG header bytes returned asbytestest_download_files_binary— mixed text/binary multi-file downloadtest_download_file_blob_utf8_returns_str— UTF-8-valid blob returned asstrtest_download_files_blob_utf8_returns_str— same for multi-file variantreproduce_bug.pyconfirms bug no longer reproducesvalidate_readfiles_binary.pypasses 10/10