Unable to import Tokenizer from Keras
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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

