keras-team/keras · error · TypeError

wrong number of output arguments: expected

Error message

wrong number of output arguments: expected {len(output_core_dims)}, got {len(out_shapes)}

What it means

Guard in the wrapped function of ops.vectorize: the number of tensors the wrapped function returned does not match the number of output groups declared in the gufunc signature, so outputs cannot be paired with their declared core dimensions.

Solutions

  1. Return exactly as many outputs as the signature declares after '->'.
  2. Adjust the signature's output list (e.g. '(n),(n)' vs '(n)') to the actual number of returned tensors.
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

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

    def wrapped(*args):
        out = func(*args)
        if isinstance(out, (list, tuple)):
            out_shapes = [ops.shape(x) for x in out]
        else:
            out_shapes = [out.shape]

        if expected_output_core_dims is None:
            output_core_dims = [()] * len(out_shapes)
        else:
            output_core_dims = expected_output_core_dims
            if len(output_core_dims) > 1 and not isinstance(out, tuple):
                raise TypeError(
                    "output must be a tuple when multiple outputs "
                    f"are expected, got: {out}"
                )
            if len(out_shapes) != len(output_core_dims):
                raise TypeError(
                    "wrong number of output arguments: "
                    f"expected {len(output_core_dims)}, got {len(out_shapes)}"
                )

        sizes = dict(dim_sizes)
        for shape, core_dims in zip(out_shapes, output_core_dims):
            _vectorize_update_dim_sizes(sizes, shape, core_dims, is_input=False)

        return out

    return wrapped


def _vectorize_apply_excluded(func, excluded, args, kwargs):
    if not excluded:
        return func, args, kwargs

    dynamic_args = [arg for i, arg in enumerate(args) if i not in excluded]

View on GitHub (pinned to 7a34a03db6)