jax-ml/jax · error · OverflowError

Python int {value} too large to convert to {dtype}

Error message

Python int {value} too large to convert to {dtype}

What it means

scalar_type_to_dtype validates that a Python int fits the (canonicalized) target dtype — under 32-bit mode int becomes int32, so ints outside [-2^31, 2^31) raise OverflowError before silent wraparound.

Source

Thrown at jax/_src/dtypes.py:466

  >>> scalar_type_to_dtype(int)
  dtype('int32')
  >>> scalar_type_to_dtype(float)
  dtype('float32')
  >>> scalar_type_to_dtype(complex)
  dtype('complex64')
  >>> scalar_type_to_dtype(int)
  dtype('int32')
  >>> scalar_type_to_dtype(int, 0)
  dtype('int32')
  >>> scalar_type_to_dtype(int, 1 << 63)  # doctest: +IGNORE_EXCEPTION_DETAIL
  Traceback (most recent call last):
  OverflowError: Python int 9223372036854775808 too large to convert to int32
  """
  dtype = canonicalize_dtype(python_scalar_types_to_dtypes[typ])
  if typ is int and value is not None:
    iinfo = np.iinfo(dtype)
    if value < iinfo.min or value > iinfo.max:
      raise OverflowError(f"Python int {value} too large to convert to {dtype}")
  return dtype


def coerce_to_array(x: Any, dtype: DTypeLike | None = None) -> np.ndarray:
  """Coerces a scalar or NumPy array to an np.array.

  Handles Python scalar type promotion according to JAX's rules, not NumPy's
  rules.
  """
  if dtype is None and type(x) in python_scalar_types:
    dtype = scalar_type_to_dtype(type(x), x)
  return np.asarray(x, dtype)

iinfo = ml_dtypes.iinfo
finfo = ml_dtypes.finfo

def _issubclass(a: Any, b: Any) -> bool:
  """Determines if ``a`` is a subclass of ``b``.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Enable 64-bit mode at process start: `import jax; jax.config.update('jax_enable_x64', True)` (must be set before arrays are created)
  2. Cast explicitly: jnp.array(value, dtype=jnp.int64) after enabling x64, or use jnp.int64-aware code
  3. Keep values within int32 range or store as float64/uint32 as appropriate

Example fix

# before
jnp.array(2**40)  # OverflowError

# after
import jax
jax.config.update('jax_enable_x64', True)
jnp.array(2**40)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np, jax
info = np.iinfo(jnp.int32 if not jax.config.x64_enabled else jnp.int64)
assert info.min <= value <= info.max, f'{value} out of range'

Try / catch

try:
    coerce_to_array(value)
except OverflowError:
    jax.config.update('jax_enable_x64', True)  # only if before array creation
    coerce_to_array(value)

Prevention

When it happens

Trigger: Calling coerce_to_array / scalar_type_to_dtype with a large Python int (e.g. 2**40, hashing offsets, dataset ids) while x64 is disabled (default), so int32 is the target.

Common situations: Large IDs/indices from datasets; hashes or timestamps as ints; code written assuming 64-bit ints without jax_enable_x64.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/6b2957dc55cc5a53. Report an issue: GitHub.