keras-team/keras · error · ValueError

The following variable path is found twice in the model

Error message

The following variable path is found twice in the model: '{v.path}'. `get_state_tree()` can only be called when all variable paths are unique. Make sure to give unique names to your layers (and other objects).

What it means

Error "The following variable path is found twice in the model: '{v.path}'. `get_state_tree()` can only be called when all variable paths are unique. Make sure to give unique names to your layers (and other objects)." thrown in keras-team/keras.

Solutions

  1. Give unique names to layers/objects so each variable path is unique
  2. Check for the same layer instance added twice to the model
  3. Rename the colliding layer via its name argument before rebuilding the state tree
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/models/model.py:1144 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/models/model.py:1144

        variables["trainable_variables"] = self._create_nested_dict(
            self.trainable_variables, value_format
        )
        variables["non_trainable_variables"] = self._create_nested_dict(
            self.non_trainable_variables, value_format
        )
        variables["optimizer_variables"] = self._create_nested_dict(
            self.optimizer.variables, value_format
        )
        variables["metrics_variables"] = self._create_nested_dict(
            self.metrics_variables, value_format
        )
        return variables

    def _create_nested_dict(self, variables, value_format):
        flat_dict = {}
        for v in variables:
            if v.path in flat_dict:
                raise ValueError(
                    "The following variable path is found twice in the model: "
                    f"'{v.path}'. `get_state_tree()` can only be called when "
                    "all variable paths are unique. Make sure to give unique "
                    "names to your layers (and other objects)."
                )
            if value_format == "backend_tensor":
                flat_dict[v.path] = v.value
            elif value_format == "numpy_array":
                flat_dict[v.path] = v.numpy()
            else:
                raise ValueError(
                    "Invalid `value_format` argument. Expected one of "
                    "{'numpy_array', 'backend_tensor'}. Received: "
                    f"value_format={value_format}"
                )

        nested_dict = {}
        for path, value in flat_dict.items():

View on GitHub (pinned to 7a34a03db6)