huggingface/transformers · error · ValueError
When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make
Error message
When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined What it means
PaddingStrategy.MAX_LENGTH pads everything to a fixed max_length, so that length must be known. If padding resolves to 'max_length' and max_length is None, the strategy is meaningless and the extractor raises this ValueError immediately.
Source
Thrown at src/transformers/feature_extraction_sequence_utils.py:358
"""
Find the correct padding strategy
"""
# Get padding strategy
if padding is not False:
if padding is True:
padding_strategy = PaddingStrategy.LONGEST # Default to pad to the longest sequence in the batch
elif not isinstance(padding, PaddingStrategy):
padding_strategy = PaddingStrategy(padding)
elif isinstance(padding, PaddingStrategy):
padding_strategy = padding
else:
padding_strategy = PaddingStrategy.DO_NOT_PAD
# Set max length if needed
if max_length is None:
if padding_strategy == PaddingStrategy.MAX_LENGTH:
raise ValueError(
f"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"
)
# Test if we have a padding value
if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
raise ValueError(
"Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"
" as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."
)
return padding_strategy
def fetch_audio(self, audio_url_or_urls: str | list[str] | list[list[str]], sampling_rate: int | None = None):
"""
Convert a single or a list of urls into the corresponding `np.ndarray` objects.
If a single url is passed, the return value will be a single object. If a list is passed a list of objects is
returned.View on GitHub (pinned to a597f97485)
Solutions
- Pass max_length explicitly: fe(audio, padding='max_length', max_length=32000)
- Or use padding='longest'/padding=True if a fixed length is not required
- Set a default max_length on the extractor config if it should always apply
Example fix
# before fe(audio, padding="max_length") # after fe(audio, padding="max_length", max_length=32000)
Defensive patterns
Strategy: validation
Validate before calling
if padding == "max_length" and max_length is None:
raise ValueError("padding='max_length' needs max_length") # fail early at the call site
fe(audio, padding=padding, max_length=max_length) Try / catch
try:
fe(audio, padding="max_length")
except ValueError as e:
if "max_length" in str(e):
fe(audio, padding="longest") # or supply max_length
else:
raise Prevention
- Treat padding='max_length' and max_length as one mandatory pair
- Default to padding='longest' when a fixed size is unneeded
- Set max_length once in the extractor config for consistent behavior
When it happens
Trigger: Calling fe(audio, padding='max_length') without max_length; also padding=True where the extractor's default strategy was configured as MAX_LENGTH but no length is set anywhere.
Common situations: Assuming max_length defaults from the model config (it does not for feature extractors); migrating tokenizer call patterns verbatim; hand-edited preprocessor configs.
Related errors
- You should supply an instance of `transformers.BatchFeature`
- type of {first_element} unknown: {type(first_element)}. Shou
- Some items in the output dictionary have a different batch s
- Invalid padding strategy:{padding_side}
- When setting ``truncation=True``, make sure that ``max_lengt
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/5430dad2518b63b6.
Report an issue: GitHub.