keras-team/keras · error · ValueError

should be a TensorLayout type, got

Error message

{layout} should be a TensorLayout type, got {type(layout)}

What it means

Raised by LayoutMap.__setitem__ in keras/src/distribution/distribution_lib.py: values may be a TensorLayout, or a shortcut tuple (sharded_axes, replicated_axes) that gets converted automatically — but the inserted value is neither. The item being stored is of an unsupported type (e.g. a string, list, or LayoutMap), so the assignment is rejected.

Solutions

  1. Pass a TensorLayout(sharded_axes=..., replicated_axes=...) instance
  2. Or use the tuple shortcut: layout_map['key'] = ('batch', None)
  3. Wrap plain values in TensorLayout before insertion
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/distribution/distribution_lib.py:774 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/eb8a9a26c978a510. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/distribution/distribution_lib.py:774

    def __setitem__(self, key, layout):
        """Insert TensorLayout to the LayoutMap.

        Args:
            key: String key for the `TensorLayout`.
            layout: The `TensorLayout`. As a shortcut, tuple of string and None
                are also acceptable, and will be converted to `TensorLayout`.
        """
        if key in self._layout_map:
            raise ValueError(
                f"{key} already exist in the LayoutMap with "
                f"value {self._layout_map[key]}. Please make sure to "
                "not use duplicated keys."
            )
        if isinstance(layout, tuple):
            layout = TensorLayout(axes=layout, device_mesh=None)

        if not isinstance(layout, TensorLayout):
            raise ValueError(
                f"{layout} should be a TensorLayout type, got {type(layout)}"
            )
        self._maybe_populate_device_mesh(layout)
        self._layout_map[key] = layout

    def __delitem__(self, key):
        # let the dict to handle the key missing error
        return self._layout_map.pop(key)

    def __len__(self):
        return len(self._layout_map)

    def __iter__(self):
        return iter(self._layout_map)

    @property
    def device_mesh(self):
        return self._device_mesh

View on GitHub (pinned to 7a34a03db6)