keras-team/keras · error · ValueError
The lists `inputs` and `mask` should have the same length. R
Error message
The lists `inputs` and `mask` should have the same length. Received: inputs={inputs} of length {len(inputs)}, and mask={mask} of length {len(mask)} What it means
Merge layers combine masks element-wise, so compute_mask requires len(mask) == len(inputs). This error fires when the number of masks does not match the number of input tensors.
Source
Thrown at keras/src/layers/merging/base_merge.py:265
def compute_output_spec(self, inputs):
output_shape = self.compute_output_shape([x.shape for x in inputs])
output_sparse = all(x.sparse for x in inputs)
return KerasTensor(
output_shape, dtype=self.compute_dtype, sparse=output_sparse
)
def compute_mask(self, inputs, mask=None):
if mask is None:
return None
if not isinstance(mask, (tuple, list)):
raise ValueError(f"`mask` should be a list. Received: mask={mask}")
if not isinstance(inputs, (tuple, list)):
raise ValueError(
f"`inputs` should be a list. Received: inputs={inputs}"
)
if len(mask) != len(inputs):
raise ValueError(
"The lists `inputs` and `mask` should have the same length. "
f"Received: inputs={inputs} of length {len(inputs)}, and "
f"mask={mask} of length {len(mask)}"
)
# Default implementation does an OR between the masks, which works
# for `Add`, `Subtract`, `Average`, `Maximum`, `Minimum`, `Multiply`.
if any(m is None for m in mask):
return None
output_mask = mask[0]
for m in mask[1:]:
output_mask = ops.logical_or(output_mask, m)
return output_mask
def get_config(self):
return super().get_config()
View on GitHub (pinned to 7a34a03db6)
Solutions
- Supply exactly one mask entry per input, using None for unmasked inputs: mask=[m1, None]
- Recompute mask lists whenever input arity changes
- In Functional models, rely on automatic mask propagation instead of hand-built mask lists
Example fix
# before out = merge([x1, x2], mask=[m1]) # after out = merge([x1, x2], mask=[m1, None])
Defensive patterns
Strategy: validation
Validate before calling
assert isinstance(mask, (list, tuple)) and len(mask) == len(inputs), 'one mask per input (use None)'
Type guard
def masks_match_inputs(mask, inputs) -> bool:
return mask is None or (isinstance(mask, (list, tuple)) and len(mask) == len(inputs)) Prevention
- Always emit one mask entry per input, None for unmasked
- Recompute mask lists when input arity changes
When it happens
Trigger: Calling compute_mask([x1, x2], mask=[m1]) or __call__ paths where one input's mask is missing from the list (you must pass None placeholders).
Common situations: Mixing masked and unmasked inputs (forgetting a None entry); changing the number of inputs without updating the mask list.
Related errors
- `mask` should be a list. Received: mask={mask}
- `inputs` should be a list. Received: inputs={inputs}
- The lists `inputs` and `mask` should have the same length. R
- Inputs have incompatible shapes. Received shapes {shape1} an
- A merge layer should be called on a list of inputs. Received
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/6b9fd8bfdb9f2f3f.
Report an issue: GitHub.