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.
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:
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:
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:
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:
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
- SageMaker and TensorFlow 2.0
- Same function in Keras \`Loss\` and Metric give different values even without regularization
- Same function in Keras \`Loss\` and Metric give different values even without regularization
- Save Keras model at specific epochs
- Sampling without replacement from a given non-uniform distribution in TensorFlow
- Save Keras ModelCheckpoints in Google Cloud Bucket
- RuntimeError view size is not compatible with input tensor's size and stride at least one dimension spans across two contiguous subspaces
- S3 Bucket action doesn't apply to any resources
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.