keras-team/keras · error · ValueError

Expected `model` argument to be a `Sequential` model instanc

Error message

Expected `model` argument to be a `Sequential` model instance. Received: model={model}

What it means

_clone_sequential_model() is the internal Sequential branch of clone_model and asserts its model argument is a Sequential instance. If a non-Sequential reaches it (usually via direct internal calls or broken type dispatch), it raises this ValueError echoing the received object.

Source

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

    except that it creates new layers (and thus new weights) instead
    of sharing the weights of the existing layers.

    Args:
        model: Instance of `Sequential`.
        input_tensors: optional list of input tensors
            to build the model upon. If not provided,
            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

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use the public keras.models.clone_model(model), which dispatches by model type.
  2. Ensure your model actually subclasses keras.Sequential if you rely on Sequential-specific cloning.
  3. For lookalike classes, clone from config: type(model).from_config(model.get_config()).

Example fix

# before
from keras.src.models.cloning import _clone_sequential_model
clone = _clone_sequential_model(my_model, clone_function=fn)

# after
clone = keras.models.clone_model(my_model, clone_function=fn)
Defensive patterns

Strategy: type-guard

Validate before calling

import keras
assert isinstance(model, keras.Sequential), 'use keras.models.clone_model for non-Sequential models'

Type guard

import keras
def is_sequential(m) -> bool:
    return isinstance(m, keras.Sequential)

Prevention

When it happens

Trigger: Directly calling keras.src.models.cloning._clone_sequential_model(functional_or_custom_model); indirectly when a Sequential-lookalike does not actually subclass keras.Sequential.

Common situations: Copy-pasting internal cloning code; custom model classes that mimic Sequential's API but do not subclass it.

Related errors


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