keras-team/keras · error · ValueError

To call stateless_call, {self.__class__.__name__} must be bu

Error message

To call stateless_call, {self.__class__.__name__} must be built (i.e. its variables must have been already created). You can build it by calling it on some data.

What it means

stateless_call() computes outputs with replacement values for the layer's variables, so those variables must already exist. Calling it on a layer that has never processed data (built=False) raises this error, telling you to build first.

Source

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

        trainable_variables = model.trainable_variables
        non_trainable_variables = model.non_trainable_variables
        # Call the model with zero side effects
        outputs, non_trainable_variables = model.stateless_call(
            trainable_variables,
            non_trainable_variables,
            data,
        )
        # Attach the updated state to the model
        # (until you do this, the model is still in its pre-call state).
        for ref_var, value in zip(
            model.non_trainable_variables, non_trainable_variables
        ):
            ref_var.assign(value)
        ```
        """
        self._check_super_called()
        if not self.built:
            raise ValueError(
                f"To call stateless_call, {self.__class__.__name__} must be "
                "built (i.e. its variables must have been already created). "
                "You can build it by calling it on some data."
            )
        if len(trainable_variables) != len(self.trainable_variables):
            raise ValueError(
                "Argument `trainable_variables` must be a list of tensors "
                "corresponding 1:1 to "
                f"{self.__class__.__name__}().trainable_variables. "
                f"Received list with length {len(trainable_variables)}, "
                f"but expected {len(self.trainable_variables)} variables."
            )
        if len(non_trainable_variables) != len(self.non_trainable_variables):
            raise ValueError(
                "Argument `non_trainable_variables` must be a list of tensors "
                "corresponding 1:1 to "
                f"{self.__class__.__name__}().non_trainable_variables. "
                f"Received list with length {len(non_trainable_variables)}, "

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Build the layer first: call it once on sample data, layer.build(input_shape), or model(x) before stateless_call
  2. In functional loops, ensure the init/apply split happens after parameter creation

Example fix

# before
out, nw = layer.stateless_call(x, tv, ntv)  # layer never called
# after
_ = layer(x_sample)  # or layer.build(x_sample.shape)
out, nw = layer.stateless_call(x, tv, ntv)
Defensive patterns

Strategy: validation

Validate before calling

if not layer.built:
    layer.build(input_shape)  # or layer(x_sample)

Type guard

def is_built(layer):
    return bool(layer.built)

Prevention

When it happens

Trigger: Calling layer.stateless_call(x, trainable_variables, non_trainable_variables) before layer(x); typical inside functional/jaxax training loops or tests that exercise stateless execution before any forward pass.

Common situations: Writing JAX/functional training steps with freshly constructed models; using compute_loss/stateless paths in Model methods on an unbuilt model.

Related errors


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