keras-team/keras · error · ValueError

No model config found in the file at {filepath}.

Error message

No model config found in the file at {filepath}.

What it means

An .h5 whole-model file stores the architecture in the 'model_config' HDF5 attribute at the file root. load_model_from_hdf5 raises ValueError when that attribute is missing, i.e. the file is not a Keras whole-model save.

Source

Thrown at keras/src/legacy/saving/legacy_h5_format.py:129

    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 = None
    try:
        # instantiate model
        model_config = f.attrs.get("model_config")
        if model_config is None:
            raise ValueError(
                f"No model config found in the file at {filepath}."
            )
        if hasattr(model_config, "decode"):
            model_config = model_config.decode("utf-8")
        model_config = json_utils.decode(model_config)

        legacy_scope = saving_options.keras_option_scope(use_legacy_config=True)
        safe_mode_scope = serialization_lib.SafeModeScope(safe_mode)
        with legacy_scope, safe_mode_scope:
            model = saving_utils.model_from_config(
                model_config, custom_objects=custom_objects
            )

            # set weights
            load_weights_from_hdf5_group(
                safe_get_h5_group(f, "model_weights"), model
            )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. If the file is weights-only, rebuild the architecture in code and call model.load_weights('file.h5')
  2. Re-save a full model from the original training environment with model.save('model.h5')
  3. Inspect first: h5py.File(path).attrs.keys() should contain 'model_config'

Example fix

# before
model = keras.saving.load_model('weights_only.h5')
# after
model = build_model()
model.load_weights('weights_only.h5')
Defensive patterns

Strategy: validation

Validate before calling

import h5py
with h5py.File(path, 'r') as f:
    if 'model_config' not in f.attrs:
        model = build_model(); model.load_weights(path)
    else:
        model = keras.saving.load_model(path)

Type guard

def is_full_model_h5(path):
    import h5py
    with h5py.File(path, 'r') as f:
        return 'model_config' in f.attrs

Try / catch

try:
    keras.saving.load_model(p)
except ValueError as e:
    if 'No model config' not in str(e):
        raise
    m = build_model(); m.load_weights(p)

Prevention

When it happens

Trigger: load_model on an .h5 file that contains only weights (saved via save_weights), a raw HDF5 dataset, or a file whose root attributes were stripped by conversion tooling.

Common situations: Confusing a weights-only checkpoint with a full-model save; expecting architecture reconstruction from a weights file.

Related errors


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