huggingface/transformers · error · ValueError
return_tensors should be `'pt'` or `None`
Error message
return_tensors should be `'pt'` or `None`
What it means
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.
Source
Thrown at src/transformers/data/processors/utils.py:330
if return_tensors is None:
return features
elif return_tensors == "pt":
if not is_torch_available():
raise RuntimeError("return_tensors set to 'pt' but PyTorch can't be imported")
import torch
from torch.utils.data import TensorDataset
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
if self.mode == "classification":
all_labels = torch.tensor([f.label for f in features], dtype=torch.long)
elif self.mode == "regression":
all_labels = torch.tensor([f.label for f in features], dtype=torch.float)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_labels)
return dataset
else:
raise ValueError("return_tensors should be `'pt'` or `None`")
View on GitHub (pinned to a597f97485)
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.
Example fix
# before dataset = featurizer.get_features(texts, return_tensors="np") # after features = featurizer.get_features(texts, return_tensors=None) import numpy as np input_ids = np.array([f.input_ids for f in features], dtype=np.int64)
Defensive patterns
Strategy: validation
Validate before calling
if return_tensors not in (None, "pt"):
raise ValueError(f"Unsupported return_tensors={return_tensors!r}; use 'pt' or None")
features = featurizer.get_features(texts, return_tensors=return_tensors) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Porting code between the tokenizer __call__ API (which accepts 'pt'/'tf'/'np') and the older data.processors featurizer; assuming symmetric APIs across the library.
Related errors
- Argument `{}` is not a valid argument of `GenerationConfig`.
- PUSH_TO_HUB_TOKEN is not set, cannot push results to the Hub
- reference must be greater than zero
- min_value must be greater than zero
- db_range must be greater than zero
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/1fcfc34b0d40fdb1.
Report an issue: GitHub.