{"record":{"id":"2154e507eb5627a7","repo":"huggingface/transformers","slug":"cannot-specify-both-size-as-an-int-with-default-t","errorCode":null,"errorMessage":"Cannot specify both size as an int, with default_to_square=True and max_size","messagePattern":"Cannot specify both size as an int, with default_to_square=True and max_size","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_processing_utils.py","lineNumber":561,"sourceCode":"        return False\n\n    size_dict_keys = set(size_dict.keys())\n    for allowed_keys in VALID_SIZE_DICT_KEYS:\n        if size_dict_keys == allowed_keys:\n            return True\n    return False\n\n\ndef convert_to_size_dict(\n    size: int | Iterable[int] | None = None,\n    max_size: int | None = None,\n    default_to_square: bool = True,\n    height_width_order: bool = True,\n) -> dict[str, int]:\n    # By default, if size is an int we assume it represents a tuple of (size, size).\n    if isinstance(size, int) and default_to_square:\n        if max_size is not None:\n            raise ValueError(\"Cannot specify both size as an int, with default_to_square=True and max_size\")\n        return {\"height\": size, \"width\": size}\n    # In other configs, if size is an int and default_to_square is False, size represents the length of\n    # the shortest edge after resizing.\n    elif isinstance(size, int) and not default_to_square:\n        size_dict = {\"shortest_edge\": size}\n        if max_size is not None:\n            size_dict[\"longest_edge\"] = max_size\n        return size_dict\n    # Otherwise, if size is a tuple it's either (height, width) or (width, height)\n    elif isinstance(size, (tuple, list)) and height_width_order:\n        return {\"height\": size[0], \"width\": size[1]}\n    elif isinstance(size, (tuple, list)) and not height_width_order:\n        return {\"height\": size[1], \"width\": size[0]}\n    elif size is None and max_size is not None:\n        if default_to_square:\n            raise ValueError(\"Cannot specify both default_to_square=True and max_size\")\n        return {\"longest_edge\": max_size}\n","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_processing_utils.py#L543-L579","documentation":"Thrown by convert_to_size_dict in transformers' image processing utils when the legacy `size` argument is an int while `default_to_square=True` and a `max_size` is also supplied. An int size with default_to_square means a fixed (size, size) square, which leaves no role for max_size (a cap on the longest edge when scaling the shortest edge), so the combination is ambiguous and rejected. It exists to stop image processor configs from mixing square-resize semantics with aspect-preserving-resize semantics.","triggerScenarios":"Calling get_size_dict(size=224, max_size=256, default_to_square=True) (default), or instantiating an image processor from a config where size is an int and max_size is set (e.g. ImageProcessor(size=224, max_size=256)). Any convert_to_size_dict call where isinstance(size, int) and default_to_square and max_size is not None.","commonSituations":"Upgrading old feature-extractor configs that used int size + max_size, copy-pasting resize parameters between processors that use square resizing (ViT-style) and shortest-edge resizing (ConvNet-style), or passing max_size unconditionally when building configs programmatically.","solutions":["Drop max_size if you want a square resize: get_size_dict(size=224) -> {'height': 224, 'width': 224}.","Or pass default_to_square=False to switch to shortest/longest-edge semantics: get_size_dict(size=224, max_size=256, default_to_square=False) -> {'shortest_edge': 224, 'longest_edge': 256}.","Or pass size as an explicit (height, width) tuple, which bypasses the int+square branch entirely.","If loading from a saved preprocessor_config.json, edit it so size is either a dict like {'shortest_edge': ..., 'longest_edge': ...} or an int without max_size."],"exampleFix":"# before\nsize_dict = get_size_dict(size=224, max_size=256)  # raises ValueError\n\n# after (aspect-preserving resize)\nsize_dict = get_size_dict(size=224, max_size=256, default_to_square=False)\n# {'shortest_edge': 224, 'longest_edge': 256}","handlingStrategy":"validation","validationCode":"def validate_size_kwargs(size, max_size, default_to_square=True):\n    if isinstance(size, int) and default_to_square and max_size is not None:\n        raise ValueError(\"int size + default_to_square=True cannot take max_size; drop max_size or set default_to_square=False\")\n    return True","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralize size/max_size construction in one config helper so incompatible combos are caught once.","Prefer explicit dict sizes ({'shortest_edge': .., 'longest_edge': ..}) over legacy int + flags."],"tags":["image-processing","config","resize","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}