{"record":{"id":"1fcfc34b0d40fdb1","repo":"huggingface/transformers","slug":"return-tensors-should-be-pt-or-none","errorCode":null,"errorMessage":"return_tensors should be `'pt'` or `None`","messagePattern":"return_tensors should be `'pt'` or `None`","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/data/processors/utils.py","lineNumber":330,"sourceCode":"        if return_tensors is None:\n            return features\n        elif return_tensors == \"pt\":\n            if not is_torch_available():\n                raise RuntimeError(\"return_tensors set to 'pt' but PyTorch can't be imported\")\n            import torch\n            from torch.utils.data import TensorDataset\n\n            all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)\n            all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)\n            if self.mode == \"classification\":\n                all_labels = torch.tensor([f.label for f in features], dtype=torch.long)\n            elif self.mode == \"regression\":\n                all_labels = torch.tensor([f.label for f in features], dtype=torch.float)\n\n            dataset = TensorDataset(all_input_ids, all_attention_mask, all_labels)\n            return dataset\n        else:\n            raise ValueError(\"return_tensors should be `'pt'` or `None`\")\n","sourceCodeStart":312,"sourceCodeEnd":331,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/data/processors/utils.py#L312-L331","documentation":"The legacy featurizer's return_tensors parameter only supports 'pt' (build a torch TensorDataset) or None (return a plain list of InputFeatures). Any other value - such as 'tf', 'np', or a typo - raises this ValueError, because no other backend is implemented in this API.","triggerScenarios":"Passing return_tensors='tf' or 'np' (valid for fast tokenizers but not this featurizer); a typo like 'PT' or ' pytorch '; copying a call from the PreTrainedTokenizer API where more values are accepted.","commonSituations":"Porting code between the tokenizer __call__ API (which accepts 'pt'/'tf'/'np') and the older data.processors featurizer; assuming symmetric APIs across the library.","solutions":["Use return_tensors='pt' (with torch installed) or return_tensors=None.","If you need NumPy or TF tensors, take the list of InputFeatures and stack them yourself (np.array([f.input_ids for f in features])).","Consider migrating to the modern tokenizer API, which supports 'tf' and 'np' natively."],"exampleFix":"# before\ndataset = featurizer.get_features(texts, return_tensors=\"np\")\n\n# after\nfeatures = featurizer.get_features(texts, return_tensors=None)\nimport numpy as np\ninput_ids = np.array([f.input_ids for f in features], dtype=np.int64)","handlingStrategy":"validation","validationCode":"if return_tensors not in (None, \"pt\"):\n    raise ValueError(f\"Unsupported return_tensors={return_tensors!r}; use 'pt' or None\")\nfeatures = featurizer.get_features(texts, return_tensors=return_tensors)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whitelist the parameter value before passing it through.","Remember this legacy API supports only 'pt'/None, unlike fast tokenizers.","Migrate to the modern tokenizer API if 'np'/'tf' outputs are needed."],"tags":["api-misuse","validation","tensors"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}