{"record":{"id":"707fb571bcc5fe32","repo":"keras-team/keras","slug":"variable-self-path-is-already-initialized","errorCode":null,"errorMessage":"Variable {self.path} is already initialized.","messagePattern":"Variable (.+?) is already initialized\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":223,"sourceCode":"                    \"(with a `shape` and `initializer` argument).\"\n                )\n        else:\n            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}'\"","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L205-L241","documentation":"Raised by Variable._deferred_initialize when deferred (lazy) initialization is triggered but the variable already holds a concrete value. This typically happens when a variable created under a stateless scope (e.g. inside a functional call or jax.jit trace) was already initialized by a concrete forward pass, and the deferred init path then tries to initialize it again. The NNX-enabled code path above returns early in exactly this situation; with NNX disabled, Keras treats re-initialization as a bug and raises.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:223 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Avoid triggering variable creation/initialization paths twice on the same variable; create variables once (typically in build()) and reuse them.","If the variable was already initialized by a concrete call, skip manual re-initialization instead of forcing _deferred_initialize again.","On the JAX backend, enabling NNX (keras.config.set_nnx_enabled(True)) makes this case return early instead of raising.","Do not call private initialization APIs (_initialize, _deferred_initialize) on variables managed by Keras."],"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"}