deepset-ai/haystack · error · ValueError

Device id must be an integer, got {device_id_str}

Error message

Device id must be an integer, got {device_id_str}

What it means

_split_device_string parses strings like 'cuda:0' into a device type and integer id. If the text after ':' is not a valid integer (e.g. 'cuda:zero', 'gpu:x'), int() fails and this ValueError is raised with the offending fragment.

Source

Thrown at haystack/utils/device.py:540

        return Device.mps()
    return Device.cpu()


def _split_device_string(string: str) -> tuple[str, int | None]:
    """
    Split a device string into device type and device id.

    :param string:
        The device string to split.
    :returns:
        The device type and device id, if any.
    """
    if ":" in string:
        device_type, device_id_str = string.split(":")
        try:
            device_id = int(device_id_str)
        except ValueError as e:
            raise ValueError(f"Device id must be an integer, got {device_id_str}") from e
    else:
        device_type = string
        device_id = None
    return device_type, device_id

View on GitHub (pinned to e318778c9b)

Solutions

  1. Use a numeric device id, e.g. 'cuda:0' instead of 'cuda:zero'.
  2. Omit the id entirely to mean the whole device type, e.g. 'cpu' or 'cuda'.
  3. Validate the device string format 'type[:int]' before calling from_str/from_hf.

Example fix

// before
ComponentDevice.from_str("cuda:zero")
// after
ComponentDevice.from_str("cuda:0")
Defensive patterns

Strategy: validation

Validate before calling

import re
assert re.fullmatch(r"[a-zA-Z]+(:\d+)?", device_str), f"invalid device string: {device_str}"

Type guard

def is_valid_device_string(s: str) -> bool:
    if ":" not in s:
        return s.isalpha()
    t, _, i = s.partition(":")
    return t.isalpha() and i.isdigit()

Try / catch

try:
    dev = ComponentDevice.from_str(s)
except ValueError:
    dev = ComponentDevice.from_str("cpu")

Prevention

When it happens

Trigger: Calling ComponentDevice.from_str("cuda:zero"), Device.from_str("cpu:abc"), or from_hf with a device string containing a non-numeric id after ':'.

Common situations: Typos in config files or CLI args; copying device names from logs that use word ids; localization mistakes like 'gpu:1' (full-width digit).

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/ff65801c8aecee2b. Report an issue: GitHub.