{"record":{"id":"5430dad2518b63b6","repo":"huggingface/transformers","slug":"when-setting-padding-paddingstrategy-max-length","errorCode":null,"errorMessage":"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined","messagePattern":"When setting ``padding=(.+?)``, make sure that max_length is defined","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/feature_extraction_sequence_utils.py","lineNumber":358,"sourceCode":"        \"\"\"\n        Find the correct padding strategy\n        \"\"\"\n\n        # Get padding strategy\n        if padding is not False:\n            if padding is True:\n                padding_strategy = PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch\n            elif not isinstance(padding, PaddingStrategy):\n                padding_strategy = PaddingStrategy(padding)\n            elif isinstance(padding, PaddingStrategy):\n                padding_strategy = padding\n        else:\n            padding_strategy = PaddingStrategy.DO_NOT_PAD\n\n        # Set max length if needed\n        if max_length is None:\n            if padding_strategy == PaddingStrategy.MAX_LENGTH:\n                raise ValueError(\n                    f\"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined\"\n                )\n\n        # Test if we have a padding value\n        if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):\n            raise ValueError(\n                \"Asking to pad but the feature_extractor does not have a padding value. Please select a value to use\"\n                \" as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.\"\n            )\n\n        return padding_strategy\n\n    def fetch_audio(self, audio_url_or_urls: str | list[str] | list[list[str]], sampling_rate: int | None = None):\n        \"\"\"\n        Convert a single or a list of urls into the corresponding `np.ndarray` objects.\n\n        If a single url is passed, the return value will be a single object. If a list is passed a list of objects is\n        returned.","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/feature_extraction_sequence_utils.py#L340-L376","documentation":"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.","triggerScenarios":"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.","commonSituations":"Assuming max_length defaults from the model config (it does not for feature extractors); migrating tokenizer call patterns verbatim; hand-edited preprocessor configs.","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"],"exampleFix":"# before\nfe(audio, padding=\"max_length\")\n\n# after\nfe(audio, padding=\"max_length\", max_length=32000)","handlingStrategy":"validation","validationCode":"if padding == \"max_length\" and max_length is None:\n    raise ValueError(\"padding='max_length' needs max_length\")  # fail early at the call site\nfe(audio, padding=padding, max_length=max_length)","typeGuard":null,"tryCatchPattern":"try:\n    fe(audio, padding=\"max_length\")\nexcept ValueError as e:\n    if \"max_length\" in str(e):\n        fe(audio, padding=\"longest\")  # or supply max_length\n    else:\n        raise","preventionTips":["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"],"tags":["feature-extractor","padding","audio","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}