keras-team/keras · error · ValueError
Only input tensors may be passed as positional arguments. Th
Error message
Only input tensors may be passed as positional arguments. The following argument value should be passed as a keyword argument: {arg} (of type {type(arg)}) What it means
When calling a layer, only backend tensors (or None/symbolic tensors) may be passed as positional arguments; everything else must be a keyword argument. This guard catches e.g. passing an int or a Python object positionally where only inputs belong.
Source
Thrown at keras/src/layers/layer.py:908
backend.set_keras_mask(y, mask)
return y
# Used to avoid expensive `tree` operations in the most common case.
if (
kwargs
or len(args) != 1
or not is_backend_tensor_or_symbolic(args[0], allow_none=False)
or backend.standardize_dtype(args[0].dtype) != self.input_dtype
) and self._convert_input_args:
args = tree.map_structure(maybe_convert, args)
kwargs = tree.map_structure(maybe_convert, kwargs)
##########################################################
# 2. Enforce that only tensors can be passed positionally.
if not self._allow_non_tensor_positional_args:
for arg in tree.flatten(args):
if not is_backend_tensor_or_symbolic(arg, allow_none=True):
raise ValueError(
"Only input tensors may be passed as "
"positional arguments. The following argument value "
f"should be passed as a keyword argument: {arg} "
f"(of type {type(arg)})"
)
# Caches info about `call()` signature, args, kwargs.
call_spec = CallSpec(
self._call_signature, self._call_context_args, args, kwargs
)
############################################
# 3. Check input spec for 1st positional arg.
# TODO: consider extending this to all args and kwargs.
self._assert_input_compatibility(call_spec.first_arg)
################
# 4. Call buildView on GitHub (pinned to 7a34a03db6)
Solutions
- Pass non-tensor arguments as keywords: layer(x, training=True, mask=m)
- If your custom layer legitimately takes non-tensor positionals, set self._allow_non_tensor_positional_args = True in __init__
Example fix
# before out = layer(x, mask) # after out = layer(x, mask=mask)
Defensive patterns
Strategy: validation
Validate before calling
assert all(keras.ops.is_tensor(a) for a in tree.flatten(args)), 'pass non-tensors as kwargs'
Prevention
- Always pass training, mask, and options as keyword arguments
- Set _allow_non_tensor_positional_args=True only for layers that genuinely need them
When it happens
Trigger: layer(x, training) instead of layer(x, training=training); layer(x, mask, True); custom layers called with config objects positionally when _allow_non_tensor_positional_args is False.
Common situations: Converting Keras 2 call signatures where mask was a valid 2nd positional; refactoring call() to accept new non-tensor options; passing Python bools/ints positionally.
Related errors
- `add_loss()` can only be called from inside `build()` or `ca
- `initializer` was passed both positionally and as a keyword
- `dtype` was passed both positionally and as a keyword argume
- `Sequential.layers` attribute is reserved and should not be
- Unknown activation function '{activation}' cannot be seriali
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/cf0b431fae13c4c8.
Report an issue: GitHub.