{"record":{"id":"3b85be06a0f7464a","repo":"jax-ml/jax","slug":"device-or-sharding-was-passed-to-canonicali","errorCode":null,"errorMessage":"`{device_or_sharding = }` was passed to`canonicalize_or_get_default_platform`, only xc.Device, Sharding, None or str values are supported.","messagePattern":"`(.+?)` was passed to`canonicalize_or_get_default_platform`, only xc\\.Device, Sharding, None or str values are supported\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_constructors.py","lineNumber":360,"sourceCode":"  return out_array\n\n\ndef _get_platform(\n    device_or_sharding: xc.Device | Sharding | None | str) -> str:\n  \"\"\"Get device_or_sharding platform or look up config.default_device.value.\"\"\"\n  if isinstance(device_or_sharding, xc.Device):\n    return device_or_sharding.platform\n  elif isinstance(device_or_sharding, Sharding):\n    return list(device_or_sharding.device_set)[0].platform\n  elif isinstance(device_or_sharding, str):\n    return device_or_sharding\n  elif device_or_sharding is None:\n    if config.default_device.value is None:\n      return xla_bridge.default_backend()\n    else:\n      return _get_platform(config.default_device.value)\n  else:\n    raise ValueError(f\"`{device_or_sharding = }` was passed to\"\n                     \"`canonicalize_or_get_default_platform`, only xc.Device,\"\n                     \" Sharding, None or str values are supported.\")\n\n\ndef _convert_to_array_if_dtype_fails(x: ArrayLike) -> ArrayLike:\n  try:\n    dtypes.dtype(x)\n  except TypeError:\n    return np.asarray(x)\n  else:\n    return x\n\n\n@export\ndef asarray(a: Any, dtype: DTypeLike | None = None, order: str | None = None,\n            *, copy: bool | None = None,\n            device: xc.Device | Sharding | None = None,\n            out_sharding: NamedSharding | P | None = None) -> Array:","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_constructors.py#L342-L378","documentation":"_get_platform (used by asarray/array to resolve the target device) accepts only an xc.Device, a Sharding, None, or a platform string like 'cpu'/'gpu'/'tpu'. Any other type — int device id, torch device, etc. — cannot be canonicalized and raises ValueError.","triggerScenarios":"jnp.asarray(x, device=0) or jnp.asarray(x, device=torch.device('cuda')) — a device argument that is not Device/Sharding/None/str.","commonSituations":"Porting PyTorch or NumPy-CUDA code that uses integer device IDs; passing jax.local_devices(1)[0]'s id instead of the Device object.","solutions":["Use jax.devices('gpu')[0] to get a Device object, or the string platform name","For sharded output, pass a jax.sharding.Sharding instance","Wrap integer ids: jax.devices()[device_id]"],"exampleFix":"# before\na = jnp.asarray(x, device=0)\n# after\nimport jax\na = jnp.asarray(x, device=jax.devices('gpu')[0])","handlingStrategy":"type-guard","validationCode":"import jax\nfrom jax.sharding import Sharding\nfrom jaxlib.xla_extension import Device\ndef valid_device(d):\n    return d is None or isinstance(d, (Device, Sharding, str))","typeGuard":"def is_valid_jax_device(d) -> bool:\n    import jax\n    from jax.sharding import Sharding\n    return d is None or isinstance(d, (jax.Device, Sharding, str))","tryCatchPattern":"null","preventionTips":["Never pass integer device ids; use jax.devices(platform)[i]","Map torch device ids via jax.devices('gpu')[idx]"],"tags":["jax","device-validation","type-validation"],"backgroundTag":"invalid-device-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}