Skip to content

Conversation

@nikolasavic3
Copy link
Contributor

Description

Separates the try-except blocks for tensorflow and tensorflow-text imports to allow the library to function when tensorflow-text is unavailable (e.g., Python 3.13 where tensorflow-text support is not yet available).

Problem

Currently, if tensorflow-text fails to import, both tf and tf_text are set to None, even if tensorflow imports successfully. This causes AttributeError: 'NoneType' object has no attribute 'RaggedTensor' errors in downstream libraries.

Related Issues

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nikolasavic3, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the import mechanism for tensorflow and tensorflow-text within keras_hub/src/utils/tensor_utils.py. By isolating their respective import attempts into distinct error-handling blocks, the system gains resilience, allowing core tensorflow functionalities to remain operational even when tensorflow-text cannot be loaded, such as in environments where it's not supported. This prevents critical AttributeError exceptions in downstream applications that rely on tensorflow's core components.

Highlights

  • Import Logic Separation: The import statements for tensorflow and tensorflow-text are now handled in separate try-except blocks, ensuring independent error handling for each library.
  • Improved Robustness: This change ensures that tensorflow can be imported successfully and its functionalities remain available even if tensorflow-text is unavailable or fails to import (e.g., in Python 3.13 environments).
  • Fixes AttributeError: The pull request resolves an issue where tf was incorrectly set to None when tensorflow-text failed to import, which previously led to AttributeError: 'NoneType' object has no attribute 'RaggedTensor' in downstream libraries.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly separates the import handling for tensorflow and tensorflow-text to prevent issues when one is available but not the other. This is a good improvement for robustness. However, there is a critical syntax error in the new except block that needs to be fixed. I've provided a suggestion to correct it.

@nikolasavic3 nikolasavic3 force-pushed the fix-import-try-block branch 2 times, most recently from e0b9991 to 982f4b7 Compare October 27, 2025 02:18
@sachinprasadhs
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses an issue where a failure to import tensorflow-text would incorrectly cause tensorflow to be considered unavailable. By separating the import try-except blocks, the library can now function correctly when tensorflow is installed but tensorflow-text is not. My review includes a suggestion to further clean up the new import logic for better clarity and maintainability.

try:
import tensorflow_text as tf_text
except ImportError:
tf_text = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove line 17? (tf_text = None in the other except block)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Should we nest the tensorflow_text import inside the tensorflow import block to avoid attempting to import tf_text when tf isn't available?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, it tf fails, tensorflow_text should fail too, and regardless nothing using either of those will work.

try:
import tensorflow_text as tf_text
except ImportError:
tf_text = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, it tf fails, tensorflow_text should fail too, and regardless nothing using either of those will work.

@hertschuh hertschuh merged commit 8cc8d06 into keras-team:master Oct 27, 2025
8 checks passed
@hertschuh
Copy link
Contributor

Thank you for the fix!

@nikolasavic3 nikolasavic3 deleted the fix-import-try-block branch October 28, 2025 02:29
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.

Update dev dependencies to Python 3.13

3 participants