{"record":{"id":"5bb7612374309ff3","repo":"tensorflow/models","slug":"encountered-error-when-importing-tensorflow-text","errorCode":null,"errorMessage":"Encountered error when importing tensorflow_text: %s","messagePattern":"Encountered error when importing tensorflow_text: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/layers/text_layers.py","lineNumber":31,"sourceCode":"# limitations under the License.\n\n\"\"\"Keras Layers for BERT-specific preprocessing.\"\"\"\n# pylint: disable=g-import-not-at-top\nfrom typing import Any, Dict, List, Mapping, Optional, Text, Union\n\nfrom absl import logging\nimport tensorflow as tf, tf_keras\n\ntry:\n  # pytype: disable=import-error\n  import tensorflow_text as text\n  from tensorflow_text.python.ops import bert_tokenizer\n  # pytype: enable=import-error\nexcept ImportError:\n  text = None\n  bert_tokenizer = None\nexcept tf.errors.NotFoundError as e:\n  logging.warn(\"Encountered error when importing tensorflow_text: %s\", e)\n  text = None\n  bert_tokenizer = None\n\n\ndef _check_if_tf_text_installed():\n  if text is None:\n    raise ImportError(\"import tensorflow_text failed, please install \"\n                      \"'tensorflow-text-nightly'.\")\n\n\ndef _truncate_row_lengths(ragged_tensor: tf.RaggedTensor,\n                          new_lengths: tf.Tensor) -> tf.RaggedTensor:\n  \"\"\"Truncates the rows of `ragged_tensor` to the given row lengths.\"\"\"\n  new_lengths = tf.broadcast_to(new_lengths,\n                                ragged_tensor.bounding_shape()[0:1])\n  def fn(x):\n    row, new_length = x\n    return row[0:new_length]","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/layers/text_layers.py#L13-L49","documentation":"Error \"Encountered error when importing tensorflow_text: %s\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/layers/text_layers.py:31 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Install tensorflow_text: pip install tensorflow-text.","Match the tensorflow-text version to your TensorFlow version."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}