{"record":{"id":"2ed4630adfc6d82e","repo":"keras-team/keras","slug":"argument-non-trainable-variables-must-be-a-list","errorCode":null,"errorMessage":"Argument `non_trainable_variables` must be a list of tensors corresponding 1:1 to {self.__class__.__name__}().non_trainable_variables. Received list with length {len(non_trainable_variables)}, but expected {len(self.non_trainable_variables)} variables.","messagePattern":"Argument `non_trainable_variables` must be a list of tensors corresponding 1:1 to (.+?)\\(\\)\\.non_trainable_variables\\. Received list with length (.+?), but expected (.+?) variables\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1159,"sourceCode":"        ```\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(\n            self.non_trainable_variables, non_trainable_variables\n        )\n        mapping = list(trainable_mapping) + list(non_trainable_mapping)\n\n        # Caches info about `call()` signature, args, kwargs.\n        call_spec = CallSpec(\n            self._call_signature, self._call_context_args, args, kwargs\n        )","sourceCodeStart":1141,"sourceCodeEnd":1177,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1141-L1177","documentation":"stateless_call() also validates the non_trainable_variables list length against the layer's own non-trainable variables (BatchNorm moving statistics, quantization scale/zero-point, etc.). A mismatch raises this error before any computation.","triggerScenarios":"Passing an empty list for a layer with BN statistics; passing model-level non-trainable variables to a sublayer; forgetting RNG-state variables.","commonSituations":"Layers with metrics/BN state in functional loops; models quantized after variables were collected; variable set changed between gather and call.","solutions":["Pass exactly layer.non_trainable_variables to stateless_call","Re-gather variables right before the call if the layer was rebuilt or quantized in between"],"exampleFix":"# before\nout, _ = layer.stateless_call(x, layer.trainable_variables, [])  # layer has BN stats\n# after\nout, _ = layer.stateless_call(x, layer.trainable_variables, layer.non_trainable_variables)","handlingStrategy":"validation","validationCode":"assert len(non_trainable_variables) == len(layer.non_trainable_variables)","typeGuard":"def nontrainable_match(layer, ntv):\n    return len(ntv) == len(layer.non_trainable_variables)","tryCatchPattern":null,"preventionTips":["Account for BN statistics and RNG state in non-trainable lists","Gather both variable lists from the same layer object at call time"],"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"}