{"record":{"id":"a106f0b2000a6f3b","repo":"keras-team/keras","slug":"you-are-attempting-to-initialize-a-variable-while","errorCode":null,"errorMessage":"You are attempting to initialize a variable while in a stateless scope. This is disallowed. Make sure that all variables are initialized before you start using your layer/model objects.","messagePattern":"You are attempting to initialize a variable while in a stateless scope\\. This is disallowed\\. Make sure that all variables are initialized before you start using your layer/model objects\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":226,"sourceCode":"            if callable(initializer):\n                self._shape = self._validate_shape(shape)\n                self._initialize_with_initializer(initializer)\n            else:\n                self._initialize(initializer)\n                self._shape = self._validate_shape(self._value.shape)\n        self._ndim = len(self._shape)\n\n    def _deferred_initialize(self):\n        if self._value is not None:\n            # If NNX is enabled, it's possible the variable was already\n            # initialized by a concrete call. In this case, _deferred_initialize\n            # returns early and does not raise an error.\n            if config.is_nnx_enabled():\n                return\n            raise ValueError(f\"Variable {self.path} is already initialized.\")\n\n        if in_stateless_scope():\n            raise ValueError(\n                \"You are attempting to initialize a variable \"\n                \"while in a stateless scope. This is disallowed. \"\n                \"Make sure that all variables are initialized \"\n                \"before you start using your layer/model objects.\"\n            )\n        self._initialize_with_initializer(self._initializer)\n        self._initializer = None\n\n    def _validate_shape(self, shape):\n        shape = standardize_shape(shape)\n        if None in shape:\n            raise ValueError(\n                \"Shapes used to initialize variables must be \"\n                \"fully-defined (no `None` dimensions). Received: \"\n                f\"shape={shape} for variable path='{self.path}'\"\n            )\n        return shape\n","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L208-L244","documentation":"Error \"You are attempting to initialize a variable while in a stateless scope. This is disallowed. Make sure that all variables are initialized before you start using your layer/model objects.\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:226 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}