{"record":{"id":"c81d900fd3c394a0","repo":"jax-ml/jax","slug":"value-of-type-type-self-is-not-convertible-to-f","errorCode":null,"errorMessage":"Value of type {type(self)} is not convertible to float.","messagePattern":"Value of type (.+?) is not convertible to float\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":1123,"sourceCode":"\n  def __bool__(self):\n    if is_concrete(self): return bool(self.to_concrete_value())\n    check_bool_conversion(self)\n    if not hasattr(self.aval, \"_bool\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to boolean.\")\n    return self.aval._bool(self)\n\n  def __int__(self):\n    if is_concrete(self): return int(self.to_concrete_value())\n    check_scalar_conversion(self)\n    if not hasattr(self.aval, \"_int\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to integer.\")\n    return self.aval._int(self)\n\n  def __float__(self):\n    check_scalar_conversion(self)\n    if not hasattr(self.aval, \"_float\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to float.\")\n    return self.aval._float(self)\n\n  def __complex__(self):\n    check_scalar_conversion(self)\n    if not hasattr(self.aval, \"_complex\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to complex.\")\n    return self.aval._complex(self)\n\n  def __hex__(self):\n    if is_concrete(self): return hex(self.to_concrete_value())\n    check_integer_conversion(self)\n    if not hasattr(self.aval, \"_hex\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to hex.\")\n    return self.aval._hex(self)\n\n  def __oct__(self):\n    if is_concrete(self): return oct(self.to_concrete_value())\n    check_integer_conversion(self)","sourceCodeStart":1105,"sourceCodeEnd":1141,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L1105-L1141","documentation":"Raised by JAXTracer.__float__ when Python tries to convert a JAX Tracer to a plain float (e.g. via float(x)). Tracers are abstract placeholders for values being traced by transformations like jit, grad, vmap, or pmap, and have no concrete host-side value, so conversion is impossible outside of the trace. JAX raises this instead of returning a wrong or crash-prone value.","triggerScenarios":"Calling float(tracer), or passing a traced array to code that does implicit float conversion: math module functions, %-formatting ('%f' % x), JSON serialization, comparisons in Python if-statements (bool(x) on 0-d), or third-party code calling float() on the object — all while the value is inside jax.jit/grad/vmap/scan.","commonSituations":"Using print/logging or json.dumps on values inside @jax.jit; calling float() for epsilon constants inside grad-traced loss functions; mixing NumPy/Python scalar math with traced values; converting values created under enable_checkpointing or custom control-flow primitives (while_loop, scan, cond).","solutions":["Move the float(x) conversion outside the traced function: return the tracer from jit and convert the concrete result on the host.","If the value is a Python constant needed inside the trace, close over the plain Python float instead of converting the traced value.","For 0-d arrays, use jax.lax semantics inside the trace (e.g. x.astype(jax.numpy.float32)) and never Python scalar builtins.","If you need a concrete value mid-trace for debugging, use jax.debug.print instead of print/float()."],"exampleFix":"// before\n@jax.jit\ndef f(x):\n    eps = float(x) * 1e-3   # x is a Tracer\n    return x + eps\n\n# after\n@jax.jit\ndef f(x):\n    eps = x * 1e-3          # stay in JAX\n    return x + eps","handlingStrategy":"type-guard","validationCode":"def is_tracer(x) -> bool:\n    import jax\n    return isinstance(getattr(x, '__trace__', None), jax.core.Trace) or type(x).__name__.endswith('Tracer')","typeGuard":"from jax._src.core import JaxTracer if False else None\nimport jax\ndef is_tracer(x) -> bool:\n    return isinstance(x, jax.core.Tracer)  # public alias of JaxTracer","tryCatchPattern":null,"preventionTips":["Keep host-side scalar conversion (float/int) outside jit/grad/vmap boundaries; only convert returned concrete arrays.","Prefer jax.numpy scalar math over Python builtins inside traced functions.","Lint traced functions for float()/int()/complex() calls on arguments.","Use jax.debug.print for inspecting values during tracing instead of print/format."],"tags":["jax","tracer","type-conversion","jit","float"],"backgroundTag":"jax-tracer-concretization","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}