Skip to content

fix: resolve TensorFlow keras lazy loader recursion error #3126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rkuester
Copy link
Contributor

Set TF_USE_LEGACY_KERAS=1 to force TensorFlow 2.19.0 to use tf_keras
instead of Keras 3.x if the build environment has both keras 3.10.0 and
tf_keras 2.19.0 installed. This prevents TensorFlow's lazy loader from
entering infinite recursion when trying to determine which Keras version
to use for tf.keras.models.Sequential().

This fixes runtime_test which was failing with:
"RecursionError: maximum recursion depth exceeded in comparison"
at line checking "if self._tfll_keras_version == 'keras_3'"

BUG=#3125

@rkuester rkuester requested a review from a team as a code owner June 24, 2025 22:56
@rkuester rkuester requested review from suleshahid and removed request for suleshahid June 24, 2025 22:58
Set TF_USE_LEGACY_KERAS=1 to force TensorFlow 2.19.0 to use
tf_keras instead of Keras 3.x if the environment has Keras 3.x
installed. This work around prevents TensorFlow's lazy loader
from infinitely recursing when trying to determine which
Keras version to use. See:

    https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility.

This fixes generate_test_models.py which was failing with:
"RecursionError: maximum recursion depth exceeded in comparison"
at line checking "if self._tfll_keras_version == 'keras_3'"

BUG=tensorflow#3125
@rkuester
Copy link
Contributor Author

Oops, must ensure that tf_keras is installed.

Have I mentioned how much I dislike our non-hermetic Python setup in Bazel? I might fix that soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants