{"record":{"id":"bccc2d26f8b58d2d","repo":"huggingface/transformers","slug":"pytorch-must-be-installed-to-return-a-pytorch-data","errorCode":null,"errorMessage":"PyTorch must be installed to return a PyTorch dataset.","messagePattern":"PyTorch must be installed to return a PyTorch dataset\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/transformers/data/processors/squad.py","lineNumber":399,"sourceCode":"    new_features = []\n    unique_id = 1000000000\n    example_index = 0\n    for example_features in tqdm(\n        features, total=len(features), desc=\"add example index and unique id\", disable=not tqdm_enabled\n    ):\n        if not example_features:\n            continue\n        for example_feature in example_features:\n            example_feature.example_index = example_index\n            example_feature.unique_id = unique_id\n            new_features.append(example_feature)\n            unique_id += 1\n        example_index += 1\n    features = new_features\n    del new_features\n    if return_dataset == \"pt\":\n        if not is_torch_available():\n            raise RuntimeError(\"PyTorch must be installed to return a PyTorch dataset.\")\n\n        # Convert to Tensors and build dataset\n        all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)\n        all_attention_masks = torch.tensor([f.attention_mask for f in features], dtype=torch.long)\n        all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)\n        all_cls_index = torch.tensor([f.cls_index for f in features], dtype=torch.long)\n        all_p_mask = torch.tensor([f.p_mask for f in features], dtype=torch.float)\n        all_is_impossible = torch.tensor([f.is_impossible for f in features], dtype=torch.float)\n\n        if not is_training:\n            all_feature_index = torch.arange(all_input_ids.size(0), dtype=torch.long)\n            dataset = TensorDataset(\n                all_input_ids, all_attention_masks, all_token_type_ids, all_feature_index, all_cls_index, all_p_mask\n            )\n        else:\n            all_start_positions = torch.tensor([f.start_position for f in features], dtype=torch.long)\n            all_end_positions = torch.tensor([f.end_position for f in features], dtype=torch.long)\n            dataset = TensorDataset(","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/data/processors/squad.py#L381-L417","documentation":"Raised by squad_convert_examples_to_features when return_dataset='pt' but PyTorch is not installed. The function then proceeds to build torch.tensor objects and a TensorDataset, so it checks availability first and fails fast with a RuntimeError rather than an opaque NameError on the missing torch import.","triggerScenarios":"Calling squad_convert_examples_to_features(..., return_dataset='pt') in an environment where torch is absent — e.g. a TensorFlow/JAX-only install of transformers, or a slim deployment image.","commonSituations":"Reusing a SQuAD preprocessing script inside a TF-only training stack; CI environments that install transformers without torch; Docker images trimmed of PyTorch for inference.","solutions":["pip install torch if you actually want the TensorDataset output.","If you are on TF/JAX, pass return_dataset='tf' or omit it and build the dataset with your framework from the returned features.","Gate on transformers.utils.is_torch_available() in shared scripts to choose the right return_dataset per environment."],"exampleFix":"# before\nfeatures, dataset = squad_convert_examples_to_features(examples, tokenizer, 384, 128, return_dataset='pt')  # no torch installed\n\n# after\nfeatures, dataset = squad_convert_examples_to_features(examples, tokenizer, 384, 128, return_dataset='tf')","handlingStrategy":"validation","validationCode":"from transformers.utils import is_torch_available\n\nreturn_dataset = 'pt' if is_torch_available() else 'tf'\nfeatures, dataset = squad_convert_examples_to_features(\n    examples, tokenizer, max_seq_length=384, doc_stride=128, return_dataset=return_dataset\n)","typeGuard":"def has_torch() -> bool:\n    try:\n        import torch  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    features, dataset = squad_convert_examples_to_features(examples, tok, 384, 128, return_dataset='pt')\nexcept RuntimeError as e:\n    if 'PyTorch must be installed' in str(e):\n        features, _ = squad_convert_examples_to_features(examples, tok, 384, 128)  # features only\n    else:\n        raise","preventionTips":["Declare torch in your project dependencies if any code path uses return_dataset='pt'.","Branch on is_torch_available() in shared preprocessing scripts.","Consider requesting only features (return_dataset=None) and building tensors in the training framework."],"tags":["squad","environment","pytorch","optional-dependency","question-answering"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}