{"record":{"id":"ec66893124b0f621","repo":"keras-team/keras","slug":"sequential-layers-attribute-is-reserved-and-shou","errorCode":null,"errorMessage":"`Sequential.layers` attribute is reserved and should not be used. Use `add()` and `pop()` to change the layers in this model.","messagePattern":"`Sequential\\.layers` attribute is reserved and should not be used\\. Use `add\\(\\)` and `pop\\(\\)` to change the layers in this model\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"keras/src/models/sequential.py","lineNumber":269,"sourceCode":"                outputs = layer(inputs)\n            inputs = outputs\n\n            mask = tree.map_structure(backend.get_keras_mask, outputs)\n        return outputs\n\n    @property\n    def layers(self):\n        # Historically, `sequential.layers` only returns layers that were added\n        # via `add`, and omits the auto-generated `InputLayer` that comes at the\n        # bottom of the stack.\n        layers = self._layers\n        if layers and isinstance(layers[0], InputLayer):\n            return layers[1:]\n        return layers[:]\n\n    @layers.setter\n    def layers(self, _):\n        raise AttributeError(\n            \"`Sequential.layers` attribute is reserved and should not be used. \"\n            \"Use `add()` and `pop()` to change the layers in this model.\"\n        )\n\n    def compute_output_spec(self, inputs, training=None, mask=None, **kwargs):\n        if self._functional:\n            return self._functional.compute_output_spec(\n                inputs, training=training, mask=mask, **kwargs\n            )\n        # Direct application\n        for layer in self.layers:\n            outputs = layer.compute_output_spec(\n                inputs,\n                training=training,\n                **kwargs,\n            )  # Ignore mask\n            inputs = outputs\n        return outputs","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/models/sequential.py#L251-L287","documentation":"Sequential overrides the layers setter to raise AttributeError because layer management must go through add()/pop() to keep the internal functional graph and tracking consistent.","triggerScenarios":"model.layers = [l1, l2] on a keras.Sequential instance","commonSituations":"Porting PyTorch-style code or attempting layer surgery on a trained model","solutions":["Use model.add(layer) to append and model.pop() to remove","Build a new Sequential from the desired layer list","Modify _layers only if you fully own lifecycle and rebuild via _functional"],"exampleFix":"# before\nmodel.layers = [dense1, dense2]\n\n# after\nmodel = keras.Sequential([dense1, dense2])\n# or: model.add(dense1); model.add(dense2)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    model.layers = new_layers\nexcept AttributeError:\n    model = keras.Sequential(new_layers)","preventionTips":["Use model.add() and model.pop()","To replace all layers, build a new Sequential"],"tags":["keras","sequential","attribute","layers"],"backgroundTag":"reserved-attribute-assignment","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}