tensorflow/models · error

Encountered error when importing tensorflow_text: %s

Error message

Encountered error when importing tensorflow_text: %s

What it means

Error "Encountered error when importing tensorflow_text: %s" thrown in tensorflow/models.

Source

Thrown at official/nlp/modeling/layers/text_layers.py:31

# limitations under the License.

"""Keras Layers for BERT-specific preprocessing."""
# pylint: disable=g-import-not-at-top
from typing import Any, Dict, List, Mapping, Optional, Text, Union

from absl import logging
import tensorflow as tf, tf_keras

try:
  # pytype: disable=import-error
  import tensorflow_text as text
  from tensorflow_text.python.ops import bert_tokenizer
  # pytype: enable=import-error
except ImportError:
  text = None
  bert_tokenizer = None
except tf.errors.NotFoundError as e:
  logging.warn("Encountered error when importing tensorflow_text: %s", e)
  text = None
  bert_tokenizer = None


def _check_if_tf_text_installed():
  if text is None:
    raise ImportError("import tensorflow_text failed, please install "
                      "'tensorflow-text-nightly'.")


def _truncate_row_lengths(ragged_tensor: tf.RaggedTensor,
                          new_lengths: tf.Tensor) -> tf.RaggedTensor:
  """Truncates the rows of `ragged_tensor` to the given row lengths."""
  new_lengths = tf.broadcast_to(new_lengths,
                                ragged_tensor.bounding_shape()[0:1])
  def fn(x):
    row, new_length = x
    return row[0:new_length]

View on GitHub (pinned to e006f5f0d5)

Solutions

  1. Install tensorflow_text: pip install tensorflow-text.
  2. Match the tensorflow-text version to your TensorFlow version.

When it happens

Trigger: Thrown at official/nlp/modeling/layers/text_layers.py:31 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/5bb7612374309ff3. Report an issue: GitHub.