{"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":"validation","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/c7b4afac237b976d48395d7523eaf7cec3a450b3/boto3/dynamodb/types.py#L41-L77","documentation":"Raised by Binary.__init__ in boto3/dynamodb/types.py when the value passed to Binary(value) is not an instance of BINARY_TYPES = (bytearray, bytes). The Binary wrapper exists to explicitly mark binary data for DynamoDB; passing a str, int, or other object defeats its purpose and is rejected at construction time.","triggerScenarios":"Binary('hello') (str), Binary(42) (int), Binary([1,2,3]) (list). Also Binary(memoryview(b'x')) unless converted to bytes first.","commonSituations":"Porting Python 2 code where str was byte-like; forgetting to encode a string; passing a file object or memoryview without conversion.","solutions":["Encode strings first: Binary('hello'.encode('utf-8')).","Convert memoryview/buffer objects: Binary(bytes(mv)).","Pass raw bytes or bytearray directly: Binary(b'\\x01\\x02')."],"exampleFix":"# before\nb = Binary('some-text')\n\n# after\nb = Binary(b'some-bytes')\n# or from a string\nb = Binary('some-text'.encode('utf-8'))","handlingStrategy":"type-guard","validationCode":"from boto3.dynamodb.types import Binary, BINARY_TYPES\n\ndef make_binary(value):\n    if not isinstance(value, BINARY_TYPES):\n        if isinstance(value, str):\n            value = value.encode('utf-8')\n        elif isinstance(value, (bytearray, memoryview)):\n            value = bytes(value)\n        else:\n            raise TypeError(f'Cannot convert {type(value)} to Binary')\n    return Binary(value)","typeGuard":"def is_binary_value(v) -> bool:\n    return isinstance(v, (bytes, bytearray))","tryCatchPattern":null,"preventionTips":["Always call .encode() on strings before wrapping in Binary.","Convert memoryview to bytes before Binary construction.","Unit-test Binary creation paths with both str and bytes inputs."],"tags":["dynamodb","types","binary","serialization"],"analyzedSha":"c7b4afac237b976d48395d7523eaf7cec3a450b3","analyzedAt":"2026-08-04T20:35:51.598Z","schemaVersion":2}