{"record":{"id":"d88851fdea419d55","repo":"jax-ml/jax","slug":"tracerboolconversionerror","errorCode":null,"errorMessage":"TracerBoolConversionError","messagePattern":"TracerBoolConversionError","errorType":"exception","errorClass":"TracerBoolConversionError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":2038,"sourceCode":"def is_concrete(x):\n  return to_concrete_value(x) is not None\n\ndef to_concrete_value(x):\n  if isinstance(x, Tracer):\n    return x.to_concrete_value()\n  else:\n    return x\n\ndef concretization_function_error(fun, suggest_astype=False):\n  fname = getattr(fun, \"__name__\", fun)\n  fname_context = f\"The problem arose with the `{fname}` function. \"\n  if suggest_astype:\n    fname_context += (\"If trying to convert the data type of a value, \"\n                      f\"try using `x.astype({fun.__name__})` \"\n                      f\"or `jnp.array(x, {fun.__name__})` instead.\")\n  if fun is bool:\n    def error(self, arg):\n      raise TracerBoolConversionError(arg)\n  elif fun in (hex, oct, operator.index):\n    def error(self, arg):\n      raise TracerIntegerConversionError(arg)\n  else:\n    def error(self, arg):\n      raise ConcretizationTypeError(arg, fname_context)\n  return error\n\ndef concrete_or_error(force: Any, val: Any, context=\"\"):\n  \"\"\"Like force(val), but gives the context in the error message.\"\"\"\n  if force is None:\n    force = lambda x: x\n  if isinstance(val, Tracer):\n    maybe_concrete = val.to_concrete_value()\n    if maybe_concrete is None:\n      raise ConcretizationTypeError(val, context)\n    else:\n      return force(maybe_concrete)","sourceCodeStart":2020,"sourceCodeEnd":2056,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L2020-L2056","documentation":"TracerBoolConversionError is raised when Python bool() is applied to a Tracer, e.g. 'if tracer_value:' inside a jitted function. Branching on a traced value is impossible because its value is not known at trace time.","triggerScenarios":"Using a Tracer in an if statement, while condition, assert, any()/all() over tracers, or bare 'if x:' inside jax.jit/grad/vmap; also bool(x) or placing a tracer in a boolean context.","commonSituations":"Writing control flow that depends on data (early stopping, NaN checks like 'if jnp.isnan(x):') inside @jax.jit; using Python any/all on traced booleans.","solutions":["Use jax.lax.cond / lax.switch for data-dependent branching","Compute the condition outside the jit and pass it as a static value or use two jitted branches","For scalar debugging checks, print the value after the call, or use jax.debug.print","Guard assertions outside traced functions or use static_argnums"],"exampleFix":"# before\n@jax.jit\ndef f(x):\n    if x > 0:\n        return x\n    return -x\n\n# after\n@jax.jit\ndef f(x):\n    return jax.lax.cond(x > 0, x, lambda v: v, x, lambda v: -v)","handlingStrategy":"type-guard","validationCode":"import jax\ndef guard_branch(x):\n    assert not isinstance(x, jax.core.Tracer), 'branch on concrete value outside jit'","typeGuard":"def is_tracer(x) -> bool:\n    import jax; return isinstance(x, jax.core.Tracer)","tryCatchPattern":null,"preventionTips":["Prefer jax.lax.cond/switch for data-dependent control flow","Never write 'if traced_value:' inside jit; branch outside and pass a static flag","Use jax.debug.print for runtime inspection of traced values"],"tags":["jax","tracer","bool-conversion","control-flow","jit"],"backgroundTag":"jax-tracer-concretization","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}