keras-team/keras · error · ValueError

Expected `clone_function` argument to be a callable. Receive

Error message

Expected `clone_function` argument to be a callable. Received: clone_function={clone_function}

What it means

_clone_sequential_model() requires clone_function to be callable because it maps it over every layer: [clone_function(layer) for layer in model.layers]. Passing None, a string, or any non-callable object raises this ValueError.

Source

Thrown at keras/src/models/cloning.py:286

            placeholders will be created.
        clone_function: callable to be applied on non-input layers in the model.
            By default, it clones the layer (without copying the weights).

    Returns:
        An instance of `Sequential` reproducing the behavior
        of the original model, on top of new inputs tensors,
        using newly instantiated weights.
    """

    if not isinstance(model, Sequential):
        raise ValueError(
            "Expected `model` argument "
            "to be a `Sequential` model instance. "
            f"Received: model={model}"
        )

    if not callable(clone_function):
        raise ValueError(
            "Expected `clone_function` argument to be a callable. "
            f"Received: clone_function={clone_function}"
        )

    new_layers = [clone_function(layer) for layer in model.layers]

    if isinstance(model._layers[0], InputLayer):
        ref_input_layer = model._layers[0]
        input_name = ref_input_layer.name
        input_batch_shape = ref_input_layer.batch_shape
        input_dtype = ref_input_layer._dtype
        input_optional = ref_input_layer.optional
    else:
        input_name = None
        input_dtype = None
        input_batch_shape = None
        input_optional = False

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a callable such as lambda layer: layer.__class__.from_config(layer.get_config()), or omit clone_function to use the default.
  2. If the function arrives serialized as a string, resolve it through a registry dict first.

Example fix

# before
clone = keras.models.clone_model(model, clone_function='copy_layer')

# after
def copy_layer(layer):
    return layer.__class__.from_config(layer.get_config())
clone = keras.models.clone_model(model, clone_function=copy_layer)
Defensive patterns

Strategy: type-guard

Validate before calling

assert callable(clone_function), 'clone_function must be callable'

Type guard

def is_callable_fn(f) -> bool:
    return callable(f)

Prevention

When it happens

Trigger: _clone_sequential_model(model, clone_function=None) or clone_function='default' - anything not callable.

Common situations: Config-driven cloning where clone_function arrives as a string name; passing a class or module instead of a function.

Related errors


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