{"record":{"id":"64c61d148dded601","repo":"keras-team/keras","slug":"you-are-attempting-to-create-a-variable-while-in-a","errorCode":null,"errorMessage":"You are attempting to create a variable while in a stateless scope. This is disallowed. Make sure that all variables are created before you start using your layer/model objects.\n\nIn some cases, you might be seeing this error because you need to implement a `def build(self, input_shape)` method on your layer/model, which will create its variables.\n\nIn some other cases, you might be seeing this error because you are instantiating a `Variable` and assigning it to a layer without going through self.add_variable()/self.add_weight(). Always prefer using these methods (with a `shape` and `initializer` argument).","messagePattern":"You are attempting to create a variable while in a stateless scope\\. This is disallowed\\. Make sure that all variables are created before you start using your layer/model objects\\.\n\nIn some cases, you might be seeing this error because you need to implement a `def build\\(self, input_shape\\)` method on your layer/model, which will create its variables\\.\n\nIn some other cases, you might be seeing this error because you are instantiating a `Variable` and assigning it to a layer without going through self\\.add_variable\\(\\)/self\\.add_weight\\(\\)\\. Always prefer using these methods \\(with a `shape` and `initializer` argument\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":190,"sourceCode":"                    \"the `shape` argument should be specified. \"\n                    f\"Received: initializer={initializer} \"\n                    f\"and shape={shape}\"\n                )\n        else:\n            initializer = self._convert_to_tensor(initializer, dtype=dtype)\n            # If dtype is None and `initializer` is an array, use its dtype.\n            if dtype is None:\n                dtype = initializer.dtype\n        self._dtype = standardize_dtype(dtype)\n\n        if in_stateless_scope():\n            if callable(initializer):\n                self._value = None\n                self._initializer = initializer\n                self._shape = self._validate_shape(shape)\n                register_uninitialized_variable(self)\n            else:\n                raise ValueError(\n                    \"You are attempting to create a variable \"\n                    \"while in a stateless scope. This is disallowed. \"\n                    \"Make sure that all variables are created \"\n                    \"before you start using your layer/model objects.\\n\\n\"\n                    \"In some cases, you might be seeing this error \"\n                    \"because you need to \"\n                    \"implement a `def build(self, input_shape)` method \"\n                    \"on your layer/model, which will \"\n                    \"create its variables.\\n\\n\"\n                    \"In some other cases, you might be seeing this error \"\n                    \"because you are instantiating a `Variable` and \"\n                    \"assigning it to a layer without going through \"\n                    \"self.add_variable()/self.add_weight(). Always prefer \"\n                    \"using these methods \"\n                    \"(with a `shape` and `initializer` argument).\"\n                )\n        else:\n            if callable(initializer):","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L172-L208","documentation":"Error \"You are attempting to create a variable while in a stateless scope. This is disallowed. Make sure that all variables are created before you start using your layer/model objects.\n\nIn some cases, you might be seeing this error because you need to implement a `def build(self, input_shape)` method on your layer/model, which will create its variables.\n\nIn some other cases, you might be seeing this error because you are instantiating a `Variable` and assigning it to a layer without going through self.add_variable()/self.add_weight(). Always prefer using these methods (with a `shape` and `initializer` argument).\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:190 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"}