{"record":{"id":"3e3333f607d4de0e","repo":"deepset-ai/haystack","slug":"device-id-must-be-0-got-id","errorCode":null,"errorMessage":"Device id must be >= 0, got {id}","messagePattern":"Device id must be >= 0, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/utils/device.py","lineNumber":78,"sourceCode":"        The device type.\n    :param id:\n        The optional device id.\n    \"\"\"\n\n    type: DeviceType\n    id: int | None = field(default=None)\n\n    def __init__(self, type: DeviceType, id: int | None = None) -> None:  # noqa:A002\n        \"\"\"\n        Create a generic device.\n\n        :param type:\n            The device type.\n        :param id:\n            The device id.\n        \"\"\"\n        if id is not None and id < 0:\n            raise ValueError(f\"Device id must be >= 0, got {id}\")\n\n        self.type = type\n        self.id = id\n\n    def __str__(self) -> str:\n        if self.id is None:\n            return str(self.type)\n        return f\"{self.type}:{self.id}\"\n\n    @staticmethod\n    def cpu() -> \"Device\":\n        \"\"\"\n        Create a generic CPU device.\n\n        :returns:\n            The CPU device.\n        \"\"\"\n        return Device(DeviceType.CPU)","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/utils/device.py#L60-L96","documentation":"Device.__init__ raises ValueError when a device id is provided that is negative. Device ids (GPU ordinal, XPU index, etc.) must be >= 0; only None is allowed for devices without an index (e.g. CPU).","triggerScenarios":"Constructing Device(type, id) with a negative id, e.g. Device(DeviceType.GPU, -1) or parsing a device string with a negative ordinal; often results from sentinel values (-1) used elsewhere leaking into Device construction.","commonSituations":"Using -1 as a 'no device' sentinel from other libraries (spaCy uses -1 for CPU) and passing it to Device; parsing malformed config values like 'cuda:-1'.","solutions":["Pass None instead of a negative number when the device has no id","Fix the upstream logic that produced the sentinel (e.g. spaCy's -1 means CPU → use DeviceType.CPU with id None)","Validate/parse the id before constructing Device"],"exampleFix":"// before\ndev = Device(DeviceType.GPU, -1)\n// after\ndev = Device(DeviceType.CPU, None)  # or omit id for devices without an index","handlingStrategy":"validation","validationCode":"def safe_device(dtype, id):\n    if id is not None and id < 0:\n        raise ValueError(f\"refusing negative device id {id}\")\n    return Device(dtype, id)","typeGuard":"def is_valid_device_id(id: object) -> bool:\n    return id is None or (isinstance(id, int) and not isinstance(id, bool) and id >= 0)","tryCatchPattern":"try:\n    dev = Device(dtype, raw_id)\nexcept ValueError as e:\n    print(f\"Invalid device id: {e}; falling back to CPU\")\n    dev = Device(DeviceType.CPU, None)","preventionTips":["Use None, not -1, for devices without an index","Map other frameworks' sentinels (e.g. spaCy -1 = CPU) explicitly before constructing Device","Clamp/validate ordinals parsed from config"],"tags":["device","pytorch","validation","value-error"],"backgroundTag":"invalid-device-index","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}