{"record":{"id":"6af9b09f671a65f1","repo":"keras-team/keras","slug":"you-forgot-to-call-super-init-in-the","errorCode":null,"errorMessage":"You forgot to call `super().__init__()` in the `__init__()` method. Go add it!","messagePattern":"You forgot to call `super\\(\\)\\.__init__\\(\\)` in the `__init__\\(\\)` method\\. Go add it!","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/metric.py","lineNumber":245,"sourceCode":"        return self.result()\n\n    def get_config(self):\n        \"\"\"Return the serializable config of the metric.\"\"\"\n        return {\"name\": self.name, \"dtype\": self.dtype}\n\n    @classmethod\n    def from_config(cls, config):\n        return cls(**config)\n\n    def __setattr__(self, name, value):\n        # Track Variables, Layers, Metrics\n        if hasattr(self, \"_tracker\"):\n            value = self._tracker.track(value)\n        return super().__setattr__(name, value)\n\n    def _check_super_called(self):\n        if not hasattr(self, \"_tracker\"):\n            raise RuntimeError(\n                \"You forgot to call `super().__init__()` \"\n                \"in the `__init__()` method. Go add it!\"\n            )\n\n    def __repr__(self):\n        return f\"<{self.__class__.__name__} name={self.name}>\"\n\n    def __str__(self):\n        return self.__repr__()\n","sourceCodeStart":227,"sourceCodeEnd":255,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/metric.py#L227-L255","documentation":"Keras Metric objects must call super().__init__() in their __init__ before anything else, because that call installs the _tracker used to register metric variables. add_variable() and __call__() both call _check_super_called(), which raises this RuntimeError when _tracker is missing. It almost always means a custom metric subclass skipped or deferred the parent constructor.","triggerScenarios":"Defining a custom class inheriting from keras.metrics.Metric whose __init__ does not call super().__init__() (or calls it lazily/conditionally), then calling self.add_variable(...) or invoking the metric via metric(y_true, y_pred).","commonSituations":"Porting old tf.keras custom metrics, multi-inheritance wrappers where __init__ chains break, or refactoring __init__ and accidentally removing the super() call.","solutions":["Add super().__init__(name=..., dtype=...) as the first statement of your __init__.","If using multiple inheritance, ensure keras.metrics.Metric's __init__ runs before add_variable is called.","Verify you subclass keras.metrics.Metric and no intermediate base class swallows __init__."],"exampleFix":"# before\nclass MyMetric(keras.metrics.Metric):\n    def __init__(self):\n        self.total = self.add_variable(name='total', initializer='zeros')\n\n# after\nclass MyMetric(keras.metrics.Metric):\n    def __init__(self, name='my_metric', **kwargs):\n        super().__init__(name=name, **kwargs)\n        self.total = self.add_variable(name='total', initializer='zeros')","handlingStrategy":"validation","validationCode":"class MyMetric(keras.metrics.Metric):\n    def __init__(self, **kwargs):\n        super().__init__(**kwargs)  # must be first statement","typeGuard":"def is_initialized_metric(m) -> bool:\n    import keras\n    return isinstance(m, keras.metrics.Metric) and hasattr(m, '_tracker')","tryCatchPattern":null,"preventionTips":["Make super().__init__() the first line of every custom Metric __init__.","Add a CI smoke test that instantiates and calls each custom metric once."],"tags":["keras","metrics","custom-metric","init","subclassing"],"backgroundTag":"missing-super-init-call","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}