{"record":{"id":"ed166c6f6aabd478","repo":"keras-team/keras","slug":"the-following-attributes-cannot-be-saved-to-hdf5-f","errorCode":null,"errorMessage":"The following attributes cannot be saved to HDF5 file because they are larger than {HDF5_OBJECT_HEADER_LIMIT} bytes: {bad_attributes}","messagePattern":"The following attributes cannot be saved to HDF5 file because they are larger than (.+?) bytes: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/saving/legacy_h5_format.py","lineNumber":309,"sourceCode":"    This method deals with an inherent problem of HDF5 file which is not\n    able to store data larger than HDF5_OBJECT_HEADER_LIMIT bytes.\n\n    Args:\n        group: A pointer to a HDF5 group.\n        name: A name of the attributes to save.\n        data: Attributes data to store.\n\n    Raises:\n      RuntimeError: If any single attribute is too large to be saved.\n    \"\"\"\n    # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`\n    # because in that case even chunking the array would not make the saving\n    # possible.\n    bad_attributes = [x for x in data if len(x) > HDF5_OBJECT_HEADER_LIMIT]\n\n    # Expecting this to never be true.\n    if bad_attributes:\n        raise RuntimeError(\n            \"The following attributes cannot be saved to HDF5 file because \"\n            f\"they are larger than {HDF5_OBJECT_HEADER_LIMIT} \"\n            f\"bytes: {bad_attributes}\"\n        )\n\n    data_npy = np.asarray(data)\n\n    num_chunks = 1\n    chunked_data = np.array_split(data_npy, num_chunks)\n\n    # This will never loop forever thanks to the test above.\n    while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data):\n        num_chunks += 1\n        chunked_data = np.array_split(data_npy, num_chunks)\n\n    if num_chunks > 1:\n        for chunk_id, chunk_data in enumerate(chunked_data):\n            group.attrs[\"%s%d\" % (name, chunk_id)] = chunk_data","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/saving/legacy_h5_format.py#L291-L327","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nmodel.save_weights('big.h5')  # RuntimeError: attributes too large\n# after\nmodel.save('big.keras')","handlingStrategy":"fallback","validationCode":"import h5py\nHDF5_OBJECT_HEADER_LIMIT = 64512\nnames = [w.name for l in model.layers for w in l.weights]\nif len('\\n'.join(names).encode()) > HDF5_OBJECT_HEADER_LIMIT:\n    model.save('m.keras')  # avoid the h5 path","typeGuard":null,"tryCatchPattern":"try:\n    model.save_weights('w.h5')\nexcept RuntimeError as e:\n    if 'larger than' not in str(e):\n        raise\n    model.save('m.keras')","preventionTips":["Assign short explicit layer names in large models","Use the .keras format for production checkpoints"],"tags":["keras","saving","hdf5","attribute-limit"],"backgroundTag":"hdf5-attribute-limit","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}