{"record":{"id":"48fdf065ccae3ae2","repo":"tensorflow/models","slug":"model-must-be-a-tf-keras-model-object","errorCode":null,"errorMessage":"model must be a tf_keras.Model object.","messagePattern":"model must be a tf_keras\\.Model object\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/bert/model_saving_utils.py","lineNumber":49,"sourceCode":"      model: Keras model object to export.\n      checkpoint_dir: Path from which model weights will be loaded, if\n        specified.\n      restore_model_using_load_weights: Whether to use checkpoint.restore() API\n        for custom checkpoint or to use model.load_weights() API. There are 2\n        different ways to save checkpoints. One is using tf.train.Checkpoint and\n        another is using Keras model.save_weights(). Custom training loop\n        implementation uses tf.train.Checkpoint API and Keras ModelCheckpoint\n        callback internally uses model.save_weights() API. Since these two API's\n        cannot be used toghether, model loading logic must be take into account\n        how model checkpoint was saved.\n\n  Raises:\n    ValueError when either model_export_path or model is not specified.\n  \"\"\"\n  if not model_export_path:\n    raise ValueError('model_export_path must be specified.')\n  if not isinstance(model, tf_keras.Model):\n    raise ValueError('model must be a tf_keras.Model object.')\n\n  if checkpoint_dir:\n    if restore_model_using_load_weights:\n      model_weight_path = os.path.join(checkpoint_dir, 'checkpoint')\n      assert tf.io.gfile.exists(model_weight_path)\n      model.load_weights(model_weight_path)\n    else:\n      checkpoint = tf.train.Checkpoint(model=model)\n\n      # Restores the model from latest checkpoint.\n      latest_checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)\n      assert latest_checkpoint_file\n      logging.info('Checkpoint file %s found and restoring from '\n                   'checkpoint', latest_checkpoint_file)\n      checkpoint.restore(\n          latest_checkpoint_file).assert_existing_objects_matched()\n\n  model.save(model_export_path, include_optimizer=False, save_format='tf')","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/bert/model_saving_utils.py#L31-L67","documentation":"Error \"model must be a tf_keras.Model object.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/bert/model_saving_utils.py:49 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}