keras-team/keras · error · ValueError

Invalid padding: {padding}

Error message

Invalid padding: {padding}

What it means

The deprecated backend's convolution/pooling helpers normalize the padding string via _preprocess_padding, which accepts only 'same' and 'valid' (mapped to TF's 'SAME'/'VALID'). Any other string — including uppercase 'SAME', 'causal' in functions that don't special-case it, or 'full' — raises this error before the TF op is built.

Source

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

        else:
            tf_data_format = "NCHW"
    return x, tf_data_format


def _preprocess_conv3d_input(x, data_format):
    tf_data_format = "NDHWC"
    if data_format == "channels_first":
        tf_data_format = "NCDHW"
    return x, tf_data_format


def _preprocess_padding(padding):
    if padding == "same":
        padding = "SAME"
    elif padding == "valid":
        padding = "VALID"
    else:
        raise ValueError(f"Invalid padding: {padding}")
    return padding


@keras_export("keras._legacy.backend.conv1d")
def conv1d(
    x, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
):
    """DEPRECATED."""
    if data_format is None:
        data_format = backend.image_data_format()
    if data_format not in {"channels_first", "channels_last"}:
        raise ValueError(f"Unknown data_format: {data_format}")

    kernel_shape = kernel.shape.as_list()
    if padding == "causal":
        # causal (dilated) convolution:
        left_pad = dilation_rate * (kernel_shape[0] - 1)
        x = temporal_padding(x, (left_pad, 0))

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use lowercase 'same' or 'valid'
  2. If you need 'causal' behavior, use conv1d (which special-cases it) or pad manually with temporal_padding before a 'valid' conv
  3. Validate/normalize padding strings at your own API boundary before forwarding them

Example fix

// before
out = K.conv2d(x, k, padding='SAME')

// after
out = K.conv2d(x, k, padding='same')
Defensive patterns

Strategy: type-guard

Validate before calling

assert padding in ('same', 'valid'), f'padding must be same|valid, got {padding!r}'

Type guard

def is_valid_padding(p) -> bool:
    return p in {'same', 'valid'}

Try / catch

except ValueError as e:
    if 'padding' in str(e):
        p = p.lower()
        out = K.conv2d(x, k, padding='same' if p == 'same' else 'valid')
    else:
        raise

Prevention

When it happens

Trigger: Calling conv2d/conv1d/conv3d/depthwise_conv2d/pool2d/conv2d_transpose on keras._legacy.backend with padding='SAME', 'Full', 'reflect', or a typo like 'vaild'.

Common situations: Code copied from raw TensorFlow examples using uppercase 'SAME'/'VALID'; passing a generic padding knob from a higher-level API straight through to the legacy backend; forgetting that 'causal' is only supported by conv1d.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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