{"record":{"id":"557b7fb9dcf93a88","repo":"jax-ml/jax","slug":"can-only-convert-to-extended-dtype-from-an-array-o-557b7f","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} while the given representation array has shape {x.shape}, so the shape suffix does not match: given {shape_suffix} but required {rep_aval.shape}.","messagePattern":"can only convert to extended dtype from an array of its representation type, but the extended dtype (.+?) has a representation shape (.+?) while the given representation array has shape (.+?), so the shape suffix does not match: given (.+?) but required (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5524,"sourceCode":"  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(\n          \"can only convert to extended dtype from an array with trailing \"\n          \"axes that are not explicitly sharded, but tried to convert from \"\n          f\"{x.str_short(short_dtypes=True)} to an extended dtype with element \"\n          f\"shape {rep_aval.shape}\")\n    return x.update(shape=shape_prefix, dtype=edtype,\n                    sharding=x.sharding.update(spec=spec_prefix))\n  else:\n    assert False  # unreachable, see isinstance check above\n","sourceCodeStart":5506,"sourceCodeEnd":5542,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5506-L5542","documentation":"For extended dtypes with multi-element representation, the trailing shape of the input must exactly equal the representation shape. After peeling off the leading dims, if the suffix (x.shape[n:]) != rep_aval.shape, the array cannot be reinterpreted as elements of that extended dtype.","triggerScenarios":"convert_element_type(x, edtype) where x.shape[-k:] doesn't match the representation shape, e.g. shape (2, 5) with representation (2,) — the last dim must be 2 but is 5.","commonSituations":"Mismatched flattened buffer sizes for composite dtypes; off-by-one reshapes before conversion; representation ordering confusion (e.g. (2,) vs (1,2)).","solutions":["Reshape x so its trailing dims equal rep_aval.shape exactly","Verify the required suffix via jax.core.physical_element_aval(edtype).shape","Reorder/transpose if the representation expects components along a different axis","Fix upstream shape bugs (wrong axis length) before conversion"],"exampleFix":"// before\nout = lax.convert_element_type(x.reshape(2, 5), edtype)  # needs suffix (2,)\n\n// after\nout = lax.convert_element_type(x.reshape(5, 2), edtype)","handlingStrategy":"validation","validationCode":"from jax.core import physical_element_aval\nrep = physical_element_aval(edtype)\nn = x.ndim - rep.ndim\nassert n >= 0 and x.shape[n:] == rep.shape, (x.shape, rep.shape)","typeGuard":null,"tryCatchPattern":"try:\n    out = lax.convert_element_type(x, edtype)\nexcept ValueError:\n    out = lax.convert_element_type(x.reshape(*x.shape[:-rep.ndim], *rep.shape), edtype)","preventionTips":["Reshape explicitly so trailing dims equal the representation shape","Log expected vs actual suffix when validating incoming buffers","Keep representation-shape constants next to dtype definitions"],"tags":["jax","extended-dtype","shape-validation","suffix-mismatch"],"backgroundTag":"shape-suffix-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}