keras-team/keras · error · ValueError

`shift` and `axis` must be broadcastable to the same…

Error message

`shift` and `axis` must be broadcastable to the same length. Received: shift={shift}, axis={axis}

What it means

Guard in normalize_shift_and_axis (used by ops.roll): after a scalar shift or axis is broadcast against the other sequence, the two lists still differ in length, so there is no one-to-one mapping between each shift amount and the axis it applies to.

Solutions

  1. Give shift and axis matching lengths, e.g. shift=(1, 2), axis=(0, 1).
  2. Use a scalar for one side: shift=1 with axis=(0, 1) broadcasts the shift to both axes.
  3. For distinct shifts per axis, list every axis explicitly instead of relying on broadcasting.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/backend_utils.py:454 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/4833b3508f646617. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/common/backend_utils.py:454

        return (value,)
    return value


def normalize_shift_and_axis(shift, axis):
    """Normalize `shift` and `axis` arguments of `roll` to the same length.

    Mirrors numpy's behavior of broadcasting a scalar (or length-1
    sequence) `shift`/`axis` against the other argument. Accepts ints,
    lists, tuples, and array-likes (e.g. numpy arrays).
    """
    shifts = list(to_tuple_or_list(shift))
    axes = list(to_tuple_or_list(axis))
    if len(shifts) == 1:
        shifts = shifts * len(axes)
    if len(axes) == 1:
        axes = axes * len(shifts)
    if len(shifts) != len(axes):
        raise ValueError(
            "`shift` and `axis` must be broadcastable to the same length. "
            f"Received: shift={shift}, axis={axis}"
        )
    return shifts, axes


### Code for ops.vectorize() used for TF and torch backends.

# See http://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html
_DIMENSION_NAME = r"\w+"
_CORE_DIMENSION_LIST = "(?:{0:}(?:,{0:})*)?".format(_DIMENSION_NAME)
_ARGUMENT = rf"\({_CORE_DIMENSION_LIST}\)"
_ARGUMENT_LIST = "{0:}(?:,{0:})*".format(_ARGUMENT)
_SIGNATURE = "^{0:}->{0:}$".format(_ARGUMENT_LIST)


def _vectorize_parse_gufunc_signature(
    signature,

View on GitHub (pinned to 7a34a03db6)