keras-team/keras · error · ValueError
mask_t is expected to be tensor, but got
Error message
mask_t is expected to be tensor, but got {mask_t} What it means
Raised inside the RNN mask-broadcasting helper _expand_mask (used by the deprecated backend.rnn) when the mask argument is a nested structure (list/tuple/dict) instead of a single tensor. Broadcasting a nested mask against an input tensor is undefined, so the helper rejects it before expanding dims.
Solutions
- Flatten the mask structure and pass a single tensor mask per RNN call
- If you have multiple masks, call the RNN per sequence or combine masks into one tensor (e.g. via logical AND) before passing it
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/legacy/backend.py:1473 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/f206146c4f705ef1.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/legacy/backend.py:1473
if mask.dtype != tf.bool:
mask = tf.cast(mask, tf.bool)
if len(mask.shape) == 2:
mask = expand_dims(mask)
if not time_major:
mask = swap_batch_timestep(mask)
if constants is None:
constants = []
# tf.where needs its condition tensor to be the same shape as its two
# result tensors, but in our case the condition (mask) tensor is
# (nsamples, 1), and inputs are (nsamples, ndimensions) or even more.
# So we need to broadcast the mask to match the shape of inputs.
# That's what the tile call does, it just repeats the mask along its
# second dimension n times.
def _expand_mask(mask_t, input_t, fixed_dim=1):
if tf.nest.is_nested(mask_t):
raise ValueError(
f"mask_t is expected to be tensor, but got {mask_t}"
)
if tf.nest.is_nested(input_t):
raise ValueError(
f"input_t is expected to be tensor, but got {input_t}"
)
rank_diff = len(input_t.shape) - len(mask_t.shape)
for _ in range(rank_diff):
mask_t = tf.expand_dims(mask_t, -1)
multiples = [1] * fixed_dim + input_t.shape.as_list()[fixed_dim:]
return tf.tile(mask_t, multiples)
if unroll:
if not time_steps:
raise ValueError("Unrolling requires a fixed number of timesteps.")
states = tuple(initial_states)
successive_states = []
successive_outputs = []View on GitHub (pinned to 7a34a03db6)