{"record":{"id":"56ad1b74344d46fe","repo":"jax-ml/jax","slug":"k-must-be-a-scalar-integer-got-k","errorCode":null,"errorMessage":"k must be a scalar integer; got {k}","messagePattern":"k must be a scalar integer; got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":5806,"sourceCode":"  # TODO(vfdev-5): optimize putting the array directly on the device specified\n  # instead of putting it on default device and then on the specific device\n  output = _eye(N, M=M, k=k, dtype=dtype)\n  if device is not None:\n    return api.device_put(output, device=device)\n  return output\n\n\ndef _eye(N: DimSize, M: DimSize | None = None,\n        k: int | ArrayLike = 0,\n        dtype: DTypeLike | None = None) -> Array:\n  dtype = dtypes.check_and_canonicalize_user_dtype(\n      float if dtype is None else dtype, \"eye\")\n  if isinstance(k, int):\n    k = lax._clip_int_to_valid_range(k, np.int32,\n                                              \"`argument `k` of jax.numpy.eye\")\n  offset = util.ensure_arraylike(\"eye\", k)\n  if not (offset.shape == () and dtypes.issubdtype(offset.dtype, np.integer)):\n    raise ValueError(f\"k must be a scalar integer; got {k}\")\n  N_int = core.canonicalize_dim(N, \"argument of 'N' jnp.eye()\")\n  M_int = N_int if M is None else core.canonicalize_dim(M, \"argument 'M' of jnp.eye()\")\n  if N_int < 0 or M_int < 0:\n    raise ValueError(f\"negative dimensions are not allowed, got {N} and {M}\")\n  i = lax.broadcasted_iota(offset.dtype, (N_int, M_int), 0)\n  j = lax.broadcasted_iota(offset.dtype, (N_int, M_int), 1)\n  return (i + offset == j).astype(dtype)\n\n\n@export\ndef identity(n: DimSize, dtype: DTypeLike | None = None) -> Array:\n  \"\"\"Create a square identity matrix\n\n  JAX implementation of :func:`numpy.identity`.\n\n  Args:\n    n: integer specifying the size of each array dimension.\n    dtype: optional dtype; defaults to floating point.","sourceCodeStart":5788,"sourceCodeEnd":5824,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L5788-L5824","documentation":"The k (diagonal offset) argument of jnp.eye must be a scalar with integer dtype after conversion to a JAX array. The check `offset.shape == () and issubdtype(offset.dtype, np.integer)` fails for non-scalar k (arrays with ndim > 0) or non-integer k (float, complex), raising this ValueError.","triggerScenarios":"jnp.eye(3, k=1.0) (float offset), jnp.eye(3, k=jnp.array([0])) (1-element array, not scalar), or k computed as float from division e.g. k=n/2.","commonSituations":"Computing the diagonal offset arithmetically (n//2 vs n/2 bug); passing a traced or batched value for k; passing k as a 0-d float array from external configuration.","solutions":["Use integer division: k = n // 2 not n / 2","Wrap k: jnp.eye(N, k=int(k)) to force a Python int scalar","Ensure k is a Python int or 0-d integer array before the call"],"exampleFix":"// before\neye = jnp.eye(n, k=n/2)   # float\n// after\neye = jnp.eye(n, k=n//2)  # int","handlingStrategy":"type-guard","validationCode":"k = int(k)  # forces scalar int; raises early if not convertible\neye = jnp.eye(N, k=k)","typeGuard":"def is_scalar_int(k) -> bool:\n    k = jnp.asarray(k)\n    return k.shape == () and jnp.issubdtype(k.dtype, jnp.integer)","tryCatchPattern":null,"preventionTips":["Use // instead of / for offsets","Run a lint/type pass on config values feeding k"],"tags":["jax","eye","dtype","scalar","valueerror"],"backgroundTag":"array-dtype-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}