{"record":{"id":"dedf5841e70ae201","repo":"huggingface/transformers","slug":"invalid-padding-mode-mode","errorCode":null,"errorMessage":"Invalid padding mode: {mode}","messagePattern":"Invalid padding mode: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_transforms.py","lineNumber":750,"sourceCode":"        values = ((0, 0), *values) if input_data_format == ChannelDimension.FIRST else (*values, (0, 0))\n\n        # Add additional padding if there's a batch dimension\n        values = ((0, 0), *values) if image.ndim == 4 else values\n        return values\n\n    padding = _expand_for_data_format(padding)\n\n    if mode == PaddingMode.CONSTANT:\n        constant_values = _expand_for_data_format(constant_values)\n        image = np.pad(image, padding, mode=\"constant\", constant_values=constant_values)\n    elif mode == PaddingMode.REFLECT:\n        image = np.pad(image, padding, mode=\"reflect\")\n    elif mode == PaddingMode.REPLICATE:\n        image = np.pad(image, padding, mode=\"edge\")\n    elif mode == PaddingMode.SYMMETRIC:\n        image = np.pad(image, padding, mode=\"symmetric\")\n    else:\n        raise ValueError(f\"Invalid padding mode: {mode}\")\n\n    image = to_channel_dimension_format(image, data_format, input_data_format) if data_format is not None else image\n    return image\n\n\n# TODO (Amy): Accept 1/3/4 channel numpy array as input and return np.array as default\ndef convert_to_rgb(image: ImageInput) -> ImageInput:\n    \"\"\"\n    Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image\n    as is.\n    Args:\n        image (Image):\n            The image to convert.\n    \"\"\"\n    requires_backends(convert_to_rgb, [\"vision\"])\n\n    if not isinstance(image, PIL.Image.Image):\n        return image","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_transforms.py#L732-L768","documentation":"Raised by `transformers.image_transforms.pad()` when `mode` does not equal one of the four supported `PaddingMode` enum members: CONSTANT, REFLECT, REPLICATE, SYMMETRIC. `PaddingMode` is an `ExplicitEnum`, so raw strings 'constant', 'reflect', 'replicate', 'symmetric' also compare equal and are accepted; every other string or value reaches the else-branch and raises. This maps np.pad modes: replicate -> 'edge', symmetric -> 'symmetric'.","triggerScenarios":"Calling `pad(image, padding, mode='circular')`, `mode='edge'` (np.pad's name, not transformers'), `mode='zero'`, or a typo like 'refelct'. Also passing torchvision's padding constants or a `PaddingMode` from a different/older transformers import path whose value differs.","commonSituations":"Porting code from torchvision.transforms.Pad (which supports 'constant', 'edge', 'reflect', 'symmetric' — note 'edge' works there but not here) or from raw np.pad ('edge', 'wrap', 'maximum', etc.). Version changes or copy-pasted mode strings are typical causes.","solutions":["Use one of the four accepted values: 'constant', 'reflect', 'replicate', or 'symmetric' (or the `PaddingMode` enum members).","Map np.pad names to transformers names first: 'edge' -> 'replicate', 'symmetric' -> 'symmetric'.","For unsupported modes like 'wrap'/'circular', call `np.pad` (or `torch.nn.functional.pad`) directly on the array/tensor.","Import the enum to avoid typos: `from transformers.image_transforms import PaddingMode`."],"exampleFix":"// before\nimage = pad(img, (4, 4), mode=\"edge\")     # ValueError: Invalid padding mode\nimage = pad(img, (4, 4), mode=\"circular\")  # ValueError\n\n// after\nfrom transformers.image_transforms import PaddingMode\nimage = pad(img, (4, 4), mode=PaddingMode.REPLICATE)  # np.pad 'edge' equivalent\n// truly unsupported modes -> use the backend directly:\nimage = np.pad(img, ((0,0),(4,4),(4,4)), mode=\"wrap\")","handlingStrategy":"validation","validationCode":"from transformers.image_transforms import PaddingMode\n\nSUPPORTED = {m.value for m in PaddingMode}\nassert isinstance(mode, PaddingMode) or mode in SUPPORTED, f\"mode must be one of {SUPPORTED}, got {mode!r}\"","typeGuard":"from transformers.image_transforms import PaddingMode\n\ndef is_supported_pad_mode(mode) -> bool:\n    try:\n        PaddingMode(mode)\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    out = pad(image, padding, mode=mode)\nexcept ValueError as e:\n    if \"Invalid padding mode\" in str(e):\n        mode = \"replicate\" if mode == \"edge\" else mode  # remap foreign names\n        out = pad(image, padding, mode=mode)\n    else:\n        raise","preventionTips":["Import and use the PaddingMode enum instead of raw strings.","Map np.pad/torchvision mode names at your pipeline boundary ('edge' -> 'replicate').","Reject unsupported modes (wrap, circular) early and route them to np.pad/F.pad directly."],"tags":["image-processing","padding","enum","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}