{"record":{"id":"e0202bd43a0e1004","repo":"apache/beam","slug":"batch-batch-r-does-not-have-expected-dtype-self-dtype-r-e0202b","errorCode":null,"errorMessage":"Batch {batch!r} does not have expected dtype: {self.dtype!r}","messagePattern":"Batch (.+?) does not have expected dtype: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/pytorch_type_compatibility.py","lineNumber":101,"sourceCode":"\n  def get_length(self, batch):\n    return batch.size(dim=self.partition_dimension)\n\n  def estimate_byte_size(self, batch):\n    return batch.nelement() * batch.element_size()\n\n\nclass PytorchTypeHint():\n  class PytorchTypeConstraint(typehints.TypeConstraint):\n    def __init__(self, dtype, shape=()):\n      self.dtype = dtype\n      self.shape = shape\n\n    def type_check(self, batch):\n      if not isinstance(batch, torch.Tensor):\n        raise TypeError(f\"Batch {batch!r} is not an instance of torch.Tensor\")\n      if not batch.dtype == self.dtype:\n        raise TypeError(\n            f\"Batch {batch!r} does not have expected dtype: {self.dtype!r}\")\n\n      for dim in range(len(self.shape)):\n        if not self.shape[dim] == N and not batch.shape[dim] == self.shape[dim]:\n          raise TypeError(\n              f\"Batch {batch!r} does not have expected shape: {self.shape!r}\")\n\n    def _consistent_with_check_(self, sub):\n      # TODO Check sub against batch type, and element type\n      return True\n\n    def __key(self):\n      return (self.dtype, self.shape)\n\n    def __eq__(self, other) -> bool:\n      if isinstance(other, PytorchTypeHint.PytorchTypeConstraint):\n        return self.__key() == other.__key()\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/pytorch_type_compatibility.py#L83-L119","documentation":"PytorchTypeConstraint.type_check verifies that a torch.Tensor batch has the dtype declared in the PytorchTensor[dtype, shape] hint. When tensor.dtype differs from the constraint's dtype, Beam raises this TypeError at runtime, because downstream code (e.g. a model) expects a specific dtype such as float32.","triggerScenarios":"Annotating a PCollection with PytorchTensor[torch.float32, ...] while the DoFn emits tensors created as float64 (e.g. converted from numpy float64 arrays via torch.from_numpy), or after a .double()/.half() cast, when runtime type checking is enabled.","commonSituations":"torch.from_numpy preserving float64 numpy dtype, mixing a half-precision model with float32 hints, loading checkpoints saved in a different dtype, or torch.tensor on integer data producing int64 where a float hint was declared.","solutions":["Cast the tensor to the hinted dtype before yielding it: batch.to(torch.float32) (or .float()/.double() as appropriate).","Create tensors with the intended dtype explicitly: torch.tensor(data, dtype=torch.float32).","Change the PytorchTensor hint's dtype to match the actual runtime dtype, e.g. PytorchTensor[torch.float64, (N, 128)].","Ensure numpy conversions use matching dtypes (arr.astype(np.float32) before torch.from_numpy)."],"exampleFix":"// before\nyield torch.from_numpy(embeddings)  # float64 from numpy, hint is float32\n// after\nyield torch.from_numpy(embeddings.astype(np.float32))  # matches PytorchTensor[torch.float32, ...]","handlingStrategy":"type-guard","validationCode":"def ensure_dtype(t: torch.Tensor, dtype) -> torch.Tensor:\n    return t if t.dtype == dtype else t.to(dtype)","typeGuard":"def has_dtype(t, dtype) -> bool:\n    return isinstance(t, torch.Tensor) and t.dtype == dtype","tryCatchPattern":"try:\n    yield batch\nexcept TypeError as e:\n    if 'does not have expected dtype' in str(e):\n        yield batch.to(expected_dtype)\n    else:\n        raise","preventionTips":["Specify dtype explicitly when constructing tensors (torch.tensor(data, dtype=...)).","Remember torch.from_numpy preserves the numpy dtype; astype first if needed.","Declare the PytorchTensor dtype from the same constant your tensors are created with.","Beware mixed-precision (.half()) paths changing runtime dtypes."],"tags":["apache-beam","pytorch","dtype","type-check"],"backgroundTag":"dtype-mismatch","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"}