Skip to content

fix: Attempting to serialize bytes type causes error (#5660)#5681

Closed
aviruthen wants to merge 2 commits intoaws:masterfrom
aviruthen:fix/attempting-to-serialize-bytes-type-causes-error-5660
Closed

fix: Attempting to serialize bytes type causes error (#5660)#5681
aviruthen wants to merge 2 commits intoaws:masterfrom
aviruthen:fix/attempting-to-serialize-bytes-type-causes-error-5660

Conversation

@aviruthen
Copy link
Copy Markdown
Collaborator

Description

The serialize() function in sagemaker-core/src/sagemaker/core/utils/utils.py fails when encountering bytes objects because bytes is not included in the is_not_primitive() check. When serialize({'body': b'1'}) is called, the bytes value passes through to _serialize_shape(), which calls vars() on the bytes object, causing a TypeError. The fix is to add bytes to the primitive type tuple in is_not_primitive() and is_primitive_class() so that bytes values are returned as-is during serialization.

Related Issue

Fixes #5660

Changes Made

  • sagemaker-core/src/sagemaker/core/utils/utils.py
  • sagemaker-core/tests/unit/generated/test_utils.py

AI-Generated PR

This PR was automatically generated by the PySDK Issue Agent.

  • Confidence score: 85%
  • Classification: bug
  • SDK version target: V3

Merge Checklist

  • Changes are backward compatible
  • Commit message follows prefix: description format
  • Unit tests added/updated
  • Integration tests added (if applicable)
  • Documentation updated (if applicable)

Copy link
Copy Markdown
Member

@mufaddal-rohawala mufaddal-rohawala left a comment

Choose a reason for hiding this comment

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

🤖 AI Code Review

The fix correctly adds bytes to the primitive type checks to prevent TypeError when serializing bytes objects. However, there are indentation issues in the modified lines that will likely cause linting failures, and there's a minor extra blank line in the test file.


def is_not_primitive(obj):
return not isinstance(obj, (int, float, str, bool, datetime.datetime))
return not isinstance(obj, (int, float, str, bool, bytes, datetime.datetime))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bug: This line has 8 spaces of indentation (double-indented) instead of the original 4 spaces. This will cause an IndentationError since the function body was previously at 4-space indentation. Please fix:

    return not isinstance(obj, (int, float, str, bool, bytes, datetime.datetime))


def is_primitive_class(cls):
return cls in (str, int, bool, float, datetime.datetime)
return cls in (str, int, bool, float, bytes, datetime.datetime)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bug: Same indentation issue here — 8 spaces instead of 4. This will cause an IndentationError. Please fix:

    return cls in (str, int, bool, float, bytes, datetime.datetime)

@@ -373,6 +373,25 @@ def test_serialize_method_nested_shape():
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: There's an extra blank line here. PEP 8 expects two blank lines between top-level definitions, not three. This is minor but worth cleaning up.

@aviruthen aviruthen closed this Mar 26, 2026
@aviruthen aviruthen deleted the fix/attempting-to-serialize-bytes-type-causes-error-5660 branch March 26, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attempting to serialize bytes type causes error

2 participants