jax-ml/jax · error · TypeError

Value of type {type(self)} is not convertible to float.

Error message

Value of type {type(self)} is not convertible to float.

What it means

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.

Source

Thrown at jax/_src/core.py:1123

  def __bool__(self):
    if is_concrete(self): return bool(self.to_concrete_value())
    check_bool_conversion(self)
    if not hasattr(self.aval, "_bool"):
      raise TypeError(f"Value of type {type(self)} is not convertible to boolean.")
    return self.aval._bool(self)

  def __int__(self):
    if is_concrete(self): return int(self.to_concrete_value())
    check_scalar_conversion(self)
    if not hasattr(self.aval, "_int"):
      raise TypeError(f"Value of type {type(self)} is not convertible to integer.")
    return self.aval._int(self)

  def __float__(self):
    check_scalar_conversion(self)
    if not hasattr(self.aval, "_float"):
      raise TypeError(f"Value of type {type(self)} is not convertible to float.")
    return self.aval._float(self)

  def __complex__(self):
    check_scalar_conversion(self)
    if not hasattr(self.aval, "_complex"):
      raise TypeError(f"Value of type {type(self)} is not convertible to complex.")
    return self.aval._complex(self)

  def __hex__(self):
    if is_concrete(self): return hex(self.to_concrete_value())
    check_integer_conversion(self)
    if not hasattr(self.aval, "_hex"):
      raise TypeError(f"Value of type {type(self)} is not convertible to hex.")
    return self.aval._hex(self)

  def __oct__(self):
    if is_concrete(self): return oct(self.to_concrete_value())
    check_integer_conversion(self)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Move the float(x) conversion outside the traced function: return the tracer from jit and convert the concrete result on the host.
  2. If the value is a Python constant needed inside the trace, close over the plain Python float instead of converting the traced value.
  3. For 0-d arrays, use jax.lax semantics inside the trace (e.g. x.astype(jax.numpy.float32)) and never Python scalar builtins.
  4. If you need a concrete value mid-trace for debugging, use jax.debug.print instead of print/float().

Example fix

// before
@jax.jit
def f(x):
    eps = float(x) * 1e-3   # x is a Tracer
    return x + eps

# after
@jax.jit
def f(x):
    eps = x * 1e-3          # stay in JAX
    return x + eps
Defensive patterns

Strategy: type-guard

Validate before calling

def is_tracer(x) -> bool:
    import jax
    return isinstance(getattr(x, '__trace__', None), jax.core.Trace) or type(x).__name__.endswith('Tracer')

Type guard

from jax._src.core import JaxTracer if False else None
import jax
def is_tracer(x) -> bool:
    return isinstance(x, jax.core.Tracer)  # public alias of JaxTracer

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/c81d900fd3c394a0. Report an issue: GitHub.