{"record":{"id":"5cc4fad3f8f8a38d","repo":"huggingface/transformers","slug":"unable-to-convert-output-to-pytorch-tensors-format","errorCode":null,"errorMessage":"Unable to convert output to PyTorch tensors format, PyTorch is not installed.","messagePattern":"Unable to convert output to PyTorch tensors format, PyTorch is not installed\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"src/transformers/feature_extraction_utils.py","lineNumber":118,"sourceCode":"\n    def __getstate__(self):\n        return {\"data\": self.data}\n\n    def __setstate__(self, state):\n        if \"data\" in state:\n            self.data = state[\"data\"]\n\n    def _get_is_as_tensor_fns(self, tensor_type: str | TensorType | None = None):\n        if tensor_type is None:\n            return None, None\n\n        # Convert to TensorType\n        if not isinstance(tensor_type, TensorType):\n            tensor_type = TensorType(tensor_type)\n\n        if tensor_type == TensorType.PYTORCH:\n            if not is_torch_available():\n                raise ImportError(\"Unable to convert output to PyTorch tensors format, PyTorch is not installed.\")\n            import torch\n\n            def as_tensor(value):\n                if torch.is_tensor(value):\n                    return value\n\n                # stack list of tensors if tensor_type is PyTorch (# torch.tensor() does not support list of tensors)\n                if isinstance(value, (list, tuple)) and len(value) > 0 and torch.is_tensor(value[0]):\n                    return torch.stack(value)\n\n                # convert list of numpy arrays to numpy array (stack) if tensor_type is Numpy\n                if isinstance(value, (list, tuple)) and len(value) > 0:\n                    if isinstance(value[0], np.ndarray):\n                        value = np.array(value)\n                    elif (\n                        isinstance(value[0], (list, tuple))\n                        and len(value[0]) > 0\n                        and isinstance(value[0][0], np.ndarray)","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/feature_extraction_utils.py#L100-L136","documentation":"BatchFeature.convert_to_tensors resolves the as_tensor function per TensorType. For TensorType.PYTORCH it first checks is_torch_available(); if transformers was installed without torch (CPU-only extras, minimal dep set, or broken torch install), conversion cannot proceed and ImportError is raised.","triggerScenarios":"Calling a feature extractor (or BatchFeature(..., tensor_type='pt') / return_tensors='pt') in an environment where import torch fails or torch is not installed.","commonSituations":"Deploying in slim containers/docker images without the torch dependency; CI lint/type-check jobs with transformers but no torch; installing transformers via a meta-package that omits torch.","solutions":["Install torch in the environment (pip install torch) or use the appropriate transformers extra","If torch is installed, verify 'import torch' works — a broken install can also trip is_torch_available()","If you don't need tensors, call with return_tensors=None to keep numpy/python objects"],"exampleFix":"# before (env without torch)\nfe(audio, return_tensors=\"pt\")\n\n# after\npip install torch   # then:\nfe(audio, return_tensors=\"pt\")\n# or without torch:\nfe(audio)  # numpy output","handlingStrategy":"validation","validationCode":"from transformers.utils import is_torch_available\n\nif not is_torch_available():\n    raise ImportError(\"install torch before requesting return_tensors='pt'\")","typeGuard":"def can_return_pt() -> bool:\n    from transformers.utils import is_torch_available\n    return is_torch_available()","tryCatchPattern":"try:\n    batch = fe(audio, return_tensors=\"pt\")\nexcept ImportError as e:\n    if \"PyTorch is not installed\" in str(e):\n        batch = fe(audio, return_tensors=\"np\")\n    else:\n        raise","preventionTips":["Pin torch in requirements for any tensor-returning path","Check is_torch_available() at startup, not mid-pipeline","Have a numpy fallback path for slim environments"],"tags":["feature-extractor","environment","pytorch","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}