apache/beam · error · ValueError

Unable to load the TensorFlow model

Error message

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.

What it means

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.

Solutions

  1. Verify the model was saved with TF2 (tf.saved_model / model.save) or pick a TF2 module from tfhub.dev
  2. Load the model locally with tf.keras.models.load_model to see the underlying exception and fix the root cause
  3. 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

Example fix

// before
handler = TFModelHandlerMRU(model_uri='https://tfhub.dev/google/nnlm-en-dim50/1')  # TF1 module
// after
handler = TFModelHandlerMRU(model_uri='https://tfhub.dev/google/nnlm-en-dim50-with-normalization/2')  # TF2
Defensive patterns

Strategy: try-catch

Validate before calling

import tensorflow as tf
from apache_beam.ml.inference.tensorflow_inference import hub
try:
    tf.keras.models.load_model(hub.resolve(model_uri))
except Exception as e:
    raise ValueError(f'Model at {model_uri} cannot be loaded in TF2: {e}')

Try / catch

try:
    predictions = pcoll | RunInference(tf_handler)
except ValueError as e:
    if 'Unable to load the TensorFlow model' in str(e):
        raise RuntimeError('Model must be TF2 format; verify the tfhub URL/ SavedModel path and custom objects') from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/ac1c2921051d713d. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/ml/inference/tensorflow_inference.py:62

    Sequence[Union[numpy.ndarray, tf.Tensor]],
    dict[str, Any],
    Optional[str]
],
                             Iterable[PredictionResult]]


class ModelType(enum.Enum):
  """Defines how a model file should be loaded."""
  SAVED_MODEL = 1
  SAVED_WEIGHTS = 2


def _load_model(model_uri, custom_weights, load_model_args):
  try:
    model = tf.keras.models.load_model(
        hub.resolve(model_uri), **load_model_args)
  except Exception as e:
    raise ValueError(
        "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."  # pylint: disable=line-too-long
        .format(exception=e))
  if custom_weights:
    model.load_weights(custom_weights)
  return model


def _load_model_from_weights(create_model_fn, weights_path):
  model = create_model_fn()
  model.load_weights(weights_path)
  return model


def default_numpy_inference_fn(
    model: tf.Module,
    batch: Sequence[numpy.ndarray],

View on GitHub (pinned to 12126d8942)