keras-team/keras · error · ValueError

Argument `input_tensors` must be a KerasTensor. Received inv

Error message

Argument `input_tensors` must be a KerasTensor. Received invalid value: input_tensors={input_tensors}

What it means

After cardinality is resolved, _clone_sequential_model validates that input_tensors is a backend KerasTensor - the clone is built by feeding it into keras.Input(tensor=...). Raw numpy arrays, tf.Tensors or torch tensors raise this ValueError.

Source

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

        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

    if input_tensors is not None:
        if isinstance(input_tensors, (list, tuple)):
            if len(input_tensors) != 1:
                raise ValueError(
                    "Argument `input_tensors` must contain a single tensor."
                )
            input_tensors = input_tensors[0]
        if not isinstance(input_tensors, backend.KerasTensor):
            raise ValueError(
                "Argument `input_tensors` must be a KerasTensor. "
                f"Received invalid value: input_tensors={input_tensors}"
            )
        inputs = Input(
            tensor=input_tensors,
            name=input_name,
            optional=input_optional,
        )
        new_layers = [inputs] + new_layers
    else:
        if input_batch_shape is not None:
            inputs = Input(
                batch_shape=input_batch_shape,
                dtype=input_dtype,
                name=input_name,
                optional=input_optional,
            )
            new_layers = [inputs] + new_layers

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Create a symbolic input first: keras.Input(shape=..., dtype=...) and pass that instead.
  2. If you only want a specific batch shape, pass input_batch_shape rather than real tensors.

Example fix

# before
clone = keras.models.clone_model(seq_model, input_tensors=np.zeros((4, 10)))

# after
new_input = keras.Input(shape=(10,))
clone = keras.models.clone_model(seq_model, input_tensors=new_input)
Defensive patterns

Strategy: type-guard

Validate before calling

from keras.src import backend
if not isinstance(input_tensors, backend.KerasTensor):
    input_tensors = keras.Input(shape=tuple(input_tensors.shape[1:])) if hasattr(input_tensors, 'shape') else keras.Input(shape=(None,))

Type guard

from keras.src import backend
def is_keras_tensor(t) -> bool:
    return isinstance(t, backend.KerasTensor)

Prevention

When it happens

Trigger: clone_model(seq_model, input_tensors=np.random.rand(4, 10)) or any framework-native tensor instead of a keras.KerasTensor.

Common situations: Passing real data batches instead of symbolic inputs; mixing framework-native tensors with Keras 3's symbolic KerasTensor.

Related errors


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