keras-team/keras · error · ValueError
`dtype` was passed both positionally and as a keyword argume
Error message
`dtype` was passed both positionally and as a keyword argument.
What it means
Layer.add_weight() in Keras 3 rejects calls where dtype is supplied both positionally (the 3rd positional argument) and via the dtype= keyword. Positional args map to (shape, initializer, dtype), so any overlap with the corresponding keyword raises this ValueError.
Source
Thrown at keras/src/layers/layer.py:605
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:
if dtype is not None:
raise ValueError(
"`dtype` was passed both positionally and as a "
"keyword argument."
)
dtype = args[2]
if shape is None:
shape = ()
if dtype is not None:
dtype = backend.standardize_dtype(dtype)
else:
dtype = self.variable_dtype
if initializer is None:
if "float" in dtype:
initializer = "glorot_uniform"
else:
initializer = "zeros"
initializer = initializers.get(initializer)
with backend.name_scope(self.name, caller=self):
variable = backend.Variable(View on GitHub (pinned to 7a34a03db6)
Solutions
- Drop one of the two dtype specifications and pass all arguments as keywords
- Audit the layer for other positional/keyword overlaps (shape, initializer) since they raise sibling errors
Example fix
# before self.add_weight((64, 64), 'zeros', 'float32', dtype='float32') # after self.add_weight(shape=(64, 64), initializer='zeros', dtype='float32')
Defensive patterns
Strategy: validation
Validate before calling
# keep add_weight calls keyword-only: layer.add_weight(shape=s, initializer=i, dtype=d)
Prevention
- Adopt the Keras 3 keyword convention in CI via a code-style check
- Never mix legacy positional calls with edited keyword arguments
When it happens
Trigger: add_weight(shape, init, 'float32', dtype='float32') — the 3rd positional collides with the dtype keyword. Typical when Keras 2 code that passed dtype positionally is edited to add dtype= for mixed precision.
Common situations: Migrating mixed-precision Keras 2 layers; editing generated add_weight calls; custom layers whose signature changed across Keras versions.
Related errors
- `initializer` was passed both positionally and as a keyword
- Only input tensors may be passed as positional arguments. Th
- `add_loss()` can only be called from inside `build()` or `ca
- Quantization mode='{mode}' doesn't work well with compute_dt
- Feature '{name}' has `output_mode='one_hot'`. Thus its prepr
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/23fbfd372aae6a35.
Report an issue: GitHub.