Keras
RuntimeError
Error Handling
Deep Learning
Model Saving

RuntimeError Unable to create link name already exists 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.

Practice ML system design

Introduction

The Keras error RuntimeError: Unable to create link (name already exists) usually appears while saving a model in HDF5 format. The root issue is not that Keras suddenly lost the ability to save. It is that the HDF5 file structure is trying to create a group or dataset name that already exists, most commonly because layer names collide.

Why This Happens

HDF5 stores model data in a hierarchy of named groups and datasets. When Keras saves a model to .h5, it writes layer-related data under names derived from the model structure. If two layers end up with the same effective name, HDF5 cannot create both links.

A simplified example:

python
1from tensorflow import keras
2
3inputs = keras.Input(shape=(10,))
4x = keras.layers.Dense(8, name="shared_name")(inputs)
5outputs = keras.layers.Dense(1, name="shared_name")(x)
6
7model = keras.Model(inputs, outputs)
8model.save("model.h5")

This is a problem because the two layers use the same name. Keras layer names should be unique within the model graph.

Fix Duplicate Layer Names

The simplest fix is to give each layer a unique name or let Keras auto-generate names:

python
1from tensorflow import keras
2
3inputs = keras.Input(shape=(10,))
4x = keras.layers.Dense(8, name="dense_hidden")(inputs)
5outputs = keras.layers.Dense(1, name="dense_output")(x)
6
7model = keras.Model(inputs, outputs)
8model.save("model.h5")

If you do not need explicit names, removing manual names is often the cleanest option. Keras will generate unique names automatically.

This issue appears often when:

  • model-building code is copied and pasted
  • submodels are combined without renaming
  • custom layers hard-code names

Prefer the Native Keras Save Format When Possible

If you are using a modern Keras workflow, saving in the native Keras format is often simpler:

python
model.save("model.keras")

This does not magically fix every naming problem, but it avoids some of the older HDF5-specific friction and is generally the better default unless you specifically need .h5 compatibility.

If you must use HDF5 for compatibility with older tooling, then keeping names unique matters even more.

Rule Out File Reuse Problems

Name collisions can also happen when code interacts with HDF5 files manually through h5py or appends to an existing structure incorrectly. If you are not dealing with duplicate layer names, try saving to a fresh filename:

python
model.save("new_model.h5")

If a fresh file works, the problem may be with how the previous HDF5 file was created or reused.

That is less common than duplicate layer names in pure Keras code, but it is still worth checking.

Common Pitfalls

The biggest mistake is assuming the error means the output file itself already exists and must always be deleted. Sometimes the real problem is duplicate layer names inside the model, and deleting the file only hides the symptom temporarily.

Another issue is hard-coding names in reusable model-building functions. That makes collisions more likely when the function is called multiple times in a larger model graph.

Developers also often keep using .h5 out of habit even when .keras is a better fit for the current project. If there is no compatibility requirement, the native format is usually easier to work with.

Finally, if you manually manipulate HDF5 files outside Keras, be careful about reopening and appending under existing paths. HDF5 name collisions are not Keras-specific.

Summary

  • This error usually points to an HDF5 naming collision during model save.
  • Duplicate Keras layer names are a common cause.
  • Give layers unique names or let Keras auto-name them.
  • Prefer model.save("model.keras") when HDF5 compatibility is not required.
  • If needed, test with a fresh file to rule out HDF5 reuse problems.

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.