jax-ml/jax · error · TypeError
x argument to bincount must have an integer type; got
Error message
x argument to bincount must have an integer type; got {x.dtype} What it means
jnp.bincount counts occurrences of values, which requires the values to be integer bin numbers. The function fires when x's dtype (after bool promotion) is not an integer subtype, e.g. passing floats.
Solutions
- Cast to integer: jnp.bincount(x.astype(jnp.int32))
- If values are class probabilities, take argmax first: jnp.bincount(jnp.argmax(x, axis=-1))
- Note bool input is handled automatically; no cast needed there
Example fix
// before jnp.bincount(jnp.array([0.1, 1.7, 1.2, 0.0])) // after jnp.bincount(jnp.floor(x).astype(jnp.int32))
Defensive patterns
Strategy: type-guard
Validate before calling
if not jnp.issubdtype(x.dtype, jnp.integer) and x.dtype != jnp.bool_:
x = x.astype(jnp.int32) Type guard
def is_integer_like(a) -> bool:
return jnp.issubdtype(a.dtype, jnp.integer) or a.dtype == jnp.bool_ Prevention
- Cast indices to int32 right after argmax/squeeze ops
- Assert dtype in pipeline tests
When it happens
Trigger: Calling jnp.bincount(jnp.array([0.5, 1.0, 1.5])) or bincount on float32/float64 logits, indices from argmax on float arrays, or complex arrays.
Common situations: Counting values after computing indices without casting; passing probabilities or normalized floats directly; feeding output of a float pipeline into a histogram-like count.
Related errors
- x.dtype= is not supported, see docstring for supported…
- a and b must have the same element type, but got
- `a` array must be integer typed
- A.dtype= is not supported.
- A, X must have same dtypes
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/9840c2271f5c6454.
Report an issue: GitHub.
Appendix: source
Thrown at jax/_src/numpy/lax_numpy.py:2961
Specifying a static ``length`` makes this jit-compatible:
>>> jit_bincount = jax.jit(jnp.bincount, static_argnames=['length'])
>>> jit_bincount(x, length=5)
Array([0, 2, 1, 3, 0], dtype=int32)
Any negative numbers are clipped to the first bin, and numbers beyond the
specified ``length`` are dropped:
>>> x = jnp.array([-1, -1, 1, 3, 10])
>>> jnp.bincount(x, length=5)
Array([2, 1, 0, 1, 0], dtype=int32)
"""
x = util.ensure_arraylike("bincount", x)
if x.dtype == bool:
x = lax.convert_element_type(x, 'int32')
if not issubdtype(x.dtype, np.integer):
raise TypeError(f"x argument to bincount must have an integer type; got {x.dtype}")
if np.ndim(x) != 1:
raise ValueError("only 1-dimensional input supported.")
minlength = core.concrete_or_error(
operator.index, minlength,
"The error occurred because of argument 'minlength' of jnp.bincount.")
if length is None:
x_arr = core.concrete_or_error(
asarray, x,
"The error occurred because of argument 'x' of jnp.bincount. "
"To avoid this error, pass a static `length` argument.")
length = max(minlength, x_arr.size and int(max(0, x_arr.max())) + 1)
else:
length = core.concrete_dim_or_error(
length,
"The error occurred because of argument 'length' of jnp.bincount.")
if weights is None:
weights = np.array(1, dtype=dtypes.int_)View on GitHub (pinned to 1e1c6a8fc0)