apache/beam · error · TypeError
Element type is not a dtype
Error message
Element type is not a dtype
What it means
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.
Source
Thrown at sdks/python/apache_beam/typehints/batch.py:192
batch_type,
element_type,
dtype,
element_shape=(),
partition_dimension=0):
super().__init__(batch_type, element_type)
self.dtype = np.dtype(dtype)
self.element_shape = element_shape
self.partition_dimension = partition_dimension
@staticmethod
@BatchConverter.register(name="numpy")
def from_typehints(element_type,
batch_type) -> Optional['NumpyBatchConverter']:
if not isinstance(element_type, NumpyTypeHint.NumpyTypeConstraint):
try:
element_type = NumpyArray[element_type, ()]
except TypeError as e:
raise TypeError("Element type is not a dtype") from e
if not isinstance(batch_type, NumpyTypeHint.NumpyTypeConstraint):
if not batch_type == np.ndarray:
raise TypeError(
"batch type must be np.ndarray or "
"beam.typehints.batch.NumpyArray[..]")
batch_type = NumpyArray[element_type.dtype, (N, )]
if not batch_type.dtype == element_type.dtype:
raise TypeError(
"batch type and element type must have equivalent dtypes "
f"(batch={batch_type.dtype}, element={element_type.dtype})")
computed_element_shape = list(batch_type.shape)
partition_dimension = computed_element_shape.index(N)
computed_element_shape.pop(partition_dimension)
if not tuple(computed_element_shape) == element_type.shape:
raise TypeError(View on GitHub (pinned to 12126d8942)
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
Example fix
// before BatchConverter.from_typehints(Union[int, str], np.ndarray) // after BatchConverter.from_typehints(np.int64, np.ndarray)
Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
try:
np.dtype(element_type)
except TypeError:
raise ValueError(f'{element_type!r} is not a numpy dtype') Type guard
def is_np_dtype(t):
try:
np.dtype(t); return True
except TypeError:
return False Try / catch
try:
conv = BatchConverter.from_typehints(elem_t, np.ndarray)
except TypeError:
conv = BatchConverter.from_typehints(elem_t, List[elem_t]) # fallback to list converter Prevention
- 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
When it happens
Trigger: 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.
Common situations: Passing beam or typing hints (e.g. Optional[int], Any) as element types to numpy batching; using structured types numpy doesn't accept.
Related errors
- batch type and element type must have equivalent dtypes (bat
- Batch {batch!r} does not have expected dtype: {self.dtype!r}
- batch type must be np.ndarray or beam.typehints.batch.NumpyA
- Failed to align batch type's batch dimension with element ty
- Batch {batch!r} is not an instance of ndarray
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/c3a8c1db02d09cb1.
Report an issue: GitHub.