{"record":{"id":"1bac2f3444d7129b","repo":"keras-team/keras","slug":"load-model-using-h5-format-requires-h5py-coul","errorCode":null,"errorMessage":"`load_model()` using h5 format requires h5py. Could not import h5py.","messagePattern":"`load_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":106,"sourceCode":"            (strings) to custom classes or functions to be\n            considered during deserialization.\n        compile: Boolean, whether to compile the model\n            after loading.\n\n    Returns:\n        A Keras model instance. If an optimizer was found\n        as part of the saved model, the model is already\n        compiled. Otherwise, the model is uncompiled and\n        a warning will be displayed. When `compile` is set\n        to `False`, the compilation is omitted without any\n        warning.\n\n    Raises:\n        ImportError: if h5py is not available.\n        ValueError: In case of an invalid savefile.\n    \"\"\"\n    if h5py is None:\n        raise ImportError(\n            \"`load_model()` using h5 format requires h5py. Could not \"\n            \"import h5py.\"\n        )\n\n    if not custom_objects:\n        custom_objects = {}\n\n    gco = object_registration.GLOBAL_CUSTOM_OBJECTS\n    tlco = global_state.get_global_attribute(\"custom_objects_scope_dict\", {})\n    custom_objects = {**custom_objects, **gco, **tlco}\n\n    opened_new_file = not isinstance(filepath, h5py.File)\n    if opened_new_file:\n        f = h5py.File(filepath, mode=\"r\")\n    else:\n        f = filepath\n\n    model = None","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/saving/legacy_h5_format.py#L88-L124","documentation":"Mirror of the save-side check: loading a legacy .h5 model requires h5py to read the file, and Keras raises ImportError at the top of load_model_from_hdf5 if the h5py import failed.","triggerScenarios":"keras.saving.load_model('model.h5') or load_model_from_hdf5 on a machine without h5py, e.g. a minimal inference container.","commonSituations":"Training image had h5py but the deployment image does not; CI runners installing only runtime deps.","solutions":["pip install h5py in the loading environment","Add h5py to deployment requirements if you ship .h5 checkpoints","Convert .h5 files to .keras once, in an environment that has h5py, to drop the dependency"],"exampleFix":"# before\nmodel = keras.saving.load_model('model.h5')  # ImportError\n# after\n# pip install h5py\nmodel = keras.saving.load_model('model.h5')","handlingStrategy":"fallback","validationCode":"try:\n    import h5py\nexcept ImportError:\n    raise SystemExit('pip install h5py or convert the file to .keras')","typeGuard":null,"tryCatchPattern":"try:\n    keras.saving.load_model('m.h5')\nexcept ImportError:\n    raise SystemExit('h5py missing: pip install h5py')","preventionTips":["Add h5py to inference image requirements"],"tags":["keras","loading","hdf5","dependency"],"backgroundTag":"missing-optional-dependency","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}