{"record":{"id":"cb53b611fee790a7","repo":"jax-ml/jax","slug":"invalid-scalar-value-x","errorCode":null,"errorMessage":"Invalid scalar value {x}","messagePattern":"Invalid scalar value (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/dtypes.py","lineNumber":436,"sourceCode":"\n@export\ndef scalar_type_of(x: Any) -> type:\n  \"\"\"Return the scalar type associated with a JAX value.\"\"\"\n  typ = dtype(x)\n  if typ in _custom_float_dtypes:\n    return float\n  elif typ in _intn_dtypes:\n    return int\n  elif np.issubdtype(typ, np.bool_):\n    return bool\n  elif np.issubdtype(typ, np.integer):\n    return int\n  elif np.issubdtype(typ, np.floating):\n    return float\n  elif np.issubdtype(typ, np.complexfloating):\n    return complex\n  else:\n    raise TypeError(f\"Invalid scalar value {x}\")\n\n\ndef scalar_type_to_dtype(typ: type, value: Any = None) -> DType:\n  \"\"\"Return the numpy dtype for the given scalar type.\n\n  Raises\n  ------\n  OverflowError: if `typ` is `int` and the value is too large for int64.\n\n  Examples\n  --------\n  >>> scalar_type_to_dtype(int)\n  dtype('int32')\n  >>> scalar_type_to_dtype(float)\n  dtype('float32')\n  >>> scalar_type_to_dtype(complex)\n  dtype('complex64')\n  >>> scalar_type_to_dtype(int)","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dtypes.py#L418-L454","documentation":"jax.dtypes.scalar_type_of maps a Python/numpy scalar to int/float/complex/bool; anything else (strings, None, arbitrary objects) is not a scalar and raises TypeError. It is used by lax._const when coercing Python values into typed constants.","triggerScenarios":"Passing a string, bytes, None, or custom object where a Python scalar is expected, e.g. lax ops building constants from user-supplied values (lax._const -> scalar_type_of).","commonSituations":"Trace-time constants from config (strings meant to be numbers); None defaults leaking into math; unhashable/non-scalar objects in place of scalars.","solutions":["Convert inputs to scalars before passing to JAX ops: int(v)/float(v)","Validate with isinstance(x, (bool, int, float, complex)) (plus numpy scalars) at your API boundary","Fix data pipeline producing strings/None for numeric fields"],"exampleFix":"# before\njnp.float32(config['scale'])  # scale is '2.0'\n\n# after\njnp.float32(float(config['scale']))","handlingStrategy":"type-guard","validationCode":"assert isinstance(x, (bool, int, float, complex)), f'not a scalar: {x!r}'","typeGuard":"def is_python_scalar(x) -> bool:\n    return isinstance(x, (bool, int, float, complex)) and not isinstance(x, bool) or isinstance(x, bool)","tryCatchPattern":"try:\n    fn(x)\nexcept TypeError:\n    fn(float(x))  # when a numeric string was intended","preventionTips":["Coerce config strings to numbers at load","Reject None before passing into math"],"tags":["jax","scalar","type-check"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}