keras-team/keras · critical · RuntimeError

In layer '{self.__class__.__name__}', you forgot to call `su

Error message

In layer '{self.__class__.__name__}', you forgot to call `super().__init__()` as the first statement in the `__init__()` method. Go add it!

What it means

Keras 3's Layer relies on attributes set in Layer.__init__ (like _lock). If a subclass __init__ never calls super().__init__(), the _lock attribute lookup defaults to True and this RuntimeError is raised on the first API use (build, add_weight, __call__, stateless_call, get_config).

Source

Thrown at keras/src/layers/layer.py:1694

        super().__setattr__(name, value)

    def __delattr__(self, name):
        obj = getattr(self, name)
        if isinstance(obj, backend.Variable):
            import gc

            # It will take a short amount of time for the corresponding buffer
            # to be actually removed from the device.
            # https://stackoverflow.com/a/74631949
            self._untrack_variable(obj)
            super().__delattr__(name)
            gc.collect()
        else:
            super().__delattr__(name)

    def _check_super_called(self):
        if getattr(self, "_lock", True):
            raise RuntimeError(
                f"In layer '{self.__class__.__name__}', you forgot to call "
                "`super().__init__()` as the first statement "
                "in the `__init__()` method. Go add it!"
            )

    def _assert_input_compatibility(self, arg_0):
        if self.input_spec:
            try:
                input_spec.assert_input_compatibility(
                    self.input_spec, arg_0, layer_name=self.name
                )
            except SystemError:
                if backend.backend() == "torch":
                    # TODO: The torch backend failed the ONNX CI with the error:
                    # SystemError: <method '__int__' of 'torch._C.TensorBase'
                    # objects> returned a result with an exception set
                    # As a workaround, we are skipping this for now.
                    pass

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Add super().__init__(**kwargs) as the first statement of the subclass __init__
  2. In multi-inheritance, ensure Layer appears in the MRO and cooperative super() is used

Example fix

# before
class MyLayer(keras.layers.Layer):
    def __init__(self, units):
        self.units = units
# after
class MyLayer(keras.layers.Layer):
    def __init__(self, units, **kwargs):
        super().__init__(**kwargs)
        self.units = units
Defensive patterns

Strategy: validation

Validate before calling

class MyLayer(keras.layers.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)  # mandatory
        ...

Try / catch

try:
    layer(x)
except RuntimeError as e:
    if 'super().__init__()' in str(e):
        fix_init(); layer(x)

Prevention

When it happens

Trigger: Custom layer whose __init__ sets attributes but omits super().__init__(**kwargs); copied PyTorch-style classes; multiple inheritance where the MRO skips Layer.__init__.

Common situations: Writing first custom layers; porting PyTorch modules; refactoring __init__ and accidentally deleting the super call.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/cec63f2c3702ef5d. Report an issue: GitHub.