{"record":{"id":"e697486899894264","repo":"apache/beam","slug":"batch-batch-r-does-not-have-expected-dtype-self-dtype-r","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/batch.py","lineNumber":253,"sourceCode":"  def estimate_byte_size(self, batch):\n    return batch.nbytes\n\n\n# numpy is starting to add typehints, which we should support\n# https://numpy.org/doc/stable/reference/typing.html for now they don't allow\n# specifying shape, seems to be coming after\n# https://www.python.org/dev/peps/pep-0646/\nclass NumpyTypeHint():\n  class NumpyTypeConstraint(typehints.TypeConstraint):\n    def __init__(self, dtype, shape=()):\n      self.dtype = np.dtype(dtype)\n      self.shape = shape\n\n    def type_check(self, batch):\n      if not isinstance(batch, np.ndarray):\n        raise TypeError(f\"Batch {batch!r} is not an instance of ndarray\")\n      if not np.issubdtype(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, NumpyTypeHint.NumpyTypeConstraint):\n        return self.__key() == other.__key()\n","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/batch.py#L235-L271","documentation":"Runtime check inside NumpyTypeConstraint.type_check: the value is an ndarray but its dtype differs from the dtype encoded in the NumpyArray[...] typehint (e.g. float64 data under a float32 constraint), so the batch is rejected.","triggerScenarios":"Producing np.float64 arrays while the pipeline type hint is NumpyArray[np.int64, shape]; numpy inferring a wider dtype (int32->int64, float) from mixed data.","commonSituations":"Arrays built from Python lists getting numpy's default dtype; upcast arrays from arithmetic; platform-dependent default int dtype.","solutions":["Cast the batch to the declared dtype: arr.astype(expected_dtype)","Create arrays with an explicit dtype=np.int64 (or declared dtype)","Align the declared NumpyArray dtype with the dtype your data actually produces"],"exampleFix":"// before\nreturn np.asarray(elements)\n// after\nreturn np.asarray(elements, dtype=np.int64)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert np.issubdtype(arr.dtype, np.int64), f'got {arr.dtype}'","typeGuard":"import numpy as np\ndef has_dtype(arr, dtype):\n    return isinstance(arr, np.ndarray) and np.issubdtype(arr.dtype, dtype)","tryCatchPattern":"try:\n    check_batch(arr)\nexcept TypeError:\n    arr = arr.astype(expected_dtype)","preventionTips":["Create arrays with explicit dtype= parameters","Cast batches with astype before emitting","Verify arr.dtype after numpy inference from Python lists"],"tags":["python","apache-beam","numpy","dtype"],"backgroundTag":"dtype-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}