Bug: `import tensorflow_hub` hits `AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'`

Python 3.13,

tensorflow==2.20.0
tensorflow-hub==0.16.1
import tensorflow_hub as hub

hits the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], line 3
      1 import tensorflow as tf
      2 from tensorflow import keras
----> 3 import tensorflow_hub as hub

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tensorflow_hub/__init__.py:111
    103       print(
    104           "\n\nFailed to import tf_keras. Please note that tf_keras is not"
    105           " installed by default when you install tensorflow_hub. This is so"
    106           " that users can decide which tf_keras package to use. To use"
    107           " tensorflow_hub, please install a current version of tf_keras.\n\n"
    108       )
    109       raise
--> 111 _ensure_keras_2_importable()
    114 # pylint: disable=g-import-not-at-top
    115 # pylint: disable=g-bad-import-order
    116 # Symbols exposed via tensorflow_hub.
    117 from tensorflow_hub.keras_layer import KerasLayer

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tensorflow_hub/__init__.py:100, in _ensure_keras_2_importable()
     98 if version_fn and version_fn().startswith("3."):
     99   try:
--> 100     import tf_keras as keras
    101   except ImportError:
    102     # Print more informative error message, then reraise.
    103     print(
    104         "\n\nFailed to import tf_keras. Please note that tf_keras is not"
    105         " installed by default when you install tensorflow_hub. This is so"
    106         " that users can decide which tf_keras package to use. To use"
    107         " tensorflow_hub, please install a current version of tf_keras.\n\n"
    108     )

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/__init__.py:3
      1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from tf_keras import __internal__
      4 from tf_keras import activations
      5 from tf_keras import applications

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/__internal__/__init__.py:6
      4 from tf_keras.__internal__ import layers
      5 from tf_keras.__internal__ import losses
----> 6 from tf_keras.__internal__ import models
      7 from tf_keras.__internal__ import optimizers
      8 from tf_keras.__internal__ import utils

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/__internal__/models/__init__.py:3
      1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from tf_keras.src.models.cloning import clone_and_build_model
      4 from tf_keras.src.models.cloning import in_place_subclassed_model_state_restoration

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/__init__.py:21
      1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)     13 # limitations under the License.
     14 # ==============================================================================
     15 """Implementation of the TF-Keras API, the high-level API of TensorFlow.
     16 
     17 Detailed documentation and user guides are available at
     18 [keras.io](https://keras.io).
     19 """
---> 21 from tf_keras.src import applications
     22 from tf_keras.src import distribute
     23 from tf_keras.src import models

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/applications/__init__.py:18
      1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)     13 # limitations under the License.
     14 # ==============================================================================
     15 """Keras Applications are premade architectures with pre-trained weights."""
---> 18 from tf_keras.src.applications.convnext import ConvNeXtBase
     19 from tf_keras.src.applications.convnext import ConvNeXtLarge
     20 from tf_keras.src.applications.convnext import ConvNeXtSmall

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/applications/convnext.py:33
     31 from tf_keras.src import utils
     32 from tf_keras.src.applications import imagenet_utils
---> 33 from tf_keras.src.engine import sequential
     34 from tf_keras.src.engine import training as training_lib
     36 # isort: off

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/engine/sequential.py:24
     22 from tf_keras.src import layers as layer_module
     23 from tf_keras.src.engine import base_layer
---> 24 from tf_keras.src.engine import functional
     25 from tf_keras.src.engine import input_layer
     26 from tf_keras.src.engine import training

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/engine/functional.py:33
     31 from tf_keras.src.engine import input_spec
     32 from tf_keras.src.engine import node as node_module
---> 33 from tf_keras.src.engine import training as training_lib
     34 from tf_keras.src.engine import training_utils
     35 from tf_keras.src.saving import serialization_lib

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/engine/training.py:48
     46 from tf_keras.src.optimizers import optimizer_v1
     47 from tf_keras.src.saving import pickle_utils
---> 48 from tf_keras.src.saving import saving_api
     49 from tf_keras.src.saving import saving_lib
     50 from tf_keras.src.saving import serialization_lib

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/saving/saving_api.py:25
     22 from tensorflow.python.util.tf_export import keras_export
     24 from tf_keras.src.saving import saving_lib
---> 25 from tf_keras.src.saving.legacy import save as legacy_sm_saving_lib
     26 from tf_keras.src.utils import io_utils
     28 try:

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/saving/legacy/save.py:27
     25 from tf_keras.src.saving.legacy import serialization
     26 from tf_keras.src.saving.legacy.saved_model import load as saved_model_load
---> 27 from tf_keras.src.saving.legacy.saved_model import load_context
     28 from tf_keras.src.saving.legacy.saved_model import save as saved_model_save
     29 from tf_keras.src.saving.legacy.saved_model.utils import keras_option_scope

File ~/.local/share/virtualenvs/JupyterNotebooks-uVG1pv5y/lib/python3.13/site-packages/tf_keras/src/saving/legacy/saved_model/load_context.py:68
     64     """Returns whether under a load context."""
     65     return _load_context.in_load_context()
---> 68 tf.__internal__.register_load_context_function(in_load_context)

AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'

Hi @khteh, Thanks for reporting the issue.
It looks like there is an incompatibility issue with your library versions. Please, Make sure your TensorFlow and tf-keras packages are the same version. Thank you!

I don’t cherry-pick the versions of any one of these libraries!

Could you please confirm whether you are importing tf-keras in your code without installing it first in your environment?

Does your question make sense? How possible is that?

I have tf-keras = "*" in Pipfile and don’t have any import of this module in my code at all

Using your specified versions, I was able to successfully import tensorflow_hub in Google Colab.

Are you sure you are using Python 3.13?

I’m running on Python 3.12.

And you said “Using your specified versions…”?

My apologies for the confusion. I am running on Python 3.12, not 3.13. Thank you!

Hi @khteh, Have you tried with Python 3.12?

No but Python 3.13.3

Hi @khteh, Could you please try downgrading Python 3.12, if possible? It appears to be incompatible with Python 3.13 and TensorFlow Hub.