keras-team/keras · error · ValueError

`sequence_stride` must be a positive integer. Received: sequ

Error message

`sequence_stride` must be a positive integer. Received: sequence_stride={sequence_stride}

What it means

keras.ops.stft validates sequence_stride eagerly: it must be a Python int and strictly positive. Non-int types (numpy integer, float, tensor, None) or values <= 0 raise immediately, before any backend call.

Source

Thrown at keras/src/ops/math.py:990

            centered at time `t * sequence_stride`. Otherwise, the t-th sequence
            begins at time `t * sequence_stride`. Defaults to `True`.

    Returns:
        A tuple containing two tensors - the real and imaginary parts of the
        STFT output.

    Example:

    >>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
    >>> stft(x, 3, 2, 3)
    (array([[0.75, -0.375],
       [3.75, -1.875],
       [5.25, -2.625]]), array([[0.0, 0.64951905],
       [0.0, 0.64951905],
       [0.0, -0.64951905]]))
    """
    if not isinstance(sequence_stride, int) or sequence_stride <= 0:
        raise ValueError(
            "`sequence_stride` must be a positive integer. "
            f"Received: sequence_stride={sequence_stride}"
        )
    if any_symbolic_tensors((x,)):
        return STFT(
            sequence_length=sequence_length,
            sequence_stride=sequence_stride,
            fft_length=fft_length,
            window=window,
            center=center,
        ).symbolic_call(x)
    return backend.math.stft(
        x,
        sequence_length=sequence_length,
        sequence_stride=sequence_stride,
        fft_length=fft_length,
        window=window,
        center=center,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Cast explicitly: int(sequence_stride), and ensure it is > 0
  2. Use argparse type=int for hop/stride CLI args
  3. If stride is computed (e.g. n_fft // 4), keep integer arithmetic
  4. Add a guard: assert isinstance(sequence_stride, int) and sequence_stride > 0

Example fix

# before
hop = np.int64(128)
z = keras.ops.stft(x, 256, hop, 256)
# after
hop = int(hop)
z = keras.ops.stft(x, 256, hop, 256)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(sequence_stride, int) and sequence_stride > 0, 'stride must be positive int'

Type guard

def valid_stride(s):
    return isinstance(s, int) and not isinstance(s, bool) and s > 0

Prevention

When it happens

Trigger: keras.ops.stft(x, 256, np.int64(128)); passing stride as float 128.0, a tensor, 0, or a negative hop; stride derived from a config or CLI arg without casting.

Common situations: Config values parsed as strings/floats (argparse type=float), numpy scalars from array ops, hop size computed as float (n_fft*0.5) instead of int().

Related errors


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