huggingface/transformers · error · RuntimeError

return_tensors set to 'pt' but PyTorch can't be imported

Error message

return_tensors set to 'pt' but PyTorch can't be imported

What it means

The featurizer can package examples as a torch TensorDataset when return_tensors='pt', but transformers is installed without PyTorch in the current environment (is_torch_available() is False). The library raises RuntimeError instead of attempting the import so you get a clear message rather than a ModuleNotFoundError for torch.

Source

Thrown at src/transformers/data/processors/utils.py:316

            elif self.mode == "regression":
                label = float(example.label)
            else:
                raise ValueError(self.mode)

            if ex_index < 5 and self.verbose:
                logger.info("*** Example ***")
                logger.info(f"guid: {example.guid}")
                logger.info(f"input_ids: {' '.join([str(x) for x in input_ids])}")
                logger.info(f"attention_mask: {' '.join([str(x) for x in attention_mask])}")
                logger.info(f"label: {example.label} (id = {label})")

            features.append(InputFeatures(input_ids=input_ids, attention_mask=attention_mask, label=label))

        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. Install PyTorch in the active environment (pip install torch), then retry.
  2. If you deliberately run without torch, call the featurizer with return_tensors=None and consume the list of InputFeatures.
  3. Verify the environment with python -c "import torch" and check you are in the interpreter/venv you think you are.

Example fix

# before
features = featurizer.get_features(texts, return_tensors="pt")  # no torch installed

# after (option A): install torch
# pip install torch
# after (option B): stay framework-free
features = featurizer.get_features(texts, return_tensors=None)
Defensive patterns

Strategy: validation

Validate before calling

from transformers.utils import is_torch_available
if return_tensors == "pt" and not is_torch_available():
    raise RuntimeError("torch required for return_tensors='pt'; pip install torch or use return_tensors=None")

Try / catch

try:
    ds = featurizer.get_features(texts, return_tensors="pt")
except RuntimeError as e:
    if "PyTorch" in str(e):
        features = featurizer.get_features(texts, return_tensors=None)  # graceful degrade
    else:
        raise

Prevention

When it happens

Trigger: Calling get_features(..., return_tensors='pt') in an environment where torch is not installed; running on a CPU-only slim install of transformers (pip install transformers without torch); a venv/container where torch was uninstalled or never installed.

Common situations: Using transformers only for tokenizers/ONNX/JAX and then trying the PyTorch tensor path; CI images that omit torch to save space; switching conda envs mid-project.

Related errors


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