{"record":{"id":"3ef2d769641db167","repo":"keras-team/keras","slug":"the-model-contains-two-variables-with-a-duplicate","errorCode":null,"errorMessage":"The model contains two variables with a duplicate path: path='{v.path}' appears at least twice. This path is used for {v} and for {store[v.path]}. In order to get a variable map, make sure to use unique paths/names for each variable.","messagePattern":"The model contains two variables with a duplicate path: path='(.+?)' appears at least twice\\. This path is used for (.+?) and for (.+?)\\. In order to get a variable map, make sure to use unique paths/names for each variable\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/models/variable_mapping.py","lineNumber":26,"sourceCode":"def map_saveable_variables(saveable, store, visited_saveables):\n    # If the saveable has already been seen, skip it.\n    if id(saveable) in visited_saveables:\n        return\n\n    visited_saveables.add(id(saveable))\n\n    variables = []\n    if isinstance(saveable, Layer):\n        variables = (\n            saveable._trainable_variables + saveable._non_trainable_variables\n        )\n    elif isinstance(saveable, Optimizer):\n        variables = saveable._variables\n    elif isinstance(saveable, Metric):\n        variables = saveable._variables\n    for v in variables:\n        if v.path in store:\n            raise ValueError(\n                \"The model contains two variables with a duplicate path: \"\n                f\"path='{v.path}' appears at least twice. \"\n                f\"This path is used for {v} and for {store[v.path]}. \"\n                \"In order to get a variable map, make sure to use \"\n                \"unique paths/names for each variable.\"\n            )\n        store[v.path] = v\n\n    # Recursively save state of children saveables (layers, optimizers, etc.)\n    for child_attr, child_obj in saving_lib._walk_saveable(saveable):\n        if isinstance(child_obj, KerasSaveable):\n            map_saveable_variables(\n                child_obj,\n                store,\n                visited_saveables=visited_saveables,\n            )\n        elif isinstance(child_obj, (list, dict, tuple, set)):\n            map_container_variables(","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/models/variable_mapping.py#L8-L44","documentation":"When saving/loading, Keras builds a map from variable path to variable. Two variables resolving to the same path (duplicate layer names or nested naming collisions) make the map ambiguous, so the operation is rejected.","triggerScenarios":"Two layers or variables end up with the same .path (e.g. the same explicit name used twice), then model.save_variables() or checkpoint mapping","commonSituations":"Checkpointing after manual layer renaming, sharing submodules across models, or migrating TF1 names to Keras 3 paths","solutions":["Rename layers/variables so every v.path is unique","Let Keras auto-generate names (don't reuse the same explicit name)","Rebuild the model with unique names before saving or checkpointing"],"exampleFix":"# before\nd1 = keras.layers.Dense(4, name='block')\nd2 = keras.layers.Dense(4, name='block')\n\n# after\nd1 = keras.layers.Dense(4, name='block_a')\nd2 = keras.layers.Dense(4, name='block_b')","handlingStrategy":"validation","validationCode":"paths = [v.path for v in model.variables]\nassert len(paths) == len(set(paths)), f'duplicate paths: {paths}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Give every layer and nested variable a unique name","After loading a checkpoint, verify the count of unique variable paths equals variable count"],"tags":["keras","variables","checkpointing","naming"],"backgroundTag":"duplicate-variable-names","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}