huggingface/transformers · error · ValueError

size must have 2 elements

Error message

size must have 2 elements

What it means

The resize function requires exactly two elements (height, width) in `size`; len != 2 raises ValueError before any PIL work starts. Unlike the dims helper, a 1-element list is not auto-expanded here.

Source

Thrown at src/transformers/image_transforms.py:351

            Apply optimization by resizing the image in two steps. The bigger `reducing_gap`, the closer the result to
            the fair resampling. See corresponding Pillow documentation for more details.
        data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the output image. If unset, will use the inferred format from the input.
        return_numpy (`bool`, *optional*, defaults to `True`):
            Whether or not to return the resized image as a numpy array. If False a `PIL.Image.Image` object is
            returned.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `np.ndarray`: The resized image.
    """
    requires_backends(resize, ["vision"])

    resample = resample if resample is not None else PILImageResampling.BILINEAR

    if not len(size) == 2:
        raise ValueError("size must have 2 elements")

    # For all transformations, we want to keep the same data format as the input image unless otherwise specified.
    # The resized image from PIL will always have channels last, so find the input format first.
    if input_data_format is None:
        input_data_format = infer_channel_dimension_format(image)
    data_format = input_data_format if data_format is None else data_format

    # To maintain backwards compatibility with the resizing done in previous image feature extractors, we use
    # the pillow library to resize the image and then convert back to numpy
    do_rescale = False
    if not isinstance(image, PIL.Image.Image):
        do_rescale = _rescale_for_pil_conversion(image)
        image = to_pil_image(image, do_rescale=do_rescale, input_data_format=input_data_format)
    height, width = size
    # PIL images are in the format (width, height)
    resized_image = image.resize((width, height), resample=resample, reducing_gap=reducing_gap)

    if return_numpy:

View on GitHub (pinned to a597f97485)

Solutions

  1. Always pass a 2-tuple: resize(image, size=(224, 224)).
  2. For int semantics, expand first: (s, s).
  3. Prefer the image processor's preprocess/__call__ which normalizes size for you.

Example fix

# before
img = resize(img, size=224)  # not a 2-tuple

# after
img = resize(img, size=(224, 224))
Defensive patterns

Strategy: validation

Validate before calling

if not (isinstance(size, (tuple, list)) and len(size) == 2):
    size = (size, size) if isinstance(size, int) else tuple(size)

Type guard

def is_hw_pair(s) -> bool:
    return isinstance(s, (tuple, list)) and len(s) == 2

Prevention

When it happens

Trigger: resize(image, size=(224,)), resize(image, size=224) (an int has no len -> TypeError/len failure), or size=(224, 224, 3) including channels.

Common situations: Passing an int size directly to resize instead of going through the size-dict/processor layer, or forwarding a 3-D shape tuple.

Related errors


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