keras-team/keras · error · TypeError

add_weight() takes at most 3 positional arguments but {len(a

Error message

add_weight() takes at most 3 positional arguments but {len(args)} were given.

What it means

Error "add_weight() takes at most 3 positional arguments but {len(args)} were given." thrown in keras-team/keras.

Source

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

                optimizer update, or string name of a built-in constraint.
                Defaults to `None`.
            aggregation: Optional string, one of `None`, `"none"`, `"mean"`,
                `"sum"` or `"only_first_replica"`. Annotates the variable with
                the type of multi-replica aggregation to be used for this
                variable when writing custom data parallel training loops.
                Defaults to `"none"`.
            overwrite_with_gradient: Boolean, whether to overwrite the variable
                with the computed gradient. This is useful for float8 training.
                Defaults to `False`.
            name: String name of the variable. Useful for debugging purposes.
        """
        self._check_super_called()
        if args:
            # `args` is only kept to detect the legacy Keras 2 call style
            # (`add_weight(shape, initializer, dtype, ...)`) and raise a clear
            # error for positional `name`.
            if len(args) > 3:
                raise TypeError(
                    "add_weight() takes at most 3 positional arguments "
                    f"but {len(args)} were given."
                )
            shape_arg = args[0]
            if isinstance(shape_arg, str):
                raise ValueError(
                    "`name` must be passed as a keyword argument. "
                    f"Received: add_weight('{shape_arg}', ...). "
                    f"Use: add_weight(shape=..., name='{shape_arg}')."
                )
            if shape is not None:
                raise ValueError(
                    "`shape` was passed both positionally and as "
                    "a keyword argument."
                )
            shape = shape_arg
            if len(args) > 1:
                if initializer is not None:

View on GitHub (pinned to 7a34a03db6)

When it happens

Trigger: Thrown at keras/src/layers/layer.py:579 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/e3d95cbadeb873ca. Report an issue: GitHub.