deepset-ai/haystack · error · ValueError
Only single devices can be converted to PyTorch format
Error message
Only single devices can be converted to PyTorch format
What it means
ComponentDevice.to_torch raises ValueError when the component holds a multi-device map rather than a single device, since a multi-device setup cannot be expressed as one torch.device.
Source
Thrown at haystack/utils/device.py:323
"""
if not (self._single_device is not None) ^ (self._multiple_devices is not None):
raise ValueError(
"The component device can neither be empty nor contain both a single device and a device map"
)
def to_torch(self) -> "torch.device":
"""
Convert the component device representation to PyTorch format.
Device maps are not supported.
:returns:
The PyTorch device representation.
"""
self._validate()
if self._single_device is None:
raise ValueError("Only single devices can be converted to PyTorch format")
torch_import.check()
assert self._single_device is not None
return torch.device(str(self._single_device))
def to_torch_str(self) -> str:
"""
Convert the component device representation to PyTorch string format.
Device maps are not supported.
:returns:
The PyTorch device string representation.
"""
self._validate()
if self._single_device is None:
raise ValueError("Only single devices can be converted to PyTorch format")View on GitHub (pinned to e318778c9b)
Solutions
- Use to_torch only on single-device components; inspect has_multiple_devices() first
- For multi-device setups, access ComponentDevice._multiple_devices/DeviceMap and handle per-device entries or rely on the HF/accelerate integration instead of to_torch
- Reconfigure the component to a single device if a single torch.device is required
Example fix
// before
comp = ComponentDevice.from_multiple(DeviceMap({"0": dev0, "1": dev1}))
torch_dev = comp.to_torch() # raises
// after
if comp.has_multiple_devices():
# handle the map explicitly
devices = comp._multiple_devices.devices
else:
torch_dev = comp.to_torch() Defensive patterns
Strategy: validation
Validate before calling
def torch_device_or_none(comp: ComponentDevice):
return comp.to_torch() if not comp.has_multiple_devices() else None Type guard
def has_single_device(comp: ComponentDevice) -> bool:
return comp._single_device is not None Try / catch
try:
dev = comp.to_torch()
except ValueError:
# multi-device: handle per-device or delegate to HF/accelerate
dev = None Prevention
- Check has_multiple_devices() before to_torch/to_torch_str
- Keep single-device and multi-device code paths separate
- For sharded models, rely on accelerate rather than a single torch.device
When it happens
Trigger: Calling to_torch() on a ComponentDevice built from a DeviceMap (from_multiple/from_hf with multiple devices).
Common situations: Pipeline configured with multi-GPU device maps; component-level utility assumes single device but the model was sharded across devices (e.g. via accelerate).
Related errors
- Unknown device type string '{string}'
- Device id must be >= 0, got {id}
- Only single devices can be converted to spaCy format
- The disk device can only be used as a part of device maps
- The component device can neither be empty nor contain both a
AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30).
Data as JSON: /api/errors/4f2ab2f26fa96e51.
Report an issue: GitHub.