keras-team/keras · error · ValueError

Length of `start_indices` must match length of `shape`…

Error message

Length of `start_indices` must match length of `shape`. Received: start_indices={start_indices}, shape={shape}

What it means

Validation at the top of the NumPy backend's `slice` op: `start_indices` gives the corner of the region to extract and `shape` its extent, so the two sequences must have one entry per input dimension. If `len(start_indices) != len(shape)` (e.g. a 2D offset paired with a 3D size), the slice specification is ambiguous and the guard rejects it, echoing both arguments.

Solutions

  1. Make start_indices a sequence with exactly as many elements as shape, one per dimension of `inputs`
  2. Use plain Python lists/tuples for both arguments so their lengths are unambiguous
  3. Build the size list from inputs.ndim (e.g. [inputs.shape[d] - s for d, s in enumerate(start_indices)]) to guarantee consistency
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/numpy/ops/core.py:362 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/bdcaf46310811b5b. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/numpy/ops/core.py:362

    if reduction is None:
        inputs[idx] = updates
    elif reduction == "add":
        np.add.at(inputs, idx, updates)
    elif reduction == "max":
        np.maximum.at(inputs, idx, updates)
    elif reduction == "min":
        np.minimum.at(inputs, idx, updates)
    elif reduction == "mul":
        np.multiply.at(inputs, idx, updates)
    else:
        raise ValueError(f"Unsupported reduction: {reduction}")
    return inputs


def slice(inputs, start_indices, shape):
    # Validate inputs
    if len(start_indices) != len(shape):
        raise ValueError(
            "Length of `start_indices` must match length of `shape`. "
            f"Received: start_indices={start_indices}, shape={shape}"
        )

    # Generate list of indices arrays for each dimension
    indices = [
        np.arange(start, start + length)
        for start, length in zip(start_indices, shape)
    ]

    # Use np.ix_ to create a multidimensional index array
    mesh = np.ix_(*indices)

    return inputs[mesh]


def slice_update(inputs, start_indices, updates):
    # Generate list of indices arrays for each dimension

View on GitHub (pinned to 7a34a03db6)