huggingface/transformers · error · ValueError
Could not convert size input to size dict: {size}
Error message
Could not convert size input to size dict: {size} What it means
convert_to_size_dict raises this catch-all ValueError when `size` matches none of the supported branches: not an int, not a tuple/list, not None-with-max_size. Typical offending values are strings ('256'), dicts passed to the legacy converter (dicts are handled by get_size_dict before this point), numpy scalars, or nested iterables. The message echoes the offending value so you can see exactly what fell through.
Source
Thrown at src/transformers/image_processing_utils.py:580
return {"height": size, "width": size}
# In other configs, if size is an int and default_to_square is False, size represents the length of
# the shortest edge after resizing.
elif isinstance(size, int) and not default_to_square:
size_dict = {"shortest_edge": size}
if max_size is not None:
size_dict["longest_edge"] = max_size
return size_dict
# Otherwise, if size is a tuple it's either (height, width) or (width, height)
elif isinstance(size, (tuple, list)) and height_width_order:
return {"height": size[0], "width": size[1]}
elif isinstance(size, (tuple, list)) and not height_width_order:
return {"height": size[1], "width": size[0]}
elif size is None and max_size is not None:
if default_to_square:
raise ValueError("Cannot specify both default_to_square=True and max_size")
return {"longest_edge": max_size}
raise ValueError(f"Could not convert size input to size dict: {size}")
def get_size_dict(
size: int | Iterable[int] | dict[str, int] | SizeDict | None = None,
max_size: int | None = None,
height_width_order: bool = True,
default_to_square: bool = True,
param_name="size",
) -> dict:
"""
Converts the old size parameter in the config into the new dict expected in the config. This is to ensure backwards
compatibility with the old image processor configs and removes ambiguity over whether the tuple is in (height,
width) or (width, height) format.
- If `size` is tuple, it is converted to `{"height": size[0], "width": size[1]}` or `{"height": size[1], "width":
size[0]}` if `height_width_order` is `False`.
- If `size` is an int, and `default_to_square` is `True`, it is converted to `{"height": size, "width": size}`.
- If `size` is an int and `default_to_square` is False, it is converted to `{"shortest_edge": size}`. If `max_size`View on GitHub (pinned to a597f97485)
Solutions
- Coerce string inputs to int or tuple before calling: int(size) if it is a numeric string.
- Pass size as an int, a (height, width) tuple/list, or a dict with valid keys such as {'height': ..., 'width': ...}.
- If size came from argparse or a config file, add explicit parsing/conversion at load time instead of forwarding raw strings.
- Print type(size) at the call site to identify the unexpected type.
Example fix
# before size = cfg["size"] # e.g. "224" from YAML get_size_dict(size) # after size = int(cfg["size"]) if isinstance(cfg["size"], str) else cfg["size"] get_size_dict(size)
Defensive patterns
Strategy: type-guard
Validate before calling
allowed = (type(None), int, tuple, list)
assert isinstance(size, allowed), f"size must be int/tuple/list/None, got {type(size)}" Type guard
def is_valid_legacy_size(size) -> bool:
return size is None or isinstance(size, int) or (isinstance(size, (tuple, list)) and all(isinstance(v, int) for v in size)) Try / catch
try:
size_dict = get_size_dict(size)
except ValueError as e:
if "Could not convert" in str(e):
size = int(size) if str(size).isdigit() else size
size_dict = get_size_dict(size)
else:
raise Prevention
- Coerce CLI/YAML config values to int/tuple at load time.
- Log type(size) once when building configs dynamically.
When it happens
Trigger: get_size_dict(size='224'), get_size_dict(size=np.int64(224) is fine as int subclass, but size={'wrong_key': 1} goes to the dict path and fails key validation; size=(224,) 1-tuples or size=[224, 224, 3] length-3 lists survive to other errors; direct convert_to_size_dict(size='x') raises here.
Common situations: Reading size from YAML/JSON/CLI where it arrives as a string, passing a nested config object instead of a plain int/tuple, or older code that wrapped size in an extra layer like ((224, 224),).
Related errors
- Cannot specify both size as an int, with default_to_square=T
- Cannot specify both default_to_square=True and max_size
- {param_name} must have one of the following set of keys: {VA
- max_size = {max_size} must be strictly greater than the requ
- Unsupported channel dimension format: {channel_dim}
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/94d1736b8a56f2f7.
Report an issue: GitHub.