{"record":{"id":"bcd40d4de839b227","repo":"apache/beam","slug":"batch-batch-r-does-not-have-expected-shape-self-shape-r-bcd40d","errorCode":null,"errorMessage":"Batch {batch!r} does not have expected shape: {self.shape!r}","messagePattern":"Batch (.+?) does not have expected shape: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/pytorch_type_compatibility.py","lineNumber":106,"sourceCode":"    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\n      return NotImplemented\n\n    def __hash__(self) -> int:\n      return hash(self.__key())\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/pytorch_type_compatibility.py#L88-L124","documentation":"PytorchTypeConstraint.type_check checks each declared dimension of the PytorchTensor[dtype, shape] hint against the tensor's actual shape. Dimensions equal to the special placeholder N are exempt (variable batch size); every other dimension must match exactly. Beam raises this TypeError when a non-N dimension of the tensor differs from the hinted shape.","triggerScenarios":"Hinting PytorchTensor[torch.float32, (N, 128)] but emitting tensors whose second dimension is not 128 (e.g. variable-length sequences padded to different lengths), when the variable dimension is not the one marked N, or when extra/missing dimensions shift indices.","commonSituations":"Variable-length text/audio sequences padded per batch, images with differing resolutions, model input reshaping mistakes, and forgetting that only the N dimension may vary between batches.","solutions":["Pad or truncate tensors to a fixed size so every non-N dimension matches the hint (e.g. pad sequences to max_length).","Mark the truly variable dimension with N in the hint — any other varying dimension is not allowed by this check.","Fix the shape declaration to the real tensor shape (print batch.shape and update the hint accordingly).","Reshape with tensor.view(...)/reshape(...) before returning so the tensor matches the expected shape."],"exampleFix":"// before\nPytorchTensor[torch.float32, (N, 128)]  # actual tensors vary in seq_len\n// after\n# pad to a fixed length first\npadded = torch.nn.utils.rnn.pad_sequence(tensors, batch_first=True)\n# hint: PytorchTensor[torch.float32, (N, 512)] with fixed max_len=512","handlingStrategy":"validation","validationCode":"def check_shape(t: torch.Tensor, shape) -> bool:\n    return len(t.shape) == len(shape) and all(\n        s == N or t.shape[i] == s for i, s in enumerate(shape))","typeGuard":"def matches_hint(t: torch.Tensor, hint) -> bool:\n    return isinstance(t, torch.Tensor) and check_shape(t, hint.shape)","tryCatchPattern":"try:\n    yield batch\nexcept TypeError as e:\n    if 'does not have expected shape' in str(e):\n        yield pad_to_shape(batch, expected_shape)  # pad/truncate then re-emit\n    else:\n        raise","preventionTips":["Pad variable-length data to a fixed size before emitting.","Use N only for the batch dimension; all other dimensions must be static.","Print tensor.shape alongside the hint when debugging shape errors.","Keep model input dims and hint dims sourced from one shared constant."],"tags":["apache-beam","pytorch","shape","type-check"],"backgroundTag":"tensor-shape-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"}