{"record":{"id":"c1f5c941525ad356","repo":"jax-ml/jax","slug":"value-of-type-type-self-is-not-convertible-to-c","errorCode":null,"errorMessage":"Value of type {type(self)} is not convertible to complex.","messagePattern":"Value of type (.+?) is not convertible to complex\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":1129,"sourceCode":"    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)\n    if not hasattr(self.aval, \"_oct\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to oct.\")\n    return self.aval._oct(self)\n\n  def __index__(self):\n    if is_concrete(self): return operator.index(self.to_concrete_value())","sourceCodeStart":1111,"sourceCodeEnd":1147,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L1111-L1147","documentation":"Raised by JAXTracer.__complex__ when Python tries to convert a JAX Tracer to a complex scalar (complex(x)). Tracers are symbolic values created during tracing by jit/grad/vmap/etc. and have no concrete numeric value, so conversion to a Python complex number is disallowed. JAX throws this TypeError rather than producing an incorrect result.","triggerScenarios":"Calling complex(tracer) or numpy.complex128(tracer) inside a jitted/grad-traced function; passing a traced value to cmath functions or code that does implicit complex conversion; constructing complex literals like complex(0, x) from traced parts.","commonSituations":"Writing signal-processing or quantum simulation loss functions under jax.grad that build complex numbers from traced parameters; interop code that normalizes inputs with complex(); NumPy code reused inside jax.jit without adaptation.","solutions":["Keep the arithmetic in JAX: use jax.numpy.complex64/complex128 dtypes or construct via x + 1j*y instead of complex(x, y) on host scalars.","If a concrete complex value is genuinely needed, compute it outside the traced function and pass it in as a static/closed-over Python scalar.","Replace cmath calls with jax.lax.complex / jax.numpy equivalents.","For debugging, use jax.debug.print instead of host-side conversion."],"exampleFix":"# before\n@jax.jit\ndef f(re, im):\n    z = complex(re, im)   # TypeError: Tracer not convertible to complex\n    return jnp.abs(z)\n\n# after\n@jax.jit\ndef f(re, im):\n    z = re + 1j * im      # stays a JAX value\n    return jnp.abs(z)","handlingStrategy":"type-guard","validationCode":"import jax\ndef safe_complex(x, y):\n    if isinstance(x, jax.core.Tracer) or isinstance(y, jax.core.Tracer):\n        return x + 1j * y  # stay symbolic\n    return complex(x, y)","typeGuard":"import jax\ndef is_tracer(x) -> bool:\n    return isinstance(x, jax.core.Tracer)","tryCatchPattern":null,"preventionTips":["Build complex values with x + 1j*y or jax.lax.complex rather than complex() inside traces.","Give jnp.complex64/128 dtypes explicitly to avoid host-side coercion paths.","Keep cmath out of traced code; use jax.numpy equivalents."],"tags":["jax","tracer","complex-numbers","jit"],"backgroundTag":"jax-tracer-concretization","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}