keras-team/keras · error · TypeError

`overwrite_with_gradient` must be a boolean. Received

Error message

`overwrite_with_gradient` must be a boolean. Received: {value}

What it means

Raised by the overwrite_with_gradient property setter when a non-bool value is assigned. This flag controls a special gradient mode (e.g. float8 training, where scale/amax_history variables are overwritten directly by their computed gradients) and must be strictly True or False; the isinstance check in the setter rejects anything else.

Solutions

  1. Assign a Python bool: variable.overwrite_with_gradient = True or False.
  2. If the value arrives from config/JSON, convert it with bool(value) before assigning.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at keras/src/backend/common/variables.py:365 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/backend/common/variables.py:365

        return self._path

    @property
    def overwrite_with_gradient(self):
        """Whether this variable should be overwritten by the gradient.

        This property is designed for a special case where we want to overwrite
        the variable directly with its computed gradient. For example, in float8
        training, new `scale` and `amax_history` are computed as gradients, and
        we want to overwrite them directly instead of following the typical
        procedure such as gradient descent with a learning rate, gradient
        clipping and weight decaying.
        """
        return self._overwrite_with_gradient

    @overwrite_with_gradient.setter
    def overwrite_with_gradient(self, value):
        if not isinstance(value, bool):
            raise TypeError(
                "`overwrite_with_gradient` must be a boolean. "
                f"Received: {value}"
            )
        self._overwrite_with_gradient = value

    @property
    def regularizer(self):
        return self._regularizer

    @regularizer.setter
    def regularizer(self, value):
        if value is not None and not callable(value):
            raise ValueError(
                "Invalid value for attribute `regularizer`. Expected a "
                "callable (such as a `keras.regularizers.Regularizer` "
                f"instance) or `None`. Received: regularizer={value}"
            )
        self._regularizer = value

View on GitHub (pinned to 7a34a03db6)