keras-team/keras · error · ValueError
`name` must be passed as a keyword argument. Received…
Error message
`name` must be passed as a keyword argument. Received: add_weight('{shape_arg}', ...). Use: add_weight(shape=..., name='{shape_arg}'). What it means
Detects the classic Keras 2 mistake of passing the weight's name as the first positional argument: add_weight('dense_kernel', ...). In Keras 3 the first positional slot would be `shape`, so a string there is interpreted as a misplaced name and rejected.
Solutions
- Use keyword arguments: add_weight(shape=..., name='dense_kernel', ...)
- If the string was intended as a shape description, convert it to a tuple of ints, e.g. shape=(3, 4)
- Fix custom layers copied from Keras 2 examples that called add_weight(name, shape, ...) positionally
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/layers/layer.py:585 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/833ad4c3d68ddd00.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/layer.py:585
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:
raise ValueError(
"`initializer` was passed both positionally and "
"as a keyword argument."
)
initializer = args[1]
if len(args) > 2:View on GitHub (pinned to 7a34a03db6)