{"record":{"id":"6be24eefac7d12e1","repo":"keras-team/keras","slug":"to-call-stateless-call-self-class-name","errorCode":null,"errorMessage":"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.","messagePattern":"To call stateless_call, (.+?) must be built \\(i\\.e\\. its variables must have been already created\\)\\. You can build it by calling it on some data\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1145,"sourceCode":"        trainable_variables = model.trainable_variables\n        non_trainable_variables = model.non_trainable_variables\n        # Call the model with zero side effects\n        outputs, non_trainable_variables = model.stateless_call(\n            trainable_variables,\n            non_trainable_variables,\n            data,\n        )\n        # Attach the updated state to the model\n        # (until you do this, the model is still in its pre-call state).\n        for ref_var, value in zip(\n            model.non_trainable_variables, non_trainable_variables\n        ):\n            ref_var.assign(value)\n        ```\n        \"\"\"\n        self._check_super_called()\n        if not self.built:\n            raise ValueError(\n                f\"To call stateless_call, {self.__class__.__name__} must be \"\n                \"built (i.e. its variables must have been already created). \"\n                \"You can build it by calling it on some data.\"\n            )\n        if len(trainable_variables) != len(self.trainable_variables):\n            raise ValueError(\n                \"Argument `trainable_variables` must be a list of tensors \"\n                \"corresponding 1:1 to \"\n                f\"{self.__class__.__name__}().trainable_variables. \"\n                f\"Received list with length {len(trainable_variables)}, \"\n                f\"but expected {len(self.trainable_variables)} variables.\"\n            )\n        if len(non_trainable_variables) != len(self.non_trainable_variables):\n            raise ValueError(\n                \"Argument `non_trainable_variables` must be a list of tensors \"\n                \"corresponding 1:1 to \"\n                f\"{self.__class__.__name__}().non_trainable_variables. \"\n                f\"Received list with length {len(non_trainable_variables)}, \"","sourceCodeStart":1127,"sourceCodeEnd":1163,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1127-L1163","documentation":"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.","triggerScenarios":"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.","commonSituations":"Writing JAX/functional training steps with freshly constructed models; using compute_loss/stateless paths in Model methods on an unbuilt model.","solutions":["Build the layer first: call it once on sample data, layer.build(input_shape), or model(x) before stateless_call","In functional loops, ensure the init/apply split happens after parameter creation"],"exampleFix":"# before\nout, nw = layer.stateless_call(x, tv, ntv)  # layer never called\n# after\n_ = layer(x_sample)  # or layer.build(x_sample.shape)\nout, nw = layer.stateless_call(x, tv, ntv)","handlingStrategy":"validation","validationCode":"if not layer.built:\n    layer.build(input_shape)  # or layer(x_sample)","typeGuard":"def is_built(layer):\n    return bool(layer.built)","tryCatchPattern":null,"preventionTips":["Run one forward pass on dummy data before functional/stateless code paths","Check layer.built before stateless_call in library code"],"tags":["keras","stateless","build","functional-api"],"backgroundTag":"layer-not-built","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}