huggingface/transformers · error · ValueError

Pad size must contain 'height' and 'width' keys only. Got pa

Error message

Pad size must contain 'height' and 'width' keys only. Got pad_size={pad_size}.

What it means

Error "Pad size must contain 'height' and 'width' keys only. Got pad_size={pad_size}." thrown in huggingface/transformers.

Source

Thrown at src/transformers/image_processing_backends.py:169

    def convert_to_rgb(self, image: ImageInput) -> ImageInput:
        """Convert an image to RGB format."""
        return convert_to_rgb(image)

    def pad(
        self,
        images: list["torch.Tensor"],
        pad_size: SizeDict = None,
        fill_value: int | None = 0,
        padding_mode: str | None = "constant",
        return_mask: bool = False,
        disable_grouping: bool | None = False,
        is_nested: bool | None = False,
        **kwargs,
    ) -> Union[tuple["torch.Tensor", "torch.Tensor"], "torch.Tensor"]:
        """Pad images using Torchvision with batched operations."""
        if pad_size is not None:
            if not (pad_size.height and pad_size.width):
                raise ValueError(f"Pad size must contain 'height' and 'width' keys only. Got pad_size={pad_size}.")
            pad_size = (pad_size.height, pad_size.width)
        else:
            pad_size = get_max_height_width(images)

        grouped_images, grouped_images_index = group_images_by_shape(
            images, disable_grouping=disable_grouping, is_nested=is_nested
        )
        processed_images_grouped = {}
        processed_masks_grouped = {}
        for shape, stacked_images in grouped_images.items():
            image_size = stacked_images.shape[-2:]
            padding_height = pad_size[0] - image_size[0]
            padding_width = pad_size[1] - image_size[1]
            if padding_height < 0 or padding_width < 0:
                raise ValueError(
                    f"Padding dimensions are negative. Please make sure that the `pad_size` is larger than the "
                    f"image size. Got pad_size={pad_size}, image_size={image_size}."
                )

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass `pad_size` as a dict with only 'height' and 'width' keys.

When it happens

Trigger: Raised in image padding when pad_size dict contains keys other than 'height' and 'width'.

Common situations: Passing pad_size with size-style keys like 'shortest_edge' or extra entries instead of exactly height/width.


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