keras-team/keras · error · RuntimeError
The following attributes cannot be saved to HDF5 file becaus
Error message
The following attributes cannot be saved to HDF5 file because they are larger than {HDF5_OBJECT_HEADER_LIMIT} bytes: {bad_attributes} What it means
HDF5 stores small metadata as object-header attributes capped at 64512 bytes. save_attributes_to_hdf5_group checks every serialized attribute against HDF5_OBJECT_HEADER_LIMIT and raises RuntimeError listing the offenders, because even chunking cannot rescue an attribute over the header limit.
Source
Thrown at keras/src/legacy/saving/legacy_h5_format.py:309
This method deals with an inherent problem of HDF5 file which is not
able to store data larger than HDF5_OBJECT_HEADER_LIMIT bytes.
Args:
group: A pointer to a HDF5 group.
name: A name of the attributes to save.
data: Attributes data to store.
Raises:
RuntimeError: If any single attribute is too large to be saved.
"""
# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
# because in that case even chunking the array would not make the saving
# possible.
bad_attributes = [x for x in data if len(x) > HDF5_OBJECT_HEADER_LIMIT]
# Expecting this to never be true.
if bad_attributes:
raise RuntimeError(
"The following attributes cannot be saved to HDF5 file because "
f"they are larger than {HDF5_OBJECT_HEADER_LIMIT} "
f"bytes: {bad_attributes}"
)
data_npy = np.asarray(data)
num_chunks = 1
chunked_data = np.array_split(data_npy, num_chunks)
# This will never loop forever thanks to the test above.
while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data):
num_chunks += 1
chunked_data = np.array_split(data_npy, num_chunks)
if num_chunks > 1:
for chunk_id, chunk_data in enumerate(chunked_data):
group.attrs["%s%d" % (name, chunk_id)] = chunk_dataView on GitHub (pinned to 7a34a03db6)
Solutions
- Save in the native .keras format, which does not use HDF5 attributes
- Shorten layer names: assign explicit short names instead of long auto-generated ones
- Reduce the number of separately-named weights per group where possible
Example fix
# before
model.save_weights('big.h5') # RuntimeError: attributes too large
# after
model.save('big.keras') Defensive patterns
Strategy: fallback
Validate before calling
import h5py
HDF5_OBJECT_HEADER_LIMIT = 64512
names = [w.name for l in model.layers for w in l.weights]
if len('\n'.join(names).encode()) > HDF5_OBJECT_HEADER_LIMIT:
model.save('m.keras') # avoid the h5 path Try / catch
try:
model.save_weights('w.h5')
except RuntimeError as e:
if 'larger than' not in str(e):
raise
model.save('m.keras') Prevention
- Assign short explicit layer names in large models
- Use the .keras format for production checkpoints
When it happens
Trigger: Saving weights when the serialized weight_names / attribute blobs exceed 64 KB, typically from thousands of long auto-generated layer names in large nested Functional models.
Common situations: Very deep models or heavy layer reuse producing huge name lists; re-saving an old model whose names have grown; custom layers embedding metadata in names.
Related errors
- `save_model()` using h5 format requires h5py. Could not impo
- Layer '{self.name}' was never built and thus it doesn't have
- `load_model()` using h5 format requires h5py. Could not impo
- No model config found in the file at {filepath}.
- Layer count mismatch when loading weights from file. Model e
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/ed166c6f6aabd478.
Report an issue: GitHub.