keras-team/keras · error · ValueError

Expected `padding` to be a tuple of 2 integers. Received: pa

Error message

Expected `padding` to be a tuple of 2 integers. Received: padding={padding}

What it means

The deprecated temporal_padding helper pads only the time axis (axis 1) of a 3D (batch, timesteps, features) tensor and requires `padding` to be exactly two integers: (left_pad, right_pad). Passing a nested tuple, a single int, a 3+-element tuple, or None raises this ValueError immediately.

Source

Thrown at keras/src/legacy/backend.py:2181

            tile_shape = tf.where(
                shape_diff > 0, expr_shape, tf.ones_like(expr_shape)
            )
            condition = tf.tile(condition, tile_shape)
        x = tf.where(condition, then_expression, else_expression)
    return x


@keras_export("keras._legacy.backend.tanh")
def tanh(x):
    """DEPRECATED."""
    return tf.tanh(x)


@keras_export("keras._legacy.backend.temporal_padding")
def temporal_padding(x, padding=(1, 1)):
    """DEPRECATED."""
    if len(padding) != 2:
        raise ValueError(
            "Expected `padding` to be a tuple of 2 integers. "
            f"Received: padding={padding}"
        )
    pattern = [[0, 0], [padding[0], padding[1]], [0, 0]]
    return tf.compat.v1.pad(x, pattern)


@keras_export("keras._legacy.backend.tile")
def tile(x, n):
    """DEPRECATED."""
    if isinstance(n, int):
        n = [n]
    return tf.tile(x, n)


@keras_export("keras._legacy.backend.to_dense")
def to_dense(tensor):
    """DEPRECATED."""

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass exactly two ints, e.g. temporal_padding(x, padding=(2, 2)) — the default (1,1) is often fine
  2. For a single int k, expand it to (k, k)
  3. Prefer the modern keras.layers.ZeroPadding1D(padding=k), which accepts an int or a pair

Example fix

# before
temporal_padding(x, padding=2)
# after
temporal_padding(x, padding=(2, 2))  # or: keras.layers.ZeroPadding1D(2)(x)
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(padding, int):
    padding = (padding, padding)
assert isinstance(padding, (tuple, list)) and len(padding) == 2, f'bad padding: {padding}'

Type guard

def is_temporal_padding(p) -> bool:
    return isinstance(p, (tuple, list)) and len(p) == 2 and all(isinstance(v, int) for v in p)

Prevention

When it happens

Trigger: Calling keras._legacy.backend.temporal_padding(x, padding=1), padding=((1,1),(1,1)), padding=(1,1,1), or any non-length-2 argument.

Common situations: Migrating old Keras 1/2 code where padding semantics differed; copy-pasting a spatial padding argument into a temporal call; loading a saved config where the padding tuple was serialized differently.

Related errors


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