huggingface/transformers · error · ValueError
Cannot specify both default_to_square=True and max_size
Error message
Cannot specify both default_to_square=True and max_size
What it means
Thrown by convert_to_size_dict when size is None but max_size is given while default_to_square=True. A max_size alone only makes sense as the longest-edge cap of a shortest-edge resize (default_to_square=False); with square semantics there is no size to scale at all, so the library refuses rather than guess. It protects against half-configured resize specs.
Source
Thrown at src/transformers/image_processing_utils.py:577
if isinstance(size, int) and default_to_square:
if max_size is not None:
raise ValueError("Cannot specify both size as an int, with default_to_square=True and max_size")
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":View on GitHub (pinned to a597f97485)
Solutions
- Also set a size (shortest edge) and default_to_square=False: get_size_dict(size=800, max_size=1333, default_to_square=False).
- Or remove max_size if you do not want longest-edge capping.
- Check the preprocessor_config.json / kwargs you pass for a missing or null 'size' key and restore it.
Example fix
# before
size_dict = get_size_dict(max_size=1333) # size=None, raises
# after
size_dict = get_size_dict(size=800, max_size=1333, default_to_square=False)
# {'shortest_edge': 800, 'longest_edge': 1333} Defensive patterns
Strategy: validation
Validate before calling
if max_size is not None and size is None and default_to_square:
raise ValueError("max_size requires a size and default_to_square=False") Prevention
- Never set max_size without also setting a shortest-edge size.
- Assert config invariants after loading preprocessor_config.json.
When it happens
Trigger: get_size_dict(size=None, max_size=1333, default_to_square=True), or an image processor config that sets max_size but not size while default_to_square stays at its default True.
Common situations: Migrating detectron-style configs that specify only min_size/max_size, partially overriding a config dict and accidentally clearing 'size' while keeping 'max_size', or programmatically building kwargs where the size key is conditionally omitted.
Related errors
- Cannot specify both size as an int, with default_to_square=T
- max_size = {max_size} must be strictly greater than the requ
- Could not convert size input to size dict: {size}
- {param_name} must have one of the following set of keys: {VA
- size must have 1 or 2 elements if it is a list or tuple
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/57cb64882b31110b.
Report an issue: GitHub.