{"record":{"id":"fdc7eb25c7c8e201","repo":"jax-ml/jax","slug":"cannot-convert-element-type-from-dtype-to-string-fdc7eb","errorCode":null,"errorMessage":"Cannot convert_element_type from {dtype_to_string(x.dtype)} to {dtype_to_string(dtype)}","messagePattern":"Cannot convert_element_type from (.+?) to (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5566,"sourceCode":"          convert_element_type(t, core.primal_dtype_to_tangent_dtype(edtype)))\nad.primitive_transposes[to_edtype_p] = \\\n    lambda ct, x, edtype: [from_edtype_p.bind(ct, dtype=x.aval.dtype)]\nbatching.defvectorized(to_edtype_p)\nmlir.register_lowering(to_edtype_p, lambda _, x, **__: [x])\n\n\ndef _from_edtype_abstract_eval(x, *, dtype):\n  assert (isinstance(x.dtype, dtypes.ExtendedDType) and\n          not isinstance(dtype, dtypes.ExtendedDType))\n  if not isinstance(x, ShapedArray):\n    raise TypeError(\"can only convert from an extended dtype on an array type,\"\n                    f\"but got {type(x)}\")\n  if convert_from := getattr(x.dtype._rules, 'convert_from', None):\n    allow_conversion = convert_from(x.dtype, dtype)\n  else:\n    allow_conversion = x.dtype._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(dtype)}\")\n  rep_aval = core.physical_element_aval(x.dtype)\n  assert tuple(rep_aval.sharding.spec) == (None,) * rep_aval.ndim\n  if rep_aval.dtype != dtype:\n    raise ValueError(\n        \"can only convert from extended dtype to its representation dtype, \"\n        f\"but tried to convert from {dtype_to_string(x.dtype)} to \"\n        f\"{dtype_to_string(dtype)} which doesn't match the representation type \"\n        f\"{dtype_to_string(rep_aval.dtype)}.\")\n  if isinstance(x, ShapedArray):\n    return x.update(shape=(*x.shape, *rep_aval.shape), dtype=dtype)\n  else:\n    assert False  # unreachable, see isinstance check above\n\nfrom_edtype_p = Primitive('from_edtype')\nfrom_edtype_p.def_impl(partial(dispatch.apply_primitive, from_edtype_p))\nfrom_edtype_p.def_abstract_eval(_from_edtype_abstract_eval)","sourceCodeStart":5548,"sourceCodeEnd":5584,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5548-L5584","documentation":"Converting FROM an extended dtype to a regular dtype must be permitted by the dtype's rules (convert_from callback or allow_conversion flag). If the target dtype is not an allowed destination (e.g. converting float8 directly to int32 when rules disallow it), ValueError is raised.","triggerScenarios":"jax.lax.convert_element_type(x_edtype, dtype) where the pair (x.dtype, dtype) is rejected by the dtype rules, e.g. float8 -> int, or a custom extended dtype with restrictive convert_from.","commonSituations":"Trying to view/convert float8 payloads as integers directly; custom dtype ecosystems with explicit conversion whitelists; assuming numpy-style any-to-any casts.","solutions":["Convert to the representation dtype first (e.g. -> uint8), then cast to the desired type","For float8, go through float32 for numeric semantics","Inspect dtype._rules.convert_from to learn allowed targets","Extend the rules for custom extended dtypes where the conversion is meaningful"],"exampleFix":"// before\ny = x_f8.astype(jnp.int32)  # disallowed pair\n\n// after\ny = x_f8.astype(jnp.float32).astype(jnp.int32)","handlingStrategy":"validation","validationCode":"def from_conversion_allowed(edtype, target):\n    fn = getattr(edtype._rules, 'convert_from', None)\n    return fn(edtype, target) if fn else edtype._rules.allow_conversion","typeGuard":null,"tryCatchPattern":"try:\n    y = x_ed.astype(dt)\nexcept ValueError:\n    y = x_ed.astype(rep_dtype(x_ed.dtype)).astype(dt)","preventionTips":["Convert to representation dtype first, then cast","Whitelist target dtypes at API boundaries","Read the dtype rules before adding new cast targets"],"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"}