{"record":{"id":"8c02d3124b1b3923","repo":"keras-team/keras","slug":"save-model-using-h5-format-requires-h5py-coul","errorCode":null,"errorMessage":"`save_model()` using h5 format requires h5py. Could not import h5py.","messagePattern":"`save_model\\(\\)` using h5 format requires h5py\\. Could not import h5py\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/saving/legacy_h5_format.py","lineNumber":30,"sourceCode":"from keras.src.legacy.saving import saving_utils\nfrom keras.src.saving import object_registration\nfrom keras.src.saving import serialization_lib\nfrom keras.src.saving.saving_lib import safe_get_h5_dataset\nfrom keras.src.saving.saving_lib import safe_get_h5_group\nfrom keras.src.utils import io_utils\n\ntry:\n    import h5py\nexcept ImportError:\n    h5py = None\n\n\nHDF5_OBJECT_HEADER_LIMIT = 64512\n\n\ndef save_model_to_hdf5(model, filepath, overwrite=True, include_optimizer=True):\n    if h5py is None:\n        raise ImportError(\n            \"`save_model()` using h5 format requires h5py. Could not \"\n            \"import h5py.\"\n        )\n\n    if not isinstance(filepath, h5py.File):\n        # If file exists and should not be overwritten.\n        if not overwrite and os.path.isfile(filepath):\n            proceed = io_utils.ask_to_proceed_with_overwrite(filepath)\n            if not proceed:\n                return\n\n        dirpath = os.path.dirname(filepath)\n        if dirpath and not os.path.exists(dirpath):\n            os.makedirs(dirpath, exist_ok=True)\n\n        f = h5py.File(filepath, mode=\"w\")\n        opened_new_file = True\n    else:","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/saving/legacy_h5_format.py#L12-L48","documentation":"The legacy HDF5 save path requires the optional h5py package. Keras imports h5py lazily; if the import failed, save_model_to_hdf5 raises ImportError at the top rather than crashing deep inside the writer.","triggerScenarios":"model.save('model.h5') or save_model_to_hdf5 in an environment where h5py is missing or failed to build (fresh venvs, Alpine, or a Python version with no wheel yet).","commonSituations":"Slim Docker images omitting h5py; Python upgraded before h5py wheels ship; broken h5py builds from HDF5 library mismatches.","solutions":["pip install h5py (prefer a prebuilt wheel; use a glibc-based image if the source build fails)","Save natively instead: model.save('model.keras') needs no h5py","Verify with python -c 'import h5py'"],"exampleFix":"# before\nmodel.save('model.h5')  # ImportError\n# after\n# pip install h5py, or:\nmodel.save('model.keras')","handlingStrategy":"fallback","validationCode":"try:\n    import h5py\nexcept ImportError:\n    raise SystemExit('pip install h5py or save as .keras')","typeGuard":null,"tryCatchPattern":"try:\n    model.save('m.h5')\nexcept ImportError:\n    model.save('m.keras')","preventionTips":["List h5py in requirements when using .h5 output","Add python -c 'import h5py' to Docker build health checks"],"tags":["keras","saving","hdf5","dependency"],"backgroundTag":"missing-optional-dependency","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}