keras-team/keras · error · ValueError

Invalid `value_format` argument. Expected one of

Error message

Invalid `value_format` argument. Expected one of {'numpy_array', 'backend_tensor'}. Received: value_format={value_format}

What it means

_create_nested_dict (used by get_state_tree) validates `value_format`, which controls whether variables are returned as numpy arrays or backend tensors. Only "numpy_array" and "backend_tensor" are accepted; any other string raises this ValueError.

Solutions

  1. Pass value_format="numpy_array" or value_format="backend_tensor"
  2. Fix casing/typos in the format string
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/models/model.py:1155 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/69524c7da0209997. Report an issue: GitHub.

Appendix: source

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

        )
        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():
            parts = path.split("/")
            current_dict = nested_dict
            for part in parts[:-1]:
                if part not in current_dict:
                    current_dict[part] = {}
                current_dict = current_dict[part]
            current_dict[parts[-1]] = value

        return nested_dict

    def set_state_tree(self, state_tree):

View on GitHub (pinned to 7a34a03db6)