{"record":{"id":"96261741fcc525c6","repo":"huggingface/transformers","slug":"when-setting-truncation-true-make-sure-that","errorCode":null,"errorMessage":"When setting ``truncation=True``, make sure that ``max_length`` is defined.","messagePattern":"When setting ``truncation=True``, make sure that ``max_length`` is defined\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/feature_extraction_sequence_utils.py","lineNumber":322,"sourceCode":"        Truncate inputs to predefined length or max length in the batch\n\n        Args:\n            processed_features(`Union[dict[str, np.ndarray], BatchFeature]`):\n                Dictionary of input values (`np.ndarray[float]`) / input vectors (`list[np.ndarray[float]]`) or batch\n                of inputs values (`list[np.ndarray[int]]`) / input vectors (`list[np.ndarray[int]]`)\n            max_length (`int`, *optional*):\n                maximum length of the returned list and optionally padding length (see below)\n            pad_to_multiple_of (`int`, *optional*) :\n                Integer if set will pad the sequence to a multiple of the provided value. This is especially useful to\n                enable the use of Tensor Core on NVIDIA hardware with compute capability `>= 7.5` (Volta), or on TPUs\n                which benefit from having sequence lengths be a multiple of 128.\n            truncation (`bool`, *optional*):\n                Activates truncation to cut input sequences longer than `max_length` to `max_length`.\n        \"\"\"\n        if not truncation:\n            return processed_features\n        elif truncation and max_length is None:\n            raise ValueError(\"When setting ``truncation=True``, make sure that ``max_length`` is defined.\")\n\n        required_input = processed_features[self.model_input_names[0]]\n\n        # find `max_length` that fits `pad_to_multiple_of`\n        if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):\n            max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of\n\n        needs_to_be_truncated = len(required_input) > max_length\n\n        if needs_to_be_truncated:\n            processed_features[self.model_input_names[0]] = processed_features[self.model_input_names[0]][:max_length]\n            if \"attention_mask\" in processed_features:\n                processed_features[\"attention_mask\"] = processed_features[\"attention_mask\"][:max_length]\n\n        return processed_features\n\n    def _get_padding_strategies(self, padding=False, max_length=None):\n        \"\"\"","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/feature_extraction_sequence_utils.py#L304-L340","documentation":"The internal _truncate step only cuts sequences when both truncation=True and a max_length to cut to are known. truncation=True without max_length leaves no defined target length, so the extractor refuses rather than truncating to an arbitrary length.","triggerScenarios":"Calling __call__ or _truncate with truncation=True but max_length=None and no model_max_length fallback at this layer: fe(audio, truncation=True) with no max_length argument.","commonSituations":"Porting tokenizer-style calls (where max_length defaults from model config) to feature extractors, which have no such default; setting truncation globally in a processing config but forgetting the length.","solutions":["Pass max_length together with truncation: fe(audio, truncation=True, max_length=16000)","Or define max_length once on the extractor (fe.max_length = N) if the subclass supports it","Drop truncation=True if you want full-length features"],"exampleFix":"# before\nfe(audio, truncation=True)\n\n# after\nfe(audio, truncation=True, max_length=160000)","handlingStrategy":"validation","validationCode":"def truncate_kwargs(truncation, max_length):\n    if truncation and max_length is None:\n        raise ValueError(\"truncation requires max_length\")\n    return {\"truncation\": truncation, \"max_length\": max_length}","typeGuard":null,"tryCatchPattern":"try:\n    fe(audio, truncation=True)\nexcept ValueError as e:\n    if \"max_length\" in str(e):\n        fe(audio, truncation=True, max_length=default_len)\n    else:\n        raise","preventionTips":["Always pair truncation=True with an explicit max_length","Do not assume tokenizer defaults transfer to feature extractors","Centralize length constants in one config object"],"tags":["feature-extractor","truncation","audio","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}