{"record":{"id":"93e2fabaa32be900","repo":"keras-team/keras","slug":"layer-self-name-was-never-built-and-thus-it-do-93e2fa","errorCode":null,"errorMessage":"Layer '{self.name}' was never built and thus it doesn't have any variables. However the weights file lists {len(store.keys())} variables for this layer.\nIn most cases, this error indicates that either:\n\n1. The layer is owned by a parent layer that implements a `build()` method, but calling the parent's `build()` method did NOT create the state of the child layer '{self.name}'. A `build()` method must create ALL state for the layer, including the state of any children layers.\n\n2. You need to implement the `def build_from_config(self, config)` method on layer '{self.name}', to specify how to rebuild it during loading. In this case, you might also want to implement the method that generates the build config at saving time, `def get_build_config(self)`. The method `build_from_config()` is meant to create the state of the layer (i.e. its variables) upon deserialization.","messagePattern":"Layer '\\{self\\.name\\}' was never built and thus it doesn't have any variables\\. However the weights file lists \\{len\\(store\\.keys\\(\\)\\)\\} variables for this layer\\.\nIn most cases, this error indicates that either:\n\n1\\. The layer is owned by a parent layer that implements a `build\\(\\)` method, but calling the parent's `build\\(\\)` method did NOT create the state of the child layer '\\{self\\.name\\}'\\. A `build\\(\\)` method must create ALL state for the layer, including the state of any children layers\\.\n\n2\\. You need to implement the `def build_from_config\\(self, config\\)` method on layer '\\{self\\.name\\}', to specify how to rebuild it during loading\\. In this case, you might also want to implement the method that generates the build config at saving time, `def get_build_config\\(self\\)`\\. The method `build_from_config\\(\\)` is meant to create the state of the layer \\(i\\.e\\. its variables\\) upon deserialization\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1477,"sourceCode":"\n    def save_own_variables(self, store):\n        \"\"\"Saves the state of the layer.\n\n        You can override this method to take full control of how the state of\n        the layer is saved upon calling `model.save()`.\n\n        Args:\n            store: Dict where the state of the model will be saved.\n        \"\"\"\n        all_vars = self._trainable_variables + self._non_trainable_variables\n        for i, v in enumerate(all_vars):\n            store[f\"{i}\"] = v\n\n    def _check_load_own_variables(self, store):\n        all_vars = self._trainable_variables + self._non_trainable_variables\n        if len(store.keys()) != len(all_vars):\n            if len(all_vars) == 0 and not self.built:\n                raise ValueError(\n                    f\"Layer '{self.name}' was never built \"\n                    \"and thus it doesn't have any variables. \"\n                    f\"However the weights file lists {len(store.keys())} \"\n                    \"variables for this layer.\\n\"\n                    \"In most cases, this error indicates that either:\\n\\n\"\n                    \"1. The layer is owned by a parent layer that \"\n                    \"implements a `build()` method, but calling the \"\n                    \"parent's `build()` method did NOT create the state of \"\n                    f\"the child layer '{self.name}'. A `build()` method \"\n                    \"must create ALL state for the layer, including \"\n                    \"the state of any children layers.\\n\\n\"\n                    \"2. You need to implement \"\n                    \"the `def build_from_config(self, config)` method \"\n                    f\"on layer '{self.name}', to specify how to rebuild \"\n                    \"it during loading. \"\n                    \"In this case, you might also want to implement the \"\n                    \"method that generates the build config at saving time, \"\n                    \"`def get_build_config(self)`. \"","sourceCodeStart":1459,"sourceCodeEnd":1495,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1459-L1495","documentation":"When loading a weights file, the layer has zero variables because it was never built, yet the file lists variables for it. Keras explains the two usual causes: a parent's build() did not create the child layer's state, or the layer lacks build_from_config()/get_build_config() so loading cannot reconstruct its state.","triggerScenarios":"Loading a saved model where a custom parent layer's build() instantiates children lazily (e.g. only creates them in call()); custom layers overriding build but relying on call-time construction; deserializing layers without build config.","commonSituations":"Keras 3 custom subclassed models with nested layers; models saved after Keras upgrades; layers whose build depends on data-dependent logic.","solutions":["In the parent's build(), ensure every child layer's build() is invoked (or children created in __init__) so their variables exist","Implement get_build_config() and build_from_config(self, config) on custom layers so loading rebuilds state","Create child layers in __init__ rather than lazily inside call()"],"exampleFix":"# before\nclass Parent(keras.layers.Layer):\n    def call(self, x):\n        if not hasattr(self, 'dense'):\n            self.dense = keras.layers.Dense(4)(x)\n        return self.dense(x)\n# after\nclass Parent(keras.layers.Layer):\n    def build(self, input_shape):\n        self.dense = keras.layers.Dense(4)\n        self.dense.build(input_shape)\n    def call(self, x):\n        return self.dense(x)","handlingStrategy":"validation","validationCode":"assert layer.built or not saving, f'{layer.name} unbuilt while saving'","typeGuard":null,"tryCatchPattern":"try:\n    keras.saving.load_model(path)\nexcept ValueError as e:\n    if 'never built' in str(e):\n        fix_parent_build(); keras.saving.load_model(path)","preventionTips":["Create child layers in __init__ or parent build(), never lazily in call()","Implement get_build_config/build_from_config on custom layers"],"tags":["keras","saving","loading","custom-layers","build"],"backgroundTag":"model-loading-build-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}