{"record":{"id":"6f62f56b28a9e9f4","repo":"keras-team/keras","slug":"backend-variable-must-be-a-backend-variable-r","errorCode":null,"errorMessage":"`backend_variable` must be a `backend.Variable`. Recevied: backend_variable={backend_variable} of type ({type(backend_variable)})","messagePattern":"`backend_variable` must be a `backend\\.Variable`\\. Recevied: backend_variable=(.+?) of type \\((.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/export/saved_model_export_archive.py","lineNumber":303,"sourceCode":"        )\n\n        # Print out available endpoints\n        if verbose:\n            endpoints = \"\\n\\n\".join(\n                _print_signature(\n                    getattr(self._tf_trackable, name), name, verbose=verbose\n                )\n                for name in self._endpoint_names\n            )\n            io_utils.print_msg(\n                f\"Saved artifact at '{filepath}'. \"\n                \"The following endpoints are available:\\n\\n\"\n                f\"{endpoints}\"\n            )\n\n    def _convert_to_tf_variable(self, backend_variable):\n        if not isinstance(backend_variable, backend.Variable):\n            raise TypeError(\n                \"`backend_variable` must be a `backend.Variable`. \"\n                f\"Recevied: backend_variable={backend_variable} of type \"\n                f\"({type(backend_variable)})\"\n            )\n        return tf.Variable(\n            backend_variable.value,\n            dtype=backend_variable.dtype,\n            trainable=backend_variable.trainable,\n            name=backend_variable.name,\n        )\n\n    def _get_concrete_fn(self, endpoint):\n        \"\"\"Workaround for some SavedModel quirks.\"\"\"\n        if endpoint in self._endpoint_signatures:\n            return getattr(self._tf_trackable, endpoint)\n        else:\n            traces = getattr(self._tf_trackable, endpoint)._trackable_children(\n                \"saved_model\"","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/export/saved_model_export_archive.py#L285-L321","documentation":"Raised by ExportArchive._convert_to_tf_variable when a value passed into the SavedModel export path is not a keras.backend.Variable. The exporter walks model weights and converts each one to a tf.Variable, so any weight-like object that is not a Keras Variable (e.g. a raw tf.Variable, a numpy array, or a tensor) triggers this TypeError. It almost always means the model contains manually attached non-Keras weights or was built with a non-TensorFlow Keras backend (jax/torch) whose variables are not instances of backend.Variable in the TF backend namespace.","triggerScenarios":"Calling export_archive.track(model) or ExportArchive(...) on a model whose weights include raw tf.Variable objects, numpy arrays, or variables created under keras.backend('jax') or ('torch') while exporting to a TensorFlow SavedModel; also directly calling archive._convert_to_tf_variable(non_variable).","commonSituations":"Mixed TF/Keras 3 code where users assign tf.Variable attributes to layers; exporting a model built with the JAX or PyTorch multi-backend and then trying to write a TF SavedModel; porting Keras 2 code that manipulated weights as numpy arrays.","solutions":["Ensure the model is built and its weights created under the TensorFlow backend: set os.environ['KERAS_BACKEND']='tensorflow' before importing keras, then rebuild the model.","Replace any manually attached tf.Variable or numpy weights on layers with proper Keras variables via keras.Variable(...) or layer.add_weight(...).","If exporting from torch/jax, first convert weights: rebuild the same architecture on the TF backend and load_weights() from the saved checkpoint before exporting."],"exampleFix":"# before\nself.scale = tf.Variable(1.0)  # raw TF variable on a Keras layer\narchive.track(model)  # -> TypeError in _convert_to_tf_variable\n\n# after\nself.scale = keras.Variable(1.0)  # keras.src.backend.Variable\narchive.track(model)","handlingStrategy":"type-guard","validationCode":"import keras\nfrom keras.src import backend\n\ndef is_keras_variable(w):\n    return isinstance(w, backend.Variable)","typeGuard":"from keras.src import backend\n\ndef is_keras_variable(w) -> bool:\n    return isinstance(w, backend.Variable)","tryCatchPattern":null,"preventionTips":["Set KERAS_BACKEND=tensorflow before importing keras when exporting SavedModels.","Never attach raw tf.Variable or numpy arrays as layer weights; use keras.Variable or add_weight.","Before export, assert every entry in model.weights is a backend.Variable."],"tags":["keras","export","savedmodel","type-error","backend"],"backgroundTag":"invalid-argument-type","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}