boto/boto3 · error · TypeError

Value must be of the following types: {types}

Error message

Value must be of the following types: {types}

What it means

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.

Source

Thrown at boto3/dynamodb/types.py:59

    traps=[Clamped, Overflow, Inexact, Rounded, Underflow],
)


BINARY_TYPES = (bytearray, bytes)


class Binary:
    """A class for representing Binary in dynamodb

    Especially for Python 2, use this class to explicitly specify
    binary data for item in DynamoDB. It is essentially a wrapper around
    binary. Unicode and Python 3 string types are not allowed.
    """

    def __init__(self, value):
        if not isinstance(value, BINARY_TYPES):
            types = ', '.join([str(t) for t in BINARY_TYPES])
            raise TypeError(f'Value must be of the following types: {types}')
        self.value = value

    def __eq__(self, other):
        if isinstance(other, Binary):
            return self.value == other.value
        return self.value == other

    def __ne__(self, other):
        return not self.__eq__(other)

    def __repr__(self):
        return f'Binary({self.value!r})'

    def __str__(self):
        return self.value

    def __bytes__(self):
        return self.value

View on GitHub (pinned to c7b4afac23)

Solutions

  1. Encode strings first: Binary('hello'.encode('utf-8')).
  2. Convert memoryview/buffer objects: Binary(bytes(mv)).
  3. Pass raw bytes or bytearray directly: Binary(b'\x01\x02').

Example fix

# before
b = Binary('some-text')

# after
b = Binary(b'some-bytes')
# or from a string
b = Binary('some-text'.encode('utf-8'))
Defensive patterns

Strategy: type-guard

Validate before calling

from boto3.dynamodb.types import Binary, BINARY_TYPES

def make_binary(value):
    if not isinstance(value, BINARY_TYPES):
        if isinstance(value, str):
            value = value.encode('utf-8')
        elif isinstance(value, (bytearray, memoryview)):
            value = bytes(value)
        else:
            raise TypeError(f'Cannot convert {type(value)} to Binary')
    return Binary(value)

Type guard

def is_binary_value(v) -> bool:
    return isinstance(v, (bytes, bytearray))

Prevention

When it happens

Trigger: Binary('hello') (str), Binary(42) (int), Binary([1,2,3]) (list). Also Binary(memoryview(b'x')) unless converted to bytes first.

Common situations: Porting Python 2 code where str was byte-like; forgetting to encode a string; passing a file object or memoryview without conversion.

Related errors


AI-assisted analysis of boto/boto3@c7b4afac23 (2026-08-04). Data as JSON: /data/errors/968c6a8065f5b337.json. Report an issue: GitHub.