keras-team/keras · error · ValueError

{error_preamble} For layer '{class_name}', Received `{method

Error message

{error_preamble} For layer '{class_name}', Received `{method_name}()` argument `{name}`, which does not end in `_shape`.

What it means

For shape-computing methods with multiple arguments (compute_output_shape, build), Keras requires every parameter name to end in _shape and correspond to a call() argument. This error fires when a method like compute_output_shape(self, foo, bar_shape) declares a parameter without the _shape suffix, so Keras cannot map it back to a call() argument.

Source

Thrown at keras/src/layers/layer.py:2056

        key = expected_names[0]
        values = tuple(shapes_dict.values())
        if values:
            input_shape = values[0]
        else:
            input_shape = None
        return {key: input_shape}

    # Multiple args: check that all names line up.
    kwargs = {}
    for name in expected_names:
        method_name = target_fn.__name__
        error_preamble = (
            f"For a `{method_name}()` method with more than one argument, all "
            "arguments should have a `_shape` suffix and match an argument "
            f"from `call()`. E.g. `{method_name}(self, foo_shape, bar_shape)` "
        )
        if not name.endswith("_shape"):
            raise ValueError(
                f"{error_preamble} For layer '{class_name}', "
                f"Received `{method_name}()` argument "
                f"`{name}`, which does not end in `_shape`."
            )
        expected_call_arg = utils.removesuffix(name, "_shape")
        if expected_call_arg not in call_spec.arguments_dict:
            raise ValueError(
                f"{error_preamble} For layer '{class_name}', "
                f"received `{method_name}()` argument "
                f"`{name}`, but `call()` does not have argument "
                f"`{expected_call_arg}`."
            )
        if name in shapes_dict:
            kwargs[name] = shapes_dict[name]

    return kwargs

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Rename the parameter to <arg>_shape, e.g. compute_output_shape(self, input_shape, mask_shape)
  2. Ensure the stripped name (mask) matches an actual argument of call()
  3. If the extra parameter is a flag (like training), remove it from compute_output_shape — training is not a shape argument

Example fix

# before
class MyLayer(keras.layers.Layer):
    def compute_output_shape(self, input_shape, training):
        ...

# after
class MyLayer(keras.layers.Layer):
    def compute_output_shape(self, input_shape):
        ...
Defensive patterns

Strategy: validation

Validate before calling

import inspect
params = [n for n in inspect.signature(MyLayer.compute_output_shape).parameters if n != 'self']
assert len(params) <= 1 or all(p.endswith('_shape') for p in params), params

Prevention

When it happens

Trigger: Defining compute_output_shape(self, input_shape, training) or build(self, input_shape, mask) where the extra parameter lacks the _shape suffix on a layer with multiple such arguments.

Common situations: Porting old Keras 2 layers whose compute_output_shape took arbitrary parameter names; adding a mask or training parameter to compute_output_shape; subclassing layers that override build/compute_output_shape with positional names.

Related errors


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