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_data

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Save in the native .keras format, which does not use HDF5 attributes
  2. Shorten layer names: assign explicit short names instead of long auto-generated ones
  3. 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

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


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/ed166c6f6aabd478. Report an issue: GitHub.