keras-team/keras · error · ValueError

`mask` should be a list. Received: mask={mask}

Error message

`mask` should be a list. Received: mask={mask}

What it means

Merge.compute_mask() requires that when a mask is provided it is a list/tuple parallel to the inputs. This error fires when mask is a single mask tensor (or any non-sequence) while the layer has multiple inputs.

Source

Thrown at keras/src/layers/merging/base_merge.py:259

        batch_sizes = {s[0] for s in input_shape if s is not None} - {None}
        if len(batch_sizes) == 1:
            output_shape = (list(batch_sizes)[0],) + output_shape
        else:
            output_shape = (None,) + output_shape
        return output_shape

    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

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass mask as a list parallel to inputs: merge([x1, x2], mask=[m1, m2])
  2. Use None entries where an input has no mask: mask=[m1, None]
  3. Ensure custom layers wrap masks into lists before delegating to merge compute_mask

Example fix

# before
out = merge([x1, x2], mask=m)

# after
out = merge([x1, x2], mask=[m, m])
Defensive patterns

Strategy: validation

Validate before calling

assert mask is None or isinstance(mask, (list, tuple)), 'mask must be a list parallel to inputs'

Type guard

def is_mask_list(mask) -> bool:
    return mask is None or isinstance(mask, (list, tuple))

Prevention

When it happens

Trigger: Calling a merge layer with mask=single_tensor on multi-input merge layers; custom layers overriding compute_mask and passing a bare mask upstream.

Common situations: Masking (variable-length sequences) feeding a merge layer; passing an Embedding(mask_zero=True) output mask directly instead of as a list.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/eb750866a16bcd79. Report an issue: GitHub.