Python
AttributeError
NoneType
fit_generator
debugging

AttributeError 'NoneType' object has no attribute 'fit_generator'

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

This error means you are calling fit_generator on a variable that is actually None. In Keras and TensorFlow code, that usually happens because the model-building function forgot to return the model, a variable was overwritten, or old tutorial code still uses fit_generator even though modern Keras expects fit. The fix is to identify which object is None and then check why it never became a valid model or generator.

What The Error Is Really Saying

Python raises:

text
AttributeError: 'NoneType' object has no attribute 'fit_generator'

when you do something like this:

python
model = None
model.fit_generator(...)

So the debugging question is not "why does fit_generator fail" but "why is model equal to None here?"

The Most Common Cause: Missing return

A very common mistake is to build a model inside a helper function but forget to return it.

python
1from tensorflow import keras
2
3
4def build_model():
5    model = keras.Sequential([
6        keras.layers.Input(shape=(10,)),
7        keras.layers.Dense(16, activation="relu"),
8        keras.layers.Dense(1)
9    ])
10    model.compile(optimizer="adam", loss="mse")
11    # missing: return model
12
13model = build_model()
14print(model)  # None
15model.fit_generator(...)  # crashes

The fix is simple:

python
1def build_model():
2    model = keras.Sequential([
3        keras.layers.Input(shape=(10,)),
4        keras.layers.Dense(16, activation="relu"),
5        keras.layers.Dense(1)
6    ])
7    model.compile(optimizer="adam", loss="mse")
8    return model

Modern Keras Usually Uses fit, Not fit_generator

Another source of confusion is API age. In modern TensorFlow Keras, you usually call fit even when the input is a generator or a Sequence.

python
1from tensorflow import keras
2import numpy as np
3
4class MySequence(keras.utils.Sequence):
5    def __len__(self):
6        return 10
7
8    def __getitem__(self, idx):
9        x = np.random.rand(32, 10).astype("float32")
10        y = np.random.rand(32, 1).astype("float32")
11        return x, y
12
13model = keras.Sequential([
14    keras.layers.Input(shape=(10,)),
15    keras.layers.Dense(16, activation="relu"),
16    keras.layers.Dense(1)
17])
18model.compile(optimizer="adam", loss="mse")
19
20model.fit(MySequence(), epochs=2)

If you are following old code samples, replacing fit_generator with fit may be part of the cleanup.

Check Which Variable Is Actually None

Do not guess. Print or assert the objects involved right before the failing line.

python
print("model:", model)
assert model is not None, "model was never created"

If the generator itself might be the issue, inspect that too.

python
print("train_data:", train_data)
assert train_data is not None, "training generator is None"

This immediately tells you whether the model, the data source, or some earlier factory function is broken.

Watch For Variable Overwrites

Sometimes the model was created correctly but got overwritten later.

python
1model = build_model()
2# ... many lines later
3model = maybe_load_model()  # accidentally returns None
4model.fit(...)

This is why small, well-named variables and explicit checks matter in training scripts. Machine-learning code often grows quickly, and silent reassignments are easy to miss.

A Good Minimal Debugging Pattern

A reliable workflow is:

  1. build the model
  2. print the model summary
  3. verify the training data object
  4. call fit
python
1model = build_model()
2assert model is not None
3model.summary()
4
5train_data = MySequence()
6assert train_data is not None
7
8model.fit(train_data, epochs=2)

That keeps the failure close to the real cause instead of surfacing later as an attribute error.

Common Pitfalls

  • Forgetting to return model from the model-construction function.
  • Following older tutorials and assuming fit_generator is still the standard API entry point.
  • Printing stack traces without checking which variable is actually None.
  • Accidentally overwriting model or the generator variable later in the script.
  • Debugging the Keras API first when the real issue is ordinary Python control flow.

Summary

  • The error means you called fit_generator on None, not on a valid Keras model.
  • The most common cause is a helper function that forgot to return the model.
  • In modern TensorFlow Keras, fit usually replaces fit_generator.
  • Add assertions and prints immediately before the training call to see which object is None.
  • Treat this as a Python object-lifecycle bug first and a Keras API problem second.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms