huggingface/transformers · error · ValueError
The size dictionary must have keys 'height' and 'width'. Got
Error message
The size dictionary must have keys 'height' and 'width'. Got {size.keys()} What it means
Error "The size dictionary must have keys 'height' and 'width'. Got {size.keys()}" thrown in huggingface/transformers.
Source
Thrown at src/transformers/image_processing_utils.py:522
Args:
image (`np.ndarray`):
Image to center crop.
size (`dict[str, int]`):
Size of the output image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format for the output image. If unset, the channel dimension format of the input
image is used. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
input_data_format (`ChannelDimension` or `str`, *optional*):
The channel dimension format for the input image. If unset, the channel dimension format is inferred
from the input image. Can be one of:
- `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
- `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
"""
size = get_size_dict(size)
if "height" not in size or "width" not in size:
raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}")
return center_crop(
image,
size=(size["height"], size["width"]),
data_format=data_format,
input_data_format=input_data_format,
**kwargs,
)
VALID_SIZE_DICT_KEYS = (
{"height", "width"},
{"shortest_edge"},
{"shortest_edge", "longest_edge"},
{"longest_edge"},
{"max_height", "max_width"},
)
View on GitHub (pinned to a597f97485)
Solutions
- Provide a size dict with 'height' and 'width' keys.
- Call `convert_to_size_dict` to normalize the size input.
When it happens
Trigger: Raised in BaseImageProcessor size validation when the size dict lacks 'height' and 'width' keys.
Common situations: Custom image processor calls with size given as a dict missing the canonical height/width keys.
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/005a846e3c2e6e07.
Report an issue: GitHub.