{"record":{"id":"7c5c7876fa33f2fe","repo":"deepset-ai/haystack","slug":"only-single-devices-can-be-converted-to-spacy-form","errorCode":null,"errorMessage":"Only single devices can be converted to spaCy format","messagePattern":"Only single devices can be converted to spaCy format","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/utils/device.py","lineNumber":358,"sourceCode":"        if self._single_device is None:\n            raise ValueError(\"Only single devices can be converted to PyTorch format\")\n\n        assert self._single_device is not None\n        return str(self._single_device)\n\n    def to_spacy(self) -> int:\n        \"\"\"\n        Convert the component device representation to spaCy format.\n\n        Device maps are not supported.\n\n        :returns:\n            The spaCy device representation.\n        \"\"\"\n        self._validate()\n\n        if self._single_device is None:\n            raise ValueError(\"Only single devices can be converted to spaCy format\")\n\n        assert self._single_device is not None\n        if self._single_device.type == DeviceType.GPU:\n            assert self._single_device.id is not None\n            return self._single_device.id\n        return -1\n\n    def to_hf(self) -> int | str | dict[str, int | str]:\n        \"\"\"\n        Convert the component device representation to HuggingFace format.\n\n        :returns:\n            The HuggingFace device representation.\n        \"\"\"\n        self._validate()\n\n        def convert_device(device: Device, *, gpu_id_only: bool = False) -> int | str:\n            if gpu_id_only and device.type == DeviceType.GPU:","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/utils/device.py#L340-L376","documentation":"ComponentDevice.to_spacy raises ValueError when the component is not a single device; spaCy's device API (a GPU ordinal int, -1 for CPU) cannot express a multi-device map.","triggerScenarios":"Calling to_spacy() on a ComponentDevice holding a DeviceMap (from_multiple/from_hf).","commonSituations":"Bridging Haystack device config into spaCy-based components (e.g. tokenization/embedding) while the pipeline is configured for multi-GPU sharding.","solutions":["Use to_spacy only on single-device components; guard with has_multiple_devices()","Pick one concrete device from the DeviceMap and build ComponentDevice.from_single(Device(...)) for the spaCy component","Configure the spaCy portion to CPU (-1) when the pipeline is multi-device"],"exampleFix":"// before\nspacy_dev = comp.to_spacy()  # multi-device -> raises\n// after\nif comp.has_multiple_devices():\n    spacy_dev = -1  # spaCy cannot use multiple devices; fall back to CPU\nelse:\n    spacy_dev = comp.to_spacy()","handlingStrategy":"validation","validationCode":"def spacy_device_or_cpu(comp: ComponentDevice) -> int:\n    return comp.to_spacy() if not comp.has_multiple_devices() else -1","typeGuard":"def supports_spacy_conversion(comp: ComponentDevice) -> bool:\n    return comp._single_device is not None","tryCatchPattern":"try:\n    gpu_id = comp.to_spacy()\nexcept ValueError:\n    gpu_id = -1  # spaCy: -1 means CPU","preventionTips":["Guard with has_multiple_devices() before calling to_spacy","Remember spaCy only accepts a GPU ordinal or -1 (CPU)","Use a single concrete device for spaCy-backed components"],"tags":["device","spacy","multi-gpu","value-error"],"backgroundTag":"invalid-device-configuration","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}