{"record":{"id":"c74890d6f6a47df4","repo":"deepset-ai/haystack","slug":"couldn-t-convert-huggingface-device-map-unexpect","errorCode":null,"errorMessage":"Couldn't convert HuggingFace device map - unexpected device '{str(device)}' for '{key}'","messagePattern":"Couldn't convert HuggingFace device map - unexpected device '(.+?)' for '(.+?)'","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"haystack/utils/device.py","lineNumber":241,"sourceCode":"        :param hf_device_map:\n            The HuggingFace device map.\n        :returns:\n            The deserialized device map.\n        :raises TypeError: If a device value in the map is not an int, str, or torch.device.\n        \"\"\"\n        mapping = {}\n        for key, device in hf_device_map.items():\n            if isinstance(device, int):\n                mapping[key] = Device(DeviceType.GPU, device)\n            elif isinstance(device, str):\n                device_type, device_id = _split_device_string(device)\n                mapping[key] = Device(DeviceType.from_str(device_type), device_id)\n            elif isinstance(device, torch.device):\n                device_type = device.type\n                device_id = device.index\n                mapping[key] = Device(DeviceType.from_str(device_type), device_id)\n            else:\n                raise TypeError(\n                    f\"Couldn't convert HuggingFace device map - unexpected device '{str(device)}' for '{key}'\"\n                )\n        return DeviceMap(mapping)\n\n\n@dataclass(frozen=True)\nclass ComponentDevice:\n    \"\"\"\n    A representation of a device for a component.\n\n    This can be either a single device or a device map.\n    \"\"\"\n\n    _single_device: Device | None = field(default=None)\n    _multiple_devices: DeviceMap | None = field(default=None)\n\n    @classmethod\n    def from_str(cls, device_str: str) -> \"ComponentDevice\":","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/utils/device.py#L223-L259","documentation":"DeviceMap.from_hf raises TypeError when converting a HuggingFace accelerate device_map entry whose value is neither an int, a device string, nor a torch.device. Accelerate accepts some values (like 'disk' or exotic objects) Haystack cannot map to its Device representation.","triggerScenarios":"Calling ComponentDevice.from_hf(device_map=...) with an accelerate device_map containing unexpected per-layer values, e.g. values of an unsupported type, empty/None entries, or device strings Haystack cannot parse.","commonSituations":"Loading a large model with device_map='auto' from accelerate and passing the resulting mapping into Haystack; accelerate produced an entry referencing storage Haystack does not model; version mismatch between accelerate and haystack's device handling.","solutions":["Inspect the device_map dict and normalize entries to ints or plain device strings ('cuda:0', 'cpu', 'disk') before calling from_hf","Recreate the device_map with explicit values instead of 'auto', e.g. {'': 'cuda:0'}","Upgrade haystack and/or accelerate so the device map formats match","Convert the map manually: build haystack DeviceMap from parsed Device entries"],"exampleFix":"// before\ncomp = ComponentDevice.from_hf(device_map=auto_map)  # entries like {'model.layers': 'meta'}\n// after\ndevice_map = {'': 'cuda:0'}\ncomp = ComponentDevice.from_hf(device_map=device_map)","handlingStrategy":"type-guard","validationCode":"ALLOWED = (int, str, torch.device) if torch else (int, str)\ndef is_hf_convertible(device_map: dict) -> bool:\n    return all(isinstance(v, ALLOWED) for v in device_map.values())","typeGuard":"def is_convertible_device(v: object) -> bool:\n    return isinstance(v, (int, str)) or (torch and isinstance(v, torch.device))","tryCatchPattern":"try:\n    comp = ComponentDevice.from_hf(device_map)\nexcept TypeError as e:\n    print(f\"Device map not convertible: {e}\")\n    comp = ComponentDevice.from_str(\"cpu\")","preventionTips":["Prefer explicit device_map values (ints or 'cuda:0'-style strings) over 'auto' output","Inspect auto-generated accelerate maps before passing them to Haystack","Keep accelerate and haystack versions compatible"],"tags":["device","huggingface","accelerate","type-error"],"backgroundTag":"hf-device-map-conversion-failed","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}