{"record":{"id":"c3a8c1db02d09cb1","repo":"apache/beam","slug":"element-type-is-not-a-dtype","errorCode":null,"errorMessage":"Element type is not a dtype","messagePattern":"Element type is not a dtype","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/batch.py","lineNumber":192,"sourceCode":"      batch_type,\n      element_type,\n      dtype,\n      element_shape=(),\n      partition_dimension=0):\n    super().__init__(batch_type, element_type)\n    self.dtype = np.dtype(dtype)\n    self.element_shape = element_shape\n    self.partition_dimension = partition_dimension\n\n  @staticmethod\n  @BatchConverter.register(name=\"numpy\")\n  def from_typehints(element_type,\n                     batch_type) -> Optional['NumpyBatchConverter']:\n    if not isinstance(element_type, NumpyTypeHint.NumpyTypeConstraint):\n      try:\n        element_type = NumpyArray[element_type, ()]\n      except TypeError as e:\n        raise TypeError(\"Element type is not a dtype\") from e\n\n    if not isinstance(batch_type, NumpyTypeHint.NumpyTypeConstraint):\n      if not batch_type == np.ndarray:\n        raise TypeError(\n            \"batch type must be np.ndarray or \"\n            \"beam.typehints.batch.NumpyArray[..]\")\n      batch_type = NumpyArray[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\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(","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/batch.py#L174-L210","documentation":"NumpyBatchConverter.from_typehints requires the element_type to be representable as a numpy dtype. If wrapping element_type as NumpyArray[element_type, ()] raises TypeError, the element type is not a valid numpy dtype and this error is raised.","triggerScenarios":"from_typehints with an element type like Union[int, str], Dict[str, int], or a custom Python class that numpy cannot interpret as a dtype.","commonSituations":"Passing beam or typing hints (e.g. Optional[int], Any) as element types to numpy batching; using structured types numpy doesn't accept.","solutions":["Use a numpy-compatible scalar element type such as np.int64, np.float64, or str","Convert your element type to a registered numpy dtype before calling","Use a different BatchConverter (e.g. list) for non-dtype element types"],"exampleFix":"// before\nBatchConverter.from_typehints(Union[int, str], np.ndarray)\n// after\nBatchConverter.from_typehints(np.int64, np.ndarray)","handlingStrategy":"type-guard","validationCode":"import numpy as np\ntry:\n    np.dtype(element_type)\nexcept TypeError:\n    raise ValueError(f'{element_type!r} is not a numpy dtype')","typeGuard":"def is_np_dtype(t):\n    try:\n        np.dtype(t); return True\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    conv = BatchConverter.from_typehints(elem_t, np.ndarray)\nexcept TypeError:\n    conv = BatchConverter.from_typehints(elem_t, List[elem_t])  # fallback to list converter","preventionTips":["Restrict numpy batching to scalar numpy-dtype element types","Check np.dtype(element_type) up front","Fall back to the list converter for non-dtype types"],"tags":["python","apache-beam","numpy","dtype"],"backgroundTag":"unsupported-dtype","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"}