huggingface/transformers · error · ValueError

Invalid padding strategy:{padding_side}

Error message

Invalid padding strategy:{padding_side}

What it means

The padder supports only padding_side 'right' and 'left'. If the feature extractor's padding_side attribute is anything else (typo like 'both', 'center', an empty string, or a mis-set value on a custom extractor), the else branch raises this ValueError.

Source

Thrown at src/transformers/feature_extraction_sequence_utils.py:292

                if return_attention_mask:
                    processed_features["attention_mask"] = np.pad(
                        processed_features["attention_mask"], (0, difference)
                    )
                padding_shape = ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
                processed_features[self.model_input_names[0]] = np.pad(
                    required_input, padding_shape, "constant", constant_values=self.padding_value
                )
            elif self.padding_side == "left":
                if return_attention_mask:
                    processed_features["attention_mask"] = np.pad(
                        processed_features["attention_mask"], (difference, 0)
                    )
                padding_shape = ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
                processed_features[self.model_input_names[0]] = np.pad(
                    required_input, padding_shape, "constant", constant_values=self.padding_value
                )
            else:
                raise ValueError("Invalid padding strategy:" + str(self.padding_side))

        return processed_features

    def _truncate(
        self,
        processed_features: dict[str, np.ndarray] | BatchFeature,
        max_length: int | None = None,
        pad_to_multiple_of: int | None = None,
        truncation: bool | None = None,
    ):
        """
        Truncate inputs to predefined length or max length in the batch

        Args:
            processed_features(`Union[dict[str, np.ndarray], BatchFeature]`):
                Dictionary of input values (`np.ndarray[float]`) / input vectors (`list[np.ndarray[float]]`) or batch
                of inputs values (`list[np.ndarray[int]]`) / input vectors (`list[np.ndarray[int]]`)
            max_length (`int`, *optional*):

View on GitHub (pinned to a597f97485)

Solutions

  1. Set feature_extractor.padding_side = 'right' (or 'left') — the only supported values
  2. If you loaded a config file, fix the padding_side entry in the preprocessor_config.json
  3. For center/symmetric padding, pad manually with np.pad before calling the extractor

Example fix

# before
fe.padding_side = "both"
fe.pad(batch, padding=True)

# after
fe.padding_side = "left"
fe.pad(batch, padding=True)
Defensive patterns

Strategy: validation

Validate before calling

if fe.padding_side not in ("right", "left"):
    fe.padding_side = "right"

Type guard

def is_valid_padding_side(side) -> bool:
    return side in ("right", "left")

Try / catch

try:
    fe.pad(batch, padding=True)
except ValueError as e:
    if "Invalid padding strategy" in str(e):
        fe.padding_side = "right"
        fe.pad(batch, padding=True)
    else:
        raise

Prevention

When it happens

Trigger: Setting feature_extractor.padding_side to an unsupported value in __init__ kwargs or after construction, then calling with padding enabled; subclassing a sequence feature extractor and forgetting to override padding handling for a custom side.

Common situations: Copy-pasting config from processors that use different side names; loading a saved extractor JSON whose padding_side was hand-edited; assuming symmetric/center padding exists because some preprocessing libs offer it.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/2669deb4afbe2521. Report an issue: GitHub.