{"record":{"id":"9840c2271f5c6454","repo":"jax-ml/jax","slug":"x-argument-to-bincount-must-have-an-integer-type","errorCode":null,"errorMessage":"x argument to bincount must have an integer type; got {x.dtype}","messagePattern":"x argument to bincount must have an integer type; got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":2961,"sourceCode":"\n    Specifying a static ``length`` makes this jit-compatible:\n\n    >>> jit_bincount = jax.jit(jnp.bincount, static_argnames=['length'])\n    >>> jit_bincount(x, length=5)\n    Array([0, 2, 1, 3, 0], dtype=int32)\n\n    Any negative numbers are clipped to the first bin, and numbers beyond the\n    specified ``length`` are dropped:\n\n    >>> x = jnp.array([-1, -1, 1, 3, 10])\n    >>> jnp.bincount(x, length=5)\n    Array([2, 1, 0, 1, 0], dtype=int32)\n  \"\"\"\n  x = util.ensure_arraylike(\"bincount\", x)\n  if x.dtype == bool:\n    x = lax.convert_element_type(x, 'int32')\n  if not issubdtype(x.dtype, np.integer):\n    raise TypeError(f\"x argument to bincount must have an integer type; got {x.dtype}\")\n  if np.ndim(x) != 1:\n    raise ValueError(\"only 1-dimensional input supported.\")\n  minlength = core.concrete_or_error(\n      operator.index, minlength,\n      \"The error occurred because of argument 'minlength' of jnp.bincount.\")\n  if length is None:\n    x_arr = core.concrete_or_error(\n        asarray, x,\n        \"The error occurred because of argument 'x' of jnp.bincount. \"\n        \"To avoid this error, pass a static `length` argument.\")\n    length = max(minlength, x_arr.size and int(max(0, x_arr.max())) + 1)\n  else:\n    length = core.concrete_dim_or_error(\n        length,\n        \"The error occurred because of argument 'length' of jnp.bincount.\")\n\n  if weights is None:\n    weights = np.array(1, dtype=dtypes.int_)","sourceCodeStart":2943,"sourceCodeEnd":2979,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L2943-L2979","documentation":"jnp.bincount counts occurrences of integer values, so the input x must have an integer dtype. Float or complex inputs are rejected with a TypeError (bools are auto-cast to int32).","triggerScenarios":"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.","commonSituations":"Counting values after computing indices without casting; passing probabilities or normalized floats directly; feeding output of a float pipeline into a histogram-like count.","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"],"exampleFix":"// before\njnp.bincount(jnp.array([0.1, 1.7, 1.2, 0.0]))\n// after\njnp.bincount(jnp.floor(x).astype(jnp.int32))","handlingStrategy":"type-guard","validationCode":"if not jnp.issubdtype(x.dtype, jnp.integer) and x.dtype != jnp.bool_:\n    x = x.astype(jnp.int32)","typeGuard":"def is_integer_like(a) -> bool:\n    return jnp.issubdtype(a.dtype, jnp.integer) or a.dtype == jnp.bool_","tryCatchPattern":null,"preventionTips":["Cast indices to int32 right after argmax/squeeze ops","Assert dtype in pipeline tests"],"tags":["jax","bincount","dtype","typeerror"],"backgroundTag":"wrong-argument-dtype","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}