{"record":{"id":"5ef36b876b403af3","repo":"jax-ml/jax","slug":"can-only-convert-to-extended-dtype-from-its-repres","errorCode":null,"errorMessage":"can only convert to extended dtype from its representation dtype, but tried to convert from {dtype_to_string(x.dtype)} to {dtype_to_string(edtype)} which doesn't match the representation type {dtype_to_string(rep_aval.dtype)}.","messagePattern":"can only convert to extended dtype from its representation dtype, but tried to convert from (.+?) to (.+?) which doesn't match the representation type (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5509,"sourceCode":"  assert (isinstance(edtype, dtypes.ExtendedDType) and\n          not isinstance(x.dtype, dtypes.ExtendedDType))\n  # For backward compatibility, if the edtype rules have a `convert_to` method,\n  # use that rather than looking for an `allow_conversion: bool` attribute.\n  if not isinstance(x, ShapedArray):\n    raise TypeError(\"can only convert to an extended dtype on an array type,\"\n                    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 \"","sourceCodeStart":5491,"sourceCodeEnd":5527,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5491-L5527","documentation":"Beyond dtype kind checks, converting TO an extended dtype requires the source dtype to exactly equal the extended dtype's representation dtype (e.g. uint8 for some 8-bit extension types). This error fires when the conversion was allowed by the rules but the actual source dtype differs from the physical representation type.","triggerScenarios":"convert_element_type(x, edtype) where x.dtype != physical_element_aval(edtype).dtype, e.g. passing uint16 when the extended dtype is stored as uint8.","commonSituations":"Custom extended dtypes whose representation is not obvious; feeding buffers read with a different integer width; mixing jnp.uint8 and numpy uint8 vs int8 signedness mistakes.","solutions":["Match the source dtype exactly to the representation dtype (query it via jax.core.physical_element_aval(edtype).dtype)","Cast x to the representation dtype before the extended-dtype conversion","Verify signedness (uint8 vs int8) of the representation","For custom dtypes, document/expose the representation dtype in the rules"],"exampleFix":"// before\nx_ed = lax.convert_element_type(jnp.uint16_array, edtype)  # rep is uint8\n\n// after\nx_ed = lax.convert_element_type(jnp.uint16_array.astype(jnp.uint8), edtype)","handlingStrategy":"validation","validationCode":"from jax.core import physical_element_aval\nrep = physical_element_aval(edtype).dtype\nif x.dtype != rep:\n    x = x.astype(rep)","typeGuard":null,"tryCatchPattern":"try:\n    out = lax.convert_element_type(x, edtype)\nexcept ValueError:\n    out = lax.convert_element_type(x.astype(rep_dtype(edtype)), edtype)","preventionTips":["Query and cache physical_element_aval(edtype).dtype for each extended dtype you use","Standardize buffers to the exact representation dtype (incl. signedness)","Validate at API boundaries where raw buffers enter"],"tags":["jax","extended-dtype","dtype-conversion","representation"],"backgroundTag":"disallowed-dtype-conversion","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}