{"record":{"id":"94d1736b8a56f2f7","repo":"huggingface/transformers","slug":"could-not-convert-size-input-to-size-dict-size","errorCode":null,"errorMessage":"Could not convert size input to size dict: {size}","messagePattern":"Could not convert size input to size dict: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_processing_utils.py","lineNumber":580,"sourceCode":"        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\n    raise ValueError(f\"Could not convert size input to size dict: {size}\")\n\n\ndef get_size_dict(\n    size: int | Iterable[int] | dict[str, int] | SizeDict | None = None,\n    max_size: int | None = None,\n    height_width_order: bool = True,\n    default_to_square: bool = True,\n    param_name=\"size\",\n) -> dict:\n    \"\"\"\n    Converts the old size parameter in the config into the new dict expected in the config. This is to ensure backwards\n    compatibility with the old image processor configs and removes ambiguity over whether the tuple is in (height,\n    width) or (width, height) format.\n\n    - If `size` is tuple, it is converted to `{\"height\": size[0], \"width\": size[1]}` or `{\"height\": size[1], \"width\":\n    size[0]}` if `height_width_order` is `False`.\n    - If `size` is an int, and `default_to_square` is `True`, it is converted to `{\"height\": size, \"width\": size}`.\n    - If `size` is an int and `default_to_square` is False, it is converted to `{\"shortest_edge\": size}`. If `max_size`","sourceCodeStart":562,"sourceCodeEnd":598,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_processing_utils.py#L562-L598","documentation":"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.","triggerScenarios":"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.","commonSituations":"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),).","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."],"exampleFix":"# before\nsize = cfg[\"size\"]  # e.g. \"224\" from YAML\nget_size_dict(size)\n\n# after\nsize = int(cfg[\"size\"]) if isinstance(cfg[\"size\"], str) else cfg[\"size\"]\nget_size_dict(size)","handlingStrategy":"type-guard","validationCode":"allowed = (type(None), int, tuple, list)\nassert isinstance(size, allowed), f\"size must be int/tuple/list/None, got {type(size)}\"","typeGuard":"def is_valid_legacy_size(size) -> bool:\n    return size is None or isinstance(size, int) or (isinstance(size, (tuple, list)) and all(isinstance(v, int) for v in size))","tryCatchPattern":"try:\n    size_dict = get_size_dict(size)\nexcept ValueError as e:\n    if \"Could not convert\" in str(e):\n        size = int(size) if str(size).isdigit() else size\n        size_dict = get_size_dict(size)\n    else:\n        raise","preventionTips":["Coerce CLI/YAML config values to int/tuple at load time.","Log type(size) once when building configs dynamically."],"tags":["image-processing","type-coercion","config","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}