{"record":{"id":"e8aedf1a2411e567","repo":"jax-ml/jax","slug":"cannot-convert-element-type-from-dtype-to-string","errorCode":null,"errorMessage":"Cannot convert_element_type from {dtype_to_string(x.dtype)} to {dtype_to_string(edtype)}","messagePattern":"Cannot convert_element_type from (.+?) to (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5503,"sourceCode":"  return trace.stage_value(args[0])\n\nstage_p.def_bind_with_trace(_stage_bind_with_trace)\n\n\ndef _to_edtype_abstract_eval(x, *, edtype):\n  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","sourceCodeStart":5485,"sourceCodeEnd":5521,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5485-L5521","documentation":"When converting an array to an extended dtype, the dtype's rules must allow the conversion (via a convert_to callback or an allow_conversion flag). If the source dtype is not an allowed input for that extended dtype (e.g. converting int32 directly to a float8 type that only accepts its representation dtype), ValueError is raised.","triggerScenarios":"jax.lax.convert_element_type(x, some_extended_dtype) where x.dtype is not accepted, e.g. int -> float8_e4m3fn without going through the representation dtype, or a custom extended dtype whose rules disallow the pair.","commonSituations":"Assuming astype(float8) works from any numeric type; custom extended dtypes with restrictive allow_conversion; version changes in ml_dtypes rules.","solutions":["Convert to the extended dtype's representation dtype first (e.g. jnp.float32), then to the extended dtype","Check edtype._rules.allow_conversion / convert_to to see which pairs are permitted","Use the documented supported source dtype for the target extended dtype","For custom dtypes, extend convert_to to accept the pair if semantically valid"],"exampleFix":"// before\nx_f8 = x.astype(dtypes.float8_e4m3fn)  # from int32, disallowed\n\n// after\nx_f8 = x.astype(jnp.float32).astype(dtypes.float8_e4m3fn)","handlingStrategy":"validation","validationCode":"def conversion_allowed(src_dtype, edtype):\n    fn = getattr(edtype._rules, 'convert_to', None)\n    return fn(src_dtype, edtype) if fn else edtype._rules.allow_conversion","typeGuard":null,"tryCatchPattern":"try:\n    x_ed = x.astype(edtype)\nexcept ValueError:\n    x_ed = x.astype(jnp.float32).astype(edtype)","preventionTips":["Route conversions through the documented intermediate dtype (usually float32)","Check dtype rules before astype in dtype-generic code","Wrap user-facing dtype arguments in an allowlist"],"tags":["jax","extended-dtype","dtype-conversion","validation"],"backgroundTag":"disallowed-dtype-conversion","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}