{"record":{"id":"2a51368df677852a","repo":"apache/beam","slug":"batch-type-must-be-torch-tensor-or-beam-typehints-pytorch","errorCode":null,"errorMessage":"batch type must be torch.Tensor or beam.typehints.pytorch_type_compatibility.PytorchTensor[..]","messagePattern":"batch type must be torch\\.Tensor or beam\\.typehints\\.pytorch_type_compatibility\\.PytorchTensor\\[\\.\\.\\]","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/pytorch_type_compatibility.py","lineNumber":49,"sourceCode":"      element_type,\n      dtype,\n      element_shape=(),\n      partition_dimension=0):\n    super().__init__(batch_type, element_type)\n    self.dtype = dtype\n    self.element_shape = element_shape\n    self.partition_dimension = partition_dimension\n\n  @staticmethod\n  @BatchConverter.register(name=\"pytorch\")\n  def from_typehints(element_type,\n                     batch_type) -> Optional['PytorchBatchConverter']:\n    if not isinstance(element_type, PytorchTypeHint.PytorchTypeConstraint):\n      element_type = PytorchTensor[element_type, ()]\n\n    if not isinstance(batch_type, PytorchTypeHint.PytorchTypeConstraint):\n      if not batch_type == torch.Tensor:\n        raise TypeError(\n            \"batch type must be torch.Tensor or \"\n            \"beam.typehints.pytorch_type_compatibility.PytorchTensor[..]\")\n      batch_type = PytorchTensor[element_type.dtype, (N, )]\n\n    if not batch_type.dtype == element_type.dtype:\n      raise TypeError(\n          \"batch type and element type must have equivalent dtypes \"\n          f\"(batch={batch_type.dtype}, element={element_type.dtype})\")\n    computed_element_shape = list(batch_type.shape)\n    partition_dimension = computed_element_shape.index(N)\n    computed_element_shape.pop(partition_dimension)\n    if not tuple(computed_element_shape) == element_type.shape:\n      raise TypeError(\n          \"Could not align batch type's batch dimension with element type. \"\n          f\"(batch type dimensions: {batch_type.shape}, element type \"\n          f\"dimenstions: {element_type.shape}\")\n\n    return PytorchBatchConverter(","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/pytorch_type_compatibility.py#L31-L67","documentation":"PytorchBatchConverter.from_typehints builds a converter between individual PyTorch model elements and tensor batches. batch_type must be torch.Tensor or a PytorchTensor[...] Beam constraint; anything else raises this TypeError. This ensures batching operates only on genuine torch tensor types with a known dtype.","triggerScenarios":"Calling PytorchBatchConverter.from_typehints (via create_pytorch_batch_converter or BatchConverter.from_typehints) with batch_type set to something other than torch.Tensor or a PytorchTypeConstraint, e.g. numpy.ndarray, a string 'torch.Tensor', or a list type.","commonSituations":"Configuring batching for PyTorch inference pipelines where the batch type was configured from YAML/config as a string; mixing numpy and torch inference code; forgetting to import torch and passing a placeholder class.","solutions":["Pass the actual torch.Tensor class object (import torch) or a PytorchTensor[dtype, shape] constraint as batch_type.","If batch_type comes from config as a string, resolve it to torch.Tensor before calling.","Also verify element_type is a PytorchTypeConstraint (or wrap it with PytorchTensor[element_type, ()]) and that element and batch dtypes match to avoid the follow-up dtype TypeError.","Catch TypeError at converter construction to fail fast before launching the pipeline."],"exampleFix":"// before\nconverter = PytorchBatchConverter.from_typehints(element_type=model_output, batch_type='torch.Tensor')\n// after\nimport torch\nfrom apache_beam.typehints import pytorch_type_compatibility as ptc\nconverter = PytorchBatchConverter.from_typehints(\n    element_type=model_output, batch_type=torch.Tensor)","handlingStrategy":"validation","validationCode":"import torch\nfrom apache_beam.typehints.pytorch_type_compatibility import PytorchTensor\ndef valid_torch_batch_type(batch_type) -> bool:\n    return batch_type == torch.Tensor or isinstance(batch_type, type(torch.Tensor[object, ()]) if False else object)\n","typeGuard":"def is_torch_batch_type(batch_type) -> bool:\n    import torch\n    from apache_beam.typehints.pytorch_type_compatibility import PytorchTensor, PytorchTypeHint\n    return batch_type == torch.Tensor or isinstance(batch_type, PytorchTypeHint.PytorchTypeConstraint)\n","tryCatchPattern":"try:\n    converter = PytorchBatchConverter.from_typehints(element_type=et, batch_type=bt)\nexcept TypeError as e:\n    raise ValueError(f'Unsupported torch batch type {bt!r}; use torch.Tensor or PytorchTensor[..]') from e\n","preventionTips":["Import torch in the module building converters so batch_type is the real class, not a string/None.","Use PytorchTensor[dtype, shape] constraints for shape-aware batching.","Ensure element_type and batch_type dtypes match before constructing the converter.","Validate converter construction in a unit test before submitting the pipeline to a runner."],"tags":["python","apache-beam","pytorch","batching"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}