apache/beam · error · TypeError
batch type must be torch.Tensor or…
Error message
batch type must be torch.Tensor or beam.typehints.pytorch_type_compatibility.PytorchTensor[..]
What it means
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.
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.
Example fix
// before
converter = PytorchBatchConverter.from_typehints(element_type=model_output, batch_type='torch.Tensor')
// after
import torch
from apache_beam.typehints import pytorch_type_compatibility as ptc
converter = PytorchBatchConverter.from_typehints(
element_type=model_output, batch_type=torch.Tensor) Defensive patterns
Strategy: validation
Validate before calling
import torch
from apache_beam.typehints.pytorch_type_compatibility import PytorchTensor
def valid_torch_batch_type(batch_type) -> bool:
return batch_type == torch.Tensor or isinstance(batch_type, type(torch.Tensor[object, ()]) if False else object)
Type guard
def is_torch_batch_type(batch_type) -> bool:
import torch
from apache_beam.typehints.pytorch_type_compatibility import PytorchTensor, PytorchTypeHint
return batch_type == torch.Tensor or isinstance(batch_type, PytorchTypeHint.PytorchTypeConstraint)
Try / catch
try:
converter = PytorchBatchConverter.from_typehints(element_type=et, batch_type=bt)
except TypeError as e:
raise ValueError(f'Unsupported torch batch type {bt!r}; use torch.Tensor or PytorchTensor[..]') from e
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- A has been supplied to the model handler, but the required…
- batch type must be pa.Table or pa.Array
- batch type must be pd.Series or pd.DataFrame
- element_size_fn must be callable
- Element type must be compatible with Beam Schemas…
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/2a51368df677852a.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/python/apache_beam/typehints/pytorch_type_compatibility.py:49
element_type,
dtype,
element_shape=(),
partition_dimension=0):
super().__init__(batch_type, element_type)
self.dtype = dtype
self.element_shape = element_shape
self.partition_dimension = partition_dimension
@staticmethod
@BatchConverter.register(name="pytorch")
def from_typehints(element_type,
batch_type) -> Optional['PytorchBatchConverter']:
if not isinstance(element_type, PytorchTypeHint.PytorchTypeConstraint):
element_type = PytorchTensor[element_type, ()]
if not isinstance(batch_type, PytorchTypeHint.PytorchTypeConstraint):
if not batch_type == torch.Tensor:
raise TypeError(
"batch type must be torch.Tensor or "
"beam.typehints.pytorch_type_compatibility.PytorchTensor[..]")
batch_type = PytorchTensor[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(
"Could not align batch type's batch dimension with element type. "
f"(batch type dimensions: {batch_type.shape}, element type "
f"dimenstions: {element_type.shape}")
return PytorchBatchConverter(View on GitHub (pinned to 12126d8942)