{"record":{"id":"a2011dd033f40521","repo":"keras-team/keras","slug":"argument-trainable-variables-must-be-a-list-of-t","errorCode":null,"errorMessage":"Argument `trainable_variables` must be a list of tensors corresponding 1:1 to {self.__class__.__name__}().trainable_variables. Received list with length {len(trainable_variables)}, but expected {len(self.trainable_variables)} variables.","messagePattern":"Argument `trainable_variables` must be a list of tensors corresponding 1:1 to (.+?)\\(\\)\\.trainable_variables\\. Received list with length (.+?), but expected (.+?) variables\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1151,"sourceCode":"            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)}, \"\n                f\"but expected {len(self.non_trainable_variables)} variables.\"\n            )\n\n        # Gather variable mapping\n        trainable_mapping = zip(self.trainable_variables, trainable_variables)\n        non_trainable_mapping = zip(","sourceCodeStart":1133,"sourceCodeEnd":1169,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1133-L1169","documentation":"stateless_call() requires the trainable_variables list to match the layer's own trainable_variables 1:1, in the same order. A length mismatch means you passed a wrong or partial set of variables (often from a different layer or the whole model).","triggerScenarios":"Passing model.trainable_variables to a sublayer's stateless_call; passing variables from before a rebuild; omitting or duplicating an entry.","commonSituations":"Functional (JAX-style) training loops where variables are gathered globally then handed to individual layers; refactoring after adding new variables.","solutions":["Pass exactly layer.trainable_variables (same object, same order) for that layer","If operating on a model, use the model-level functional API rather than per-layer stateless_call"],"exampleFix":"# before\nout, nw = layer.stateless_call(x, model.trainable_variables, [])\n# after\nout, nw = layer.stateless_call(x, layer.trainable_variables, layer.non_trainable_variables)","handlingStrategy":"validation","validationCode":"assert len(trainable_variables) == len(layer.trainable_variables)","typeGuard":"def vars_match(layer, tv):\n    return len(tv) == len(layer.trainable_variables)","tryCatchPattern":null,"preventionTips":["Pass layer.trainable_variables directly rather than re-gathering lists","Re-gather variables after any rebuild/quantize"],"tags":["keras","stateless","variables","validation"],"backgroundTag":"variable-list-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}