keras-team/keras · error · ValueError

Invalid value for argument `device_name`. Expected a string…

Error message

Invalid value for argument `device_name`. Expected a string like 'gpu:0' or a `jax.Device` instance. Received: device_name='{device_name}'

What it means

Raised by device_scope in the JAX ops module when device_name is neither a 'device_type:index' string (like 'gpu:0') nor a jax.Device instance. The scope must resolve to a concrete device, and the type/value guard rejects anything else.

Solutions

  1. Pass a lowercase string like 'gpu:0'/'cpu:1' or a jax.Device from jax.devices().
  2. Enumerate valid devices with jax.devices() and pass one directly.
  3. Don't request device types your host lacks (e.g. 'tpu' without TPUs).
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at keras/src/backend/jax/ops/core.py:729 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/5075a9cfbf8efa1e. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/jax/ops/core.py:729

                return self
        name_scope_stack.append(self)
        self._pop_on_exit = True
        self._jax_name_scope.__enter__()
        return self

    def __exit__(self, *args, **kwargs):
        super().__exit__(*args, **kwargs)
        if self._pop_on_exit:
            self._jax_name_scope.__exit__(*args, **kwargs)


def device_scope(device_name):
    if isinstance(device_name, str):
        # We support string value like "cpu:0", "gpu:1", etc.
        device_name = device_name.lower()
        jax_device = distribution_lib._to_backend_device(device_name)
    elif not isinstance(device_name, jax.Device):
        raise ValueError(
            "Invalid value for argument `device_name`. "
            "Expected a string like 'gpu:0' or a `jax.Device` instance. "
            f"Received: device_name='{device_name}'"
        )
    else:
        jax_device = device_name
    return jax.default_device(jax_device)

View on GitHub (pinned to 7a34a03db6)