jnp.compress and jnp.extract raise TracerBoolConversionError under jax.jit when size is not specified
Description
jnp.compress and jnp.extract crash with TracerBoolConversionError when called inside jax.jit without a size argument. The error originates inside JAX's own implementation of jnp.compress at lax_numpy.py:9001, where reductions.any(extra) is called on a traced array — this is JAX's internal code violating JIT tracing rules, not user code.
Unlike other data-dependent-size APIs (jnp.nonzero, jnp.unique) which raise a clear ConcretizationTypeError with a helpful message pointing to size=, jnp.compress raises a TracerBoolConversionError with no guidance, making the error harder to diagnose.
Note: jnp.compress does support size= and works correctly under JIT when size is provided. The bug is specifically in the size=None code path.
Reproducer 1 — jnp.compress
import jax
import jax.numpy as jnp
t = jnp.ones((4, 4), dtype=jnp.float32)
def f(x):
return jnp.compress(x.ravel() > 0, x.ravel())
print(f(t).shape) # eager: (16,)
jax.jit(f)(t) # crashes
Reproducer 2 — jnp.extract
import jax
import jax.numpy as jnp
t = jnp.ones((4, 4), dtype=jnp.float32)
def f(x):
return jnp.extract(x > 0, x)
print(f(t).shape) # eager: (16,)
jax.jit(f)(t) # crashes
Error (both reproducers)
jax.errors.TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[].
The error occurred while tracing the function f at <string>:6 for jit. This concrete value was not available in Python because it depends on the value of the argument x.
Root Cause
In lax_numpy.py, the size=None branch of jnp.compress does:
if size is None:
if reductions.any(extra): # ← BUG: any() on a traced array
raise ValueError("condition contains entries that are out of bounds")
result = arr[condition_arr]
reductions.any(extra) is called on a traced boolean array, which is illegal under JIT. The size is not None branch correctly handles JIT using where and argsort. The fix would be to either raise a clear ConcretizationTypeError with a message pointing to size=, or to make the size=None branch JIT-compatible.
jnp.extract is affected because it calls jnp.compress internally without size=.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.10.1
jaxlib: 0.10.1
numpy: 2.4.6
python: 3.12.13 | packaged by Anaconda, Inc. | (main, Mar 19 2026, 20:20:58) [GCC 14.3.0]
device info: cpu-1, 1 local devices
process_count: 1
platform: uname_result(system='Linux', node='unicorn-login-01', release='6.8.0-110-generic', version='#110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu
Thanks for the report – this is working as expected. If you take a look at the documentation for compress and extract, you'll see the following notes:
compresssize (int | None) – optional static size for output. Must be specified in order forcompressto be compatible with JAX transformations like jit() or vmap().
extractsize (int | None) – optional static size for output. Must be specified in order forextractto be compatible with JAX transformations like jit() or vmap().
This is not something that can be changed: the semantics of numpy.extract and numpy.compress require data-dependent shapes, which is why JAX adds the size argument.
Do you have a suggestion for how we could improve the documentation here?