keras-team/keras · error · ValueError

The model contains two variables with a duplicate path: path

Error message

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.

What it means

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.

Source

Thrown at keras/src/models/variable_mapping.py:26

def map_saveable_variables(saveable, store, visited_saveables):
    # If the saveable has already been seen, skip it.
    if id(saveable) in visited_saveables:
        return

    visited_saveables.add(id(saveable))

    variables = []
    if isinstance(saveable, Layer):
        variables = (
            saveable._trainable_variables + saveable._non_trainable_variables
        )
    elif isinstance(saveable, Optimizer):
        variables = saveable._variables
    elif isinstance(saveable, Metric):
        variables = saveable._variables
    for v in variables:
        if v.path in store:
            raise ValueError(
                "The model contains two variables with a duplicate path: "
                f"path='{v.path}' appears at least twice. "
                f"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."
            )
        store[v.path] = v

    # Recursively save state of children saveables (layers, optimizers, etc.)
    for child_attr, child_obj in saving_lib._walk_saveable(saveable):
        if isinstance(child_obj, KerasSaveable):
            map_saveable_variables(
                child_obj,
                store,
                visited_saveables=visited_saveables,
            )
        elif isinstance(child_obj, (list, dict, tuple, set)):
            map_container_variables(

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Rename layers/variables so every v.path is unique
  2. Let Keras auto-generate names (don't reuse the same explicit name)
  3. Rebuild the model with unique names before saving or checkpointing

Example fix

# before
d1 = keras.layers.Dense(4, name='block')
d2 = keras.layers.Dense(4, name='block')

# after
d1 = keras.layers.Dense(4, name='block_a')
d2 = keras.layers.Dense(4, name='block_b')
Defensive patterns

Strategy: validation

Validate before calling

paths = [v.path for v in model.variables]
assert len(paths) == len(set(paths)), f'duplicate paths: {paths}'

Prevention

When it happens

Trigger: 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

Common situations: Checkpointing after manual layer renaming, sharing submodules across models, or migrating TF1 names to Keras 3 paths

Related errors


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