{"record":{"id":"d0f3c2f8e9919446","repo":"keras-team/keras","slug":"no-model-config-found-in-the-file-at-filepath","errorCode":null,"errorMessage":"No model config found in the file at {filepath}.","messagePattern":"No model config found in the file at (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/saving/legacy_h5_format.py","lineNumber":129,"sourceCode":"    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\n    try:\n        # instantiate model\n        model_config = f.attrs.get(\"model_config\")\n        if model_config is None:\n            raise ValueError(\n                f\"No model config found in the file at {filepath}.\"\n            )\n        if hasattr(model_config, \"decode\"):\n            model_config = model_config.decode(\"utf-8\")\n        model_config = json_utils.decode(model_config)\n\n        legacy_scope = saving_options.keras_option_scope(use_legacy_config=True)\n        safe_mode_scope = serialization_lib.SafeModeScope(safe_mode)\n        with legacy_scope, safe_mode_scope:\n            model = saving_utils.model_from_config(\n                model_config, custom_objects=custom_objects\n            )\n\n            # set weights\n            load_weights_from_hdf5_group(\n                safe_get_h5_group(f, \"model_weights\"), model\n            )\n","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/saving/legacy_h5_format.py#L111-L147","documentation":"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.","triggerScenarios":"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.","commonSituations":"Confusing a weights-only checkpoint with a full-model save; expecting architecture reconstruction from a weights file.","solutions":["If the file is weights-only, rebuild the architecture in code and call model.load_weights('file.h5')","Re-save a full model from the original training environment with model.save('model.h5')","Inspect first: h5py.File(path).attrs.keys() should contain 'model_config'"],"exampleFix":"# before\nmodel = keras.saving.load_model('weights_only.h5')\n# after\nmodel = build_model()\nmodel.load_weights('weights_only.h5')","handlingStrategy":"validation","validationCode":"import h5py\nwith h5py.File(path, 'r') as f:\n    if 'model_config' not in f.attrs:\n        model = build_model(); model.load_weights(path)\n    else:\n        model = keras.saving.load_model(path)","typeGuard":"def is_full_model_h5(path):\n    import h5py\n    with h5py.File(path, 'r') as f:\n        return 'model_config' in f.attrs","tryCatchPattern":"try:\n    keras.saving.load_model(p)\nexcept ValueError as e:\n    if 'No model config' not in str(e):\n        raise\n    m = build_model(); m.load_weights(p)","preventionTips":["Name weights-only files *.weights.h5 to distinguish them from whole-model saves"],"tags":["keras","loading","hdf5","model-config"],"backgroundTag":"invalid-file-format","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}