{"record":{"id":"968c6a8065f5b337","repo":"boto/boto3","slug":"value-must-be-of-the-following-types-types","errorCode":null,"errorMessage":"Value must be of the following types: {types}","messagePattern":"Value must be of the following types: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"boto3/dynamodb/types.py","lineNumber":59,"sourceCode":"    traps=[Clamped, Overflow, Inexact, Rounded, Underflow],\n)\n\n\nBINARY_TYPES = (bytearray, bytes)\n\n\nclass Binary:\n    \"\"\"A class for representing Binary in dynamodb\n\n    Especially for Python 2, use this class to explicitly specify\n    binary data for item in DynamoDB. It is essentially a wrapper around\n    binary. Unicode and Python 3 string types are not allowed.\n    \"\"\"\n\n    def __init__(self, value):\n        if not isinstance(value, BINARY_TYPES):\n            types = ', '.join([str(t) for t in BINARY_TYPES])\n            raise TypeError(f'Value must be of the following types: {types}')\n        self.value = value\n\n    def __eq__(self, other):\n        if isinstance(other, Binary):\n            return self.value == other.value\n        return self.value == other\n\n    def __ne__(self, other):\n        return not self.__eq__(other)\n\n    def __repr__(self):\n        return f'Binary({self.value!r})'\n\n    def __str__(self):\n        return self.value\n\n    def __bytes__(self):\n        return self.value","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/boto/boto3/blob/6e10b029c1326a437932a8b24f38af69fd986e15/boto3/dynamodb/types.py#L41-L77","documentation":"boto3.dynamodb.types.Binary is a wrapper that explicitly delimits binary data for DynamoDB. Its constructor checks that value is an instance of BINARY_TYPES (bytearray, bytes, and memoryview on supported versions); otherwise it raises TypeError listing the accepted types. Strings and unicode are intentionally rejected to avoid silent encoding bugs.","triggerScenarios":"Binary('hello'), Binary(b'x'.decode()), Binary(123), or passing a str where bytes are required.","commonSituations":"Porting Python 2 code that used str for binary data; receiving a base64-decoded value as a string; forgetting to encode a string before wrapping.","solutions":["Encode strings to bytes first: Binary(my_str.encode('utf-8')).","Pass bytes/bytearray directly: Binary(b'...').","Validate the source is bytes before constructing Binary."],"exampleFix":"# before\nBinary('hello')\n# after\nBinary(b'hello')\n# or\nBinary('hello'.encode('utf-8'))","handlingStrategy":"type-guard","validationCode":"BINARY_TYPES = (bytes, bytearray, memoryview)\ndef to_binary(v):\n    if isinstance(v, str):\n        v = v.encode('utf-8')\n    assert isinstance(v, BINARY_TYPES), f'Binary requires bytes, got {type(v)}'\n    from boto3.dynamodb.types import Binary\n    return Binary(v)","typeGuard":"def is_binary_value(v) -> bool:\n    return isinstance(v, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    from boto3.dynamodb.types import Binary\n    b = Binary(value)\nexcept TypeError:\n    b = Binary(str(value).encode('utf-8'))","preventionTips":["Encode strings to bytes before constructing Binary.","Validate the source type at the boundary.","Avoid passing Python 2-era str assumptions forward."],"tags":["dynamodb","types","binary","boto3"],"backgroundTag":null,"analyzedSha":"6e10b029c1326a437932a8b24f38af69fd986e15","analyzedAt":"2026-08-11T20:52:47.213Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}