keras-team/keras · error · ImportError
`load_model()` using h5 format requires h5py. Could not impo
Error message
`load_model()` using h5 format requires h5py. Could not import h5py.
What it means
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.
Source
Thrown at keras/src/legacy/saving/legacy_h5_format.py:106
(strings) to custom classes or functions to be
considered during deserialization.
compile: Boolean, whether to compile the model
after loading.
Returns:
A Keras model instance. If an optimizer was found
as part of the saved model, the model is already
compiled. Otherwise, the model is uncompiled and
a warning will be displayed. When `compile` is set
to `False`, the compilation is omitted without any
warning.
Raises:
ImportError: if h5py is not available.
ValueError: In case of an invalid savefile.
"""
if h5py is None:
raise ImportError(
"`load_model()` using h5 format requires h5py. Could not "
"import h5py."
)
if not custom_objects:
custom_objects = {}
gco = object_registration.GLOBAL_CUSTOM_OBJECTS
tlco = global_state.get_global_attribute("custom_objects_scope_dict", {})
custom_objects = {**custom_objects, **gco, **tlco}
opened_new_file = not isinstance(filepath, h5py.File)
if opened_new_file:
f = h5py.File(filepath, mode="r")
else:
f = filepath
model = NoneView on GitHub (pinned to 7a34a03db6)
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
Example fix
# before
model = keras.saving.load_model('model.h5') # ImportError
# after
# pip install h5py
model = keras.saving.load_model('model.h5') Defensive patterns
Strategy: fallback
Validate before calling
try:
import h5py
except ImportError:
raise SystemExit('pip install h5py or convert the file to .keras') Try / catch
try:
keras.saving.load_model('m.h5')
except ImportError:
raise SystemExit('h5py missing: pip install h5py') Prevention
- Add h5py to inference image requirements
When it happens
Trigger: keras.saving.load_model('model.h5') or load_model_from_hdf5 on a machine without h5py, e.g. a minimal inference container.
Common situations: Training image had h5py but the deployment image does not; CI runners installing only runtime deps.
Related errors
- `save_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
- Weight count mismatch for layer #{k} (named {layer.name} in
- You called `set_weights(weights)` on layer '{self.name}' wit
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/1bac2f3444d7129b.
Report an issue: GitHub.