{"record":{"id":"82240950ef8ac175","repo":"jax-ml/jax","slug":"can-only-convert-to-extended-dtype-from-an-array-o","errorCode":null,"errorMessage":"can only convert to extended dtype from an array of its representation type, but the extended dtype {dtype_to_string(edtype)} has a representation shape {rep_aval.shape} (rank {rep_aval.ndim}) while the given representation array has shape {x.shape} (rank {x.ndim} < {rep_aval.ndim}).","messagePattern":"can only convert to extended dtype from an array of its representation type, but the extended dtype (.+?) has a representation shape (.+?) \\(rank (.+?)\\) while the given representation array has shape (.+?) \\(rank (.+?) < (.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5515,"sourceCode":"                    f\"but got {type(x)}\")\n  if convert_to := getattr(edtype._rules, 'convert_to', None):\n    allow_conversion = convert_to(x.dtype, edtype)\n  else:\n    allow_conversion = edtype._rules.allow_conversion\n  if not allow_conversion:\n    raise ValueError(\n        f\"Cannot convert_element_type from {dtype_to_string(x.dtype)} \"\n        f\"to {dtype_to_string(edtype)}\")\n  rep_aval = core.physical_element_aval(edtype)\n  assert tuple(rep_aval.sharding.spec) == (None,) * rep_aval.ndim\n  if x.dtype != rep_aval.dtype:\n    raise ValueError(\n        \"can only convert to extended dtype from its representation dtype, \"\n        f\"but tried to convert from {dtype_to_string(x.dtype)} to \"\n        f\"{dtype_to_string(edtype)} which doesn't match the representation type \"\n        f\"{dtype_to_string(rep_aval.dtype)}.\")\n  if x.ndim < rep_aval.ndim:\n    raise ValueError(\n        \"can only convert to extended dtype from an array of its \"\n        f\"representation type, but the extended dtype {dtype_to_string(edtype)}\"\n        f\" has a representation shape {rep_aval.shape} (rank {rep_aval.ndim}) \"\n        f\"while the given representation array has shape {x.shape} (rank \"\n        f\"{x.ndim} < {rep_aval.ndim}).\")\n  n = x.ndim - rep_aval.ndim\n  shape_prefix, shape_suffix = x.shape[:n], x.shape[n:]\n  if shape_suffix != rep_aval.shape:\n    raise ValueError(\n        \"can only convert to extended dtype from an array of its \"\n        f\"representation type, but the extended dtype {dtype_to_string(edtype)}\"\n        f\" has a representation shape {rep_aval.shape} while the given \"\n        f\"representation array has shape {x.shape}, so the shape suffix \"\n        f\"does not match: given {shape_suffix} but required {rep_aval.shape}.\")\n  if isinstance(x, ShapedArray):\n    spec_prefix, spec_suffix = x.sharding.spec[:n], x.sharding.spec[n:]\n    if tuple(spec_suffix) != (None,) * len(spec_suffix):\n      raise ValueError(","sourceCodeStart":5497,"sourceCodeEnd":5533,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5497-L5533","documentation":"Some extended dtypes have multi-element representation shapes (an element occupies several physical elements, e.g. a complex-of-float8 stored as 2 uint8s). To convert into such a dtype, the input array must have at least as many trailing dimensions as the representation rank; if x.ndim < rep_aval.ndim, ValueError is raised.","triggerScenarios":"convert_element_type on a scalar or low-rank array to an extended dtype whose representation shape has rank > 0, e.g. shape () or (4,) into an edtype with representation shape (2,).","commonSituations":"Working with custom composite extended dtypes (e.g. pair/triple representations); assuming extended elements are always scalar-represented; flattening arrays before conversion.","solutions":["Ensure the input has at least rep_aval.ndim trailing dimensions (append axes with [...] if semantics allow)","Reshape so the last dims match the representation shape","Check core.physical_element_aval(edtype).ndim to know the required rank","Split into per-component conversions if the composite conversion is not needed"],"exampleFix":"// before\nout = lax.convert_element_type(scalar_x, composite_edtype)  # rep rank 2\n\n// after\nout = lax.convert_element_type(scalar_x[None, None], composite_edtype)","handlingStrategy":"validation","validationCode":"from jax.core import physical_element_aval\nrep = physical_element_aval(edtype)\nassert x.ndim >= rep.ndim, f'need rank >= {rep.ndim}'","typeGuard":null,"tryCatchPattern":"try:\n    out = lax.convert_element_type(x, edtype)\nexcept ValueError:\n    out = lax.convert_element_type(x[..., None, None], edtype)  # pad to rep rank","preventionTips":["Know each extended dtype's representation rank before converting","Avoid flattening/scalarizing arrays destined for composite dtypes","Assert rank preconditions in data-loading code"],"tags":["jax","extended-dtype","shape-validation","rank-mismatch"],"backgroundTag":"shape-rank-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}