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

  1. Use return_tensors='pt' (with torch installed) or return_tensors=None.
  2. 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])).
  3. 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

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


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/1fcfc34b0d40fdb1. Report an issue: GitHub.