keras-team/keras · error · ValueError

Invalid value for attribute `constraint`. Expected a…

Error message

Invalid value for attribute `constraint`. Expected a callable (such as a `keras.constraints.Constraint` instance) or `None`. Received: constraint={value}

What it means

Raised by the constraint property setter when the assigned value is neither None nor callable. Keras expects a keras.constraints.Constraint instance (callable) or None; raw strings or config dicts are not accepted by this guard.

Solutions

  1. Pass a Constraint instance such as variable.constraint = keras.constraints.MaxNorm(3), or None to disable.
  2. Resolve identifiers first: keras.constraints.get('max_norm') before assignment.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/variables.py:392 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/10415b9f8689f8c9. Report an issue: GitHub.

Appendix: source

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

    @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

    @property
    def constraint(self):
        return self._constraint

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

    def __repr__(self):
        value = None
        if hasattr(self, "_value") and self._value is not None:
            try:
                value = backend.core.convert_to_numpy(self._value)
            except:
                # In some cases the conversion to numpy can fail.
                pass
        value_str = f", value={value}" if value is not None else ""
        return (
            f"<Variable path={self.path}, shape={self.shape}, "
            f"dtype={self.dtype}{value_str}>"

View on GitHub (pinned to 7a34a03db6)