{"record":{"id":"ef87fc15977952d7","repo":"jax-ml/jax","slug":"unsupported-scalar-attribute-type-type-val","errorCode":null,"errorMessage":"Unsupported scalar attribute type: {type(val)}","messagePattern":"Unsupported scalar attribute type: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/interpreters/mlir.py","lineNumber":421,"sourceCode":"# Attributes\n\nAttributeHandler = Callable[[Any], ir.Attribute]\n_attribute_handlers: dict[type[Any], AttributeHandler] = {}\n\ndef register_attribute_handler(type_: type[Any], handler_fun: AttributeHandler):\n  _attribute_handlers[type_] = handler_fun\n\ndef get_attribute_handler(type_: type[Any]) -> AttributeHandler:\n  return _attribute_handlers[type_]\n\ndef _numpy_scalar_attribute(val: Any) -> ir.Attribute:\n  mlir_type = dtype_to_ir_type(val.dtype)\n  if isinstance(mlir_type, ir.IntegerType):\n    return ir.IntegerAttr.get(mlir_type, int(val))\n  elif isinstance(mlir_type, ir.FloatType):\n    return ir.FloatAttr.get(mlir_type, val)\n  else:\n    raise TypeError(f\"Unsupported scalar attribute type: {type(val)}\")\n\ndef _numpy_array_attribute(x: np.ndarray | np.generic) -> ir.Attribute:\n  element_type = dtype_to_ir_type(x.dtype)\n  shape = x.shape\n  x = np.ascontiguousarray(x)\n  return ir.DenseElementsAttr.get(x, type=element_type, shape=shape)\n\ndef _numpy_array_attribute_handler(val: np.ndarray | np.generic) -> ir.Attribute:\n  if 0 in val.strides and val.size > 0:\n    raise ValueError(\n        \"NumPy arrays with zero strides are not supported as MLIR attributes\")\n  if val.dtype == dtypes.float0:\n    val = np.zeros(val.shape, dtype=np.bool_)\n  if dtypes.is_weakly_typed_scalar(val) or np.isscalar(val):\n    return _numpy_scalar_attribute(val)\n  else:\n    return _numpy_array_attribute(val)\n","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/interpreters/mlir.py#L403-L439","documentation":"When converting a numpy scalar into an MLIR attribute, JAX maps the dtype to an MLIR IntegerType or FloatType and builds the corresponding attribute. If dtype_to_ir_type returns something else (complex handled elsewhere, or an exotic/unmapped dtype), the scalar cannot be represented and a TypeError is raised.","triggerScenarios":"Lowering a constant whose numpy scalar dtype maps to a non-integer/non-float MLIR type — typically a dtype that JAX does not support (e.g. float128 on platforms where it is not mapped, datetime64, timedelta64, or object/string scalars reaching this path).","commonSituations":"Environment-dependent dtype mismatches: np.longdouble on ARM/Windows, datasets containing np.datetime64 columns, older JAX versions lacking mappings for newer dtypes, or bfloat16 conversions done via unsupported numpy paths.","solutions":["Cast the value to a supported dtype before passing it: arr.astype(np.float32) / np.int32 / jnp.bfloat16 via JAX","Upgrade JAX so the dtype mapping table covers your type","Inspect val.dtype and convert exotic dtypes (datetime/object/string) to numeric or string metadata outside the traced function"],"exampleFix":"# before\njitted_fn(np.datetime64('2024-01-01'))\n\n# after\njitted_fn(np.int64(19723))  # convert dates to numeric outside JAX","handlingStrategy":"validation","validationCode":"SUPPORTED = {np.dtype(t) for t in (np.bool_, np.int8, np.int16, np.int32, np.int64,\n                                       np.uint8, np.uint16, np.uint32, np.uint64,\n                                       np.float16, np.float32, np.float64)}\n\ndef cast_to_supported(arr):\n    if arr.dtype not in SUPPORTED:\n        return arr.astype(np.float32)\n    return arr","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize dtypes at data loading boundaries (no datetime64/object/float128)","Keep JAX and jaxlib versions matched so dtype mappings are complete"],"tags":["jax","dtype","mlir","numpy"],"backgroundTag":"unsupported-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}