huggingface/transformers · error · ValueError

size must have 1 or 2 elements if it is a list or tuple

Error message

size must have 1 or 2 elements if it is a list or tuple

What it means

In the resize-shape helper, a list/tuple `size` must reduce to a single int or a (h, w) pair; length 3+ (or empty beyond the handled cases) raises this ValueError. A 1-element list is treated as an int, 2 elements as (height, width).

Source

Thrown at src/transformers/image_transforms.py:290

        max_size (`int`, *optional*):
            The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater
            than `max_size` after being resized according to `size`, then the image is resized again so that the longer
            edge is equal to `max_size`. As a result, `size` might be overruled, i.e the smaller edge may be shorter
            than `size`. Only used if `default_to_square` is `False`.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `tuple`: The target (height, width) dimension of the output image after resizing.
    """
    if isinstance(size, (tuple, list)):
        if len(size) == 2:
            return tuple(size)
        elif len(size) == 1:
            # Perform same logic as if size was an int
            size = size[0]
        else:
            raise ValueError("size must have 1 or 2 elements if it is a list or tuple")

    if default_to_square:
        return (size, size)

    height, width = get_image_size(input_image, input_data_format)
    short, long = (width, height) if width <= height else (height, width)
    requested_new_short = size

    new_short, new_long = requested_new_short, int(requested_new_short * long / short)

    if max_size is not None:
        if max_size <= requested_new_short:
            raise ValueError(
                f"max_size = {max_size} must be strictly greater than the requested "
                f"size for the smaller edge size = {size}"
            )
        if new_long > max_size:
            new_short, new_long = int(max_size * new_short / new_long), max_size

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass size=(height, width) or a single int.
  2. If size came from image.shape, take only spatial dims: h, w = image.shape[:2].
  3. Validate len(size) in {1, 2} in your config loader before calling the processor.

Example fix

# before
size = img.shape  # (H, W, 3)
out = get_resize_output_image_dims(img, size=size, default_to_square=False)

# after
h, w = img.shape[:2]
out = get_resize_output_image_dims(img, size=(h, w), default_to_square=False)
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(size, (tuple, list)):
    assert 1 <= len(size) <= 2, f"size must have 1 or 2 elements, got {len(size)}"

Type guard

def is_valid_resize_size(s) -> bool:
    return isinstance(s, int) or (isinstance(s, (tuple, list)) and len(s) in (1, 2))

Prevention

When it happens

Trigger: get_resize_output_image_dims(image, size=(224, 224, 3)) (accidentally including channels), size=[256, 256, 256], or size=[] style malformed iterables.

Common situations: Slicing arrays wrong so shape tuples include a channel dim, building size from image.shape, or config values written as 3-element lists.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/3d77efe4923db553. Report an issue: GitHub.