{"record":{"id":"b18bbcda3f9c30da","repo":"jax-ml/jax","slug":"value-of-type-type-self-is-not-convertible-to-i-b18bbc","errorCode":null,"errorMessage":"Value of type {type(self)} is not convertible to integer index.","messagePattern":"Value of type (.+?) is not convertible to integer index\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":1150,"sourceCode":"  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())\n    check_integer_conversion(self)\n    if not hasattr(self.aval, \"_index\"):\n      raise TypeError(f\"Value of type {type(self)} is not convertible to integer index.\")\n    return self.aval._index(self)\n\n  # raises a useful error on attempts to pickle a Tracer.\n  def __reduce__(self):\n    raise ConcretizationTypeError(\n      self, (\"The error occurred in the __reduce__ method, which may \"\n             \"indicate an attempt to serialize/pickle a traced value.\"))\n\n  # raises the better error message from ShapedArray\n  def __setitem__(self, key, value):\n    if not hasattr(self.aval, \"_setitem\"):\n      raise TypeError(f\"Value of type {type(self)} is not indexable.\")\n    return self.aval._setitem(self, key, value)\n\n  # NumPy also only looks up special methods on classes.\n  def __array_module__(self, types):\n    if not hasattr(self.aval, \"_array_module\"):\n      raise TypeError(f\"Value of type {type(self)} is not compatible with the Array API.\")","sourceCodeStart":1132,"sourceCodeEnd":1168,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L1132-L1168","documentation":"Raised by JAXTracer.__index__ when a JAX Tracer is used where Python requires a concrete integer index (operator.index), e.g. as a list index, range argument, or tuple size. Traced values have no concrete integer value on the host, so they cannot serve as Python-level indices. JAX raises this TypeError because such use would break the trace being recorded.","triggerScenarios":"Indexing a Python list/tuple with a traced integer (lst[tracer]) inside jit/grad; range(tracer) or np.arange(tracer_count) style code; slicing host-side sequences with traced bounds; using a traced value as a shape dimension in host-side code.","commonSituations":"Data-dependent control flow like data = dataset[batch_size_tracer] inside jitted functions; computing loop counts under vmap/scan; mixing Python collections with traced scalars instead of jax.numpy arrays.","solutions":["Convert the Python list/tuple to a jax.numpy array first: jnp.asarray(lst)[tracer] uses JAX dynamic indexing and works under jit.","If the index is actually constant, mark it static with functools.partial(jit, static_argnums=...) so it arrives as a plain int.","For data-dependent loops, use jax.lax.fori_loop / scan / cond instead of Python for/if on traced values.","If you truly need the concrete value, restructure so the index is computed outside the traced function."],"exampleFix":"# before\n@jax.jit\ndef f(table, i):\n    return table[i]        # table is a Python list, i a Tracer\n\n# after\ntable_arr = jnp.asarray(table)\n@jax.jit\ndef f(i):\n    return table_arr[i]    # JAX array supports dynamic (traced) indices","handlingStrategy":"type-guard","validationCode":"import jax\nimport jax.numpy as jnp\ndef lookup(table, i):\n    if isinstance(i, jax.core.Tracer) or isinstance(table, (list, tuple)):\n        return jnp.asarray(table)[i]   # dynamic index path\n    return table[i]","typeGuard":"import jax\ndef needs_dynamic_index(seq, i) -> bool:\n    return isinstance(seq, (list, tuple)) and isinstance(i, jax.core.Tracer)","tryCatchPattern":null,"preventionTips":["Convert Python lists/tuples to jnp.asarray once, before jit, so traced indices work.","Mark loop counts/static indices with static_argnums so they arrive as ints.","Use lax.fori_loop/scan/cond for data-dependent control flow instead of Python range/if on traced values."],"tags":["jax","tracer","indexing","operator-index","control-flow"],"backgroundTag":"jax-tracer-concretization","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}