{"record":{"id":"9c71f69931ec6b28","repo":"rohitg00/ai-engineering-from-scratch","slug":"inconsistent-shapes-at-index-i-s-vs-sub-shap","errorCode":null,"errorMessage":"Inconsistent shapes at index {i}: {s} vs {sub_shape}","messagePattern":"Inconsistent shapes at index (.+?): (.+?) vs (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/12-tensor-operations/code/tensors.py","lineNumber":37,"sourceCode":"            if total != len(self._data):\n                raise ValueError(\n                    f\"Cannot reshape {len(self._data)} elements into shape {shape}\"\n                )\n            self._shape = tuple(shape)\n\n        self._strides = self._compute_strides(self._shape)\n\n    def _flatten_nested(self, data):\n        if not isinstance(data, (list, tuple)):\n            return [data], ()\n        if len(data) == 0:\n            return [], (0,)\n\n        sub_results = [self._flatten_nested(item) for item in data]\n        sub_shape = sub_results[0][1]\n        for i, (_, s) in enumerate(sub_results):\n            if s != sub_shape:\n                raise ValueError(\n                    f\"Inconsistent shapes at index {i}: {s} vs {sub_shape}\"\n                )\n\n        flat = []\n        for sub_data, _ in sub_results:\n            flat.extend(sub_data)\n\n        return flat, (len(data),) + sub_shape\n\n    @staticmethod\n    def _compute_strides(shape):\n        if len(shape) == 0:\n            return ()\n        strides = [1] * len(shape)\n        for i in range(len(shape) - 2, -1, -1):\n            strides[i] = strides[i + 1] * shape[i + 1]\n        return tuple(strides)\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/12-tensor-operations/code/tensors.py#L19-L55","documentation":"Raised in Tensor._flatten_nested when sibling sublists at one nesting level report different shapes, i.e. the input is ragged rather than a regular rectangular nested sequence. A tensor requires uniform extents on every axis; the index of the first mismatching element and the two conflicting shapes are reported.","triggerScenarios":"Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:37 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pad or trim the ragged rows so every sublist at the same depth has equal length","Fix the data producer that emitted unequal-length rows (common when mapping over variable-length inputs)","Validate regularity before constructing the Tensor and raise a clearer domain error"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}