{"record":{"id":"22f762b3d24d2a8b","repo":"rohitg00/ai-engineering-from-scratch","slug":"expected-len-self-shape-indices-got-len-indi","errorCode":null,"errorMessage":"Expected {len(self._shape)} indices, got {len(indices)}","messagePattern":"Expected (.+?) indices, got (.+?)","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/12-tensor-operations/code/tensors.py","lineNumber":74,"sourceCode":"    @property\n    def shape(self):\n        return self._shape\n\n    @property\n    def rank(self):\n        return len(self._shape)\n\n    @property\n    def size(self):\n        return len(self._data)\n\n    @property\n    def strides(self):\n        return self._strides\n\n    def _flat_index(self, indices):\n        if len(indices) != len(self._shape):\n            raise IndexError(\n                f\"Expected {len(self._shape)} indices, got {len(indices)}\"\n            )\n        idx = 0\n        for i, (ind, stride) in enumerate(zip(indices, self._strides)):\n            if ind < 0 or ind >= self._shape[i]:\n                raise IndexError(\n                    f\"Index {ind} out of range for axis {i} with size {self._shape[i]}\"\n                )\n            idx += ind * stride\n        return idx\n\n    def __getitem__(self, indices):\n        if not isinstance(indices, tuple):\n            indices = (indices,)\n        if len(indices) == len(self._shape):\n            return self._data[self._flat_index(indices)]\n        raise IndexError(\"Partial indexing not supported in this basic implementation\")\n","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/12-tensor-operations/code/tensors.py#L56-L92","documentation":"Raised in Tensor._flat_index when the number of indices passed to __getitem__/__setitem__ does not equal the tensor's rank. Full indexing requires one integer per axis in this basic implementation; too few or too many indices cannot be resolved to a single flat offset.","triggerScenarios":"Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:74 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Supply exactly rank indices, e.g. t[0, 1] for a 2-D tensor and t[0] for a 1-D one","Check tensor.rank before indexing in generic code","Add slicing/partial-index support if your use case needs sub-tensor views"],"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"}