fix: download_file/download_files crash on binary content with UnicodeDecodeError #257
+158
−9
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