{"record":{"id":"6a59b114fb2acffc","repo":"keras-team/keras","slug":"only-scalar-arrays-can-be-converted-to-python-scal","errorCode":null,"errorMessage":"Only scalar arrays can be converted to Python scalars. Got: shape={self.shape}","messagePattern":"Only scalar arrays can be converted to Python scalars\\. Got: shape=(.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":430,"sourceCode":"\n    def _initialize(self, value):\n        raise NotImplementedError\n\n    def _initialize_with_initializer(self, initializer):\n        value = self._convert_to_tensor(\n            initializer(self._shape, dtype=self._dtype)\n        )\n        self._initialize(value)\n\n    def _convert_to_tensor(self, value, dtype=None):\n        raise NotImplementedError\n\n    def __getitem__(self, idx):\n        return self.value.__getitem__(idx)\n\n    def __int__(self):\n        if self.ndim > 0:\n            raise TypeError(\n                \"Only scalar arrays can be converted to Python scalars. \"\n                f\"Got: shape={self.shape}\"\n            )\n        return int(self.value)\n\n    def __float__(self):\n        if self.ndim > 0:\n            raise TypeError(\n                \"Only scalar arrays can be converted to Python scalars. \"\n                f\"Got: shape={self.shape}\"\n            )\n        return float(self.value)\n\n    def __array__(self, dtype=None):\n        # We can't directly use self.value.__array__ here because of scalar.\n        # Numpy require this method to return as array like object. In the case\n        # of scalar, it will fail the type checking from numpy. We need to\n        # return a 0d array via numpy.","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L412-L448","documentation":"Raised by Variable.__int__ when int() is called on a non-scalar variable (ndim > 0). A variable holding a tensor with one or more dimensions has no unique integer representation, so the conversion is rejected before delegating to the backend value.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:430 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Extract a scalar element first, e.g. int(variable[0]) or index the position you need.","If the variable should hold a scalar, create/assign it with a 0-d value.","Reduce the underlying tensor first: int(keras.ops.sum(variable.value))."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}