machine learning
model loading
get_config error
keras models
troubleshooting

get_config missing while loading previously saved model without custom layers

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If Keras says get_config is missing while loading a model, the real issue is usually that something inside the saved model is not fully serializable. That can happen even when you think you used only built-in layers, because serialization also includes things like lambda functions, custom losses, custom metrics, and sometimes version-specific wrappers.

Why Keras Needs get_config

When Keras reloads a full saved model, it must reconstruct the architecture from configuration data. For objects that participate in that reconstruction, Keras expects a serializable config or a known built-in implementation.

This works smoothly for ordinary built-in layers:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1, activation="sigmoid"),
7])
8
9model.save("simple_model.keras")
10loaded = tf.keras.models.load_model("simple_model.keras")

If loading fails with a get_config-related error, there is usually some object in the model or compile state that Keras does not know how to rebuild.

Common Hidden Causes

Even without a visibly custom layer, these can trigger problems:

  • 'Lambda layers using anonymous Python functions'
  • custom losses or metrics passed at compile time
  • subclassed models with incomplete serialization support
  • mismatched TensorFlow or Keras versions between save and load
  • legacy HDF5 saves containing objects that newer loaders handle differently

For example, this model looks simple, but the lambda function is a serialization hazard:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Lambda(lambda x: x * 2.0),
6    tf.keras.layers.Dense(1),
7])

A Lambda layer can work during training but still cause trouble when reloading across environments.

Safer Replacement for Lambda

Replace ad hoc lambda logic with a proper serializable layer:

python
1import tensorflow as tf
2
3class DoubleLayer(tf.keras.layers.Layer):
4    def call(self, inputs):
5        return inputs * 2.0
6
7    def get_config(self):
8        return super().get_config()
9
10
11model = tf.keras.Sequential([
12    tf.keras.layers.Input(shape=(4,)),
13    DoubleLayer(),
14    tf.keras.layers.Dense(1),
15])
16
17model.save("safe_model.keras")
18loaded = tf.keras.models.load_model(
19    "safe_model.keras",
20    custom_objects={"DoubleLayer": DoubleLayer},
21)

Now Keras has a clear serializable object instead of an anonymous function.

Loading Without Recompiling

Sometimes the model architecture and weights are fine, but the compile-time objects are the problem. In those cases, loading with compile=False can bypass the failing compile state:

python
loaded = tf.keras.models.load_model("model.keras", compile=False)

This is especially useful if you only need inference.

If you do need training later, you can recompile after loading:

python
loaded.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

Prefer Modern Save Formats

For current Keras workflows, prefer the native .keras format over older ad hoc patterns when possible:

python
model.save("model.keras")

The legacy HDF5 format still appears in older codebases, but serialization edge cases are easier to hit there, especially across library versions.

Common Pitfalls

The most common mistake is saying "there are no custom layers" while forgetting about lambda-based layers, custom metrics, or custom losses. Keras serialization cares about all serializable objects, not only obvious layer subclasses.

Another issue is loading the model in a different TensorFlow or Keras version than the one that saved it. Even built-in objects can behave differently across serialization formats and versions.

A third pitfall is assuming load_model must always restore compile state. If you only need inference, compile=False is often the simplest fix for a model that otherwise loads correctly.

Finally, if you truly have a custom serializable object, implement get_config properly and register or pass that object during loading instead of relying on implicit behavior.

Summary

  • A missing get_config error usually means some saved object is not fully serializable.
  • The cause may be a lambda layer, custom metric, loss, or version mismatch, even without obvious custom layers.
  • Prefer explicit serializable layer classes over anonymous Lambda logic.
  • Use compile=False when you only need inference and compile state is the problem.
  • Prefer modern model save formats and consistent library versions when saving and loading.

Related reading
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
Track 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.

Practice ML system design

All Rights Reserved.