Unable to import Tokenizer from Keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The Tokenizer class was part of keras.preprocessing.text in older Keras/TensorFlow versions. In TensorFlow 2.16+ with Keras 3, Tokenizer was removed because it is a legacy preprocessing utility. The import from keras.preprocessing.text import Tokenizer fails with ImportError or ModuleNotFoundError. The fix depends on your TensorFlow version: use from tensorflow.keras.preprocessing.text import Tokenizer for TF 2.x (before Keras 3), or switch to tf.keras.layers.TextVectorization which is the modern replacement.
The Error
Fix 1: Use tensorflow.keras (TF 2.0 - 2.15)
tensorflow.keras is the integrated Keras that ships with TensorFlow. The Tokenizer class is available here through TF 2.15.
Fix 2: Install tf-keras for Backward Compatibility
tf-keras is the Keras 2 API preserved as a separate package for backward compatibility with TF 2.16+.
Fix 3: Use TextVectorization Layer (Recommended)
TextVectorization is a Keras layer that can be included directly in the model, making the preprocessing part of the computation graph and exportable with the model.
TextVectorization in a Model
The model accepts raw text strings and handles tokenization internally. This makes deployment simpler because the preprocessing is part of the saved model.
Tokenizer vs TextVectorization
| Feature | Tokenizer | TextVectorization |
| API | Standalone utility | Keras layer |
| Part of model | No (preprocessing step) | Yes (in the graph) |
| Exportable with model | No (save separately) | Yes (saved with model) |
| Keras 3 support | Removed | Supported |
tf.data compatible | Via tf.py_function | Native |
| Fit method | fit_on_texts() | adapt() |
Padding Sequences (Companion Fix)
Common Pitfalls
- Importing from
kerasinstead oftensorflow.keras: Standalonekerasandtensorflow.kerasare different packages. In TF 2.x, always usefrom tensorflow.keras.preprocessing.text import Tokenizer. The standalonekeraspackage (Keras 3) removedTokenizer. - Using Tokenizer with TF 2.16+ (Keras 3): Keras 3 removed
keras.preprocessing.text.Tokenizerentirely. Installtf-kerasfor backward compatibility or migrate toTextVectorization. - Saving a Tokenizer separately from the model:
Tokenizeris not part of the model graph, so saving the model does not save the tokenizer. You must serialize it separately withtokenizer.to_json()orpickle.TextVectorizationis saved automatically with the model. - Not calling
adapt()before usingTextVectorization: UnlikeTokenizer.fit_on_texts(),TextVectorization.adapt()must be called on the training data before the layer can be used. Forgetting this raises an error about uninitialized vocabulary. - Mixing Keras 2 and Keras 3 imports: Having both
keras(v3) andtf-keras(v2) installed can cause confusion. Import explicitly fromtf_kerasfor Keras 2 APIs or fromkerasfor Keras 3 APIs. Do not mix them in the same file.
Summary
Tokenizerwas removed in Keras 3 (TF 2.16+) — usefrom tensorflow.keras.preprocessing.text import Tokenizerfor TF 2.0-2.15- Install
tf-kerasfor backward compatibility with TF 2.16+ - Migrate to
tf.keras.layers.TextVectorizationas the modern replacement TextVectorizationis a Keras layer that can be included in the model and exported with itpad_sequencesalso moved — usefrom tensorflow.keras.utils import pad_sequences- Always check your TensorFlow/Keras version to determine which import path to use
Related reading
- Unable to Run Tensorflow/Keras with GPU
- unable to use Trained Tensorflow model
- Unaggregated gradients / gradients per example in tensorflow
- Understand Op Registration and Kernel Linking in TensorFlow
- Unable to Install Tensorflow MemoryError
- Unable to install tensorflow using conda with python 3.8
- Unable to install AWS Elastic Beanstalk CLI Win10, Python 3.6, Pip 9.0.1
- Unable to load files using pickle and multiple modules
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.