{"record":{"id":"ac1c2921051d713d","repo":"apache/beam","slug":"unable-to-load-the-tensorflow-model-exception-make-sure-you","errorCode":null,"errorMessage":"Unable to load the TensorFlow model: {exception}. Make sure you've saved the model with TF2 format. Check out the list of TF2 Models on TensorFlow Hub - https://tfhub.dev/s?subtype=module,placeholder&tf-version=tf2.","messagePattern":"Unable to load the TensorFlow model: (.+?)\\. Make sure you've saved the model with TF2 format\\. Check out the list of TF2 Models on TensorFlow Hub - https://tfhub\\.dev/s\\?subtype=module,placeholder&tf-version=tf2\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/tensorflow_inference.py","lineNumber":62,"sourceCode":"    Sequence[Union[numpy.ndarray, tf.Tensor]],\n    dict[str, Any],\n    Optional[str]\n],\n                             Iterable[PredictionResult]]\n\n\nclass ModelType(enum.Enum):\n  \"\"\"Defines how a model file should be loaded.\"\"\"\n  SAVED_MODEL = 1\n  SAVED_WEIGHTS = 2\n\n\ndef _load_model(model_uri, custom_weights, load_model_args):\n  try:\n    model = tf.keras.models.load_model(\n        hub.resolve(model_uri), **load_model_args)\n  except Exception as e:\n    raise ValueError(\n        \"Unable to load the TensorFlow model: {exception}. Make sure you've \\\n        saved the model with TF2 format. Check out the list of TF2 Models on \\\n        TensorFlow Hub - https://tfhub.dev/s?subtype=module,placeholder&tf-version=tf2.\"  # pylint: disable=line-too-long\n        .format(exception=e))\n  if custom_weights:\n    model.load_weights(custom_weights)\n  return model\n\n\ndef _load_model_from_weights(create_model_fn, weights_path):\n  model = create_model_fn()\n  model.load_weights(weights_path)\n  return model\n\n\ndef default_numpy_inference_fn(\n    model: tf.Module,\n    batch: Sequence[numpy.ndarray],","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/tensorflow_inference.py#L44-L80","documentation":"Raised in tensorflow_inference._load_model when tf.keras.models.load_model fails for any reason; the original exception is chained into a ValueError advising that the model must be saved in TF2 format and pointing to TF2-compatible models on TensorFlow Hub.","triggerScenarios":"Passing a model_uri whose resolved artifact is a TF1 Hub module, a SavedModel directory that is corrupted/incomplete, or load_model_args incompatible with the saved format — any exception from load_model gets re-raised.","commonSituations":"Pointing at a TF1 hub module URL (tfhub.dev TF1 modules are unsupported); wrong GCS/HTTP path or missing files; custom layers/objects not registered so deserialization fails; passing load_model_args like compile=False incorrectly for the artifact type.","solutions":["Verify the model was saved with TF2 (tf.saved_model / model.save) or pick a TF2 module from tfhub.dev","Load the model locally with tf.keras.models.load_model to see the underlying exception and fix the root cause","Check the model_uri is reachable and complete (correct bucket path, all SavedModel files present) and that custom objects are passed via load_model_args"],"exampleFix":"// before\nhandler = TFModelHandlerMRU(model_uri='https://tfhub.dev/google/nnlm-en-dim50/1')  # TF1 module\n// after\nhandler = TFModelHandlerMRU(model_uri='https://tfhub.dev/google/nnlm-en-dim50-with-normalization/2')  # TF2","handlingStrategy":"try-catch","validationCode":"import tensorflow as tf\nfrom apache_beam.ml.inference.tensorflow_inference import hub\ntry:\n    tf.keras.models.load_model(hub.resolve(model_uri))\nexcept Exception as e:\n    raise ValueError(f'Model at {model_uri} cannot be loaded in TF2: {e}')","typeGuard":null,"tryCatchPattern":"try:\n    predictions = pcoll | RunInference(tf_handler)\nexcept ValueError as e:\n    if 'Unable to load the TensorFlow model' in str(e):\n        raise RuntimeError('Model must be TF2 format; verify the tfhub URL/ SavedModel path and custom objects') from e\n    raise","preventionTips":["Only reference TF2 modules on tfhub.dev (check the tf-version filter)","Dry-run load_model locally with the same TF version as workers","Register custom layers/objects (tf.keras.utils.get_custom_objects) before loading","Verify SavedModel paths are complete and reachable from workers"],"tags":["tensorflow","apache-beam","ml-inference","model-loading"],"backgroundTag":"file-read-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}