Fill keys in CSVLogger before stop_training is checked
diff --git a/keras/callbacks.py b/keras/callbacks.py
index d86c26ce0f2..55d640e2a84 100644
--- a/keras/callbacks.py
+++ b/keras/callbacks.py
@@ -965,13 +965,14 @@ def handle_value(k):
             else:
                 return k
 
+        if self.keys is None:
+            self.keys = sorted(logs.keys())
+
         if self.model.stop_training:
             # We set NA so that csv parsers do not fail for this last epoch.
             logs = dict([(k, logs[k]) if k in logs else (k, 'NA') for k in self.keys])
 
         if not self.writer:
-            self.keys = sorted(logs.keys())
-
             class CustomDialect(csv.excel):
                 delimiter = self.sep
 

Fixes #8797.