{"record":{"id":"e4f76b5e5173d82a","repo":"jax-ml/jax","slug":"number-of-tensordot-axes-axes-exceeds-input-r","errorCode":null,"errorMessage":"Number of tensordot axes (axes {}) exceeds input ranks ({} and {})","messagePattern":"Number of tensordot axes \\(axes (.+?)\\) exceeds input ranks \\((.+?) and (.+?)\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/tensor_contractions.py","lineNumber":566,"sourceCode":"    >>> jnp.outer(x1, x2)\n    Array([[1, 2, 3],\n           [2, 4, 6]], dtype=int32)\n  \"\"\"\n  a, b = util.ensure_arraylike(\"tensordot\", a, b)\n  a_ndim = np.ndim(a)\n  b_ndim = np.ndim(b)\n\n  if preferred_element_type is None:\n    preferred_element_type, output_weak_type = dtypes.result_type(a, b, return_weak_type_flag=True)\n  else:\n    preferred_element_type = dtypes.check_and_canonicalize_user_dtype(\n        preferred_element_type, \"tensordot\")\n    output_weak_type = False\n\n  if type(axes) is int:\n    if axes > min(a_ndim, b_ndim):\n      msg = \"Number of tensordot axes (axes {}) exceeds input ranks ({} and {})\"\n      raise TypeError(msg.format(axes, a.shape, b.shape))\n    contracting_dims = tuple(range(a_ndim - axes, a_ndim)), tuple(range(axes))\n  elif isinstance(axes, (tuple, list)) and len(axes) == 2:\n    ax1, ax2 = axes\n    if isinstance(ax1, int) and isinstance(ax2, int):\n      contracting_dims = ((canonicalize_axis(ax1, a_ndim),),\n                          (canonicalize_axis(ax2, b_ndim),))\n    elif isinstance(ax1, (tuple, list)) and isinstance(ax2, (tuple, list)):\n      if len(ax1) != len(ax2):\n        msg = \"tensordot requires axes lists to have equal length, got {} and {}.\"\n        raise TypeError(msg.format(ax1, ax2))\n      contracting_dims = (tuple(canonicalize_axis(i, a_ndim) for i in ax1),\n                          tuple(canonicalize_axis(i, b_ndim) for i in ax2))\n    else:\n      msg = (\"tensordot requires both axes lists to be either ints, tuples or \"\n             \"lists, got {} and {}\")\n      raise TypeError(msg.format(ax1, ax2))\n  else:\n    msg = (\"tensordot axes argument must be an int, a pair of ints, or a pair \"","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/tensor_contractions.py#L548-L584","documentation":"jax.numpy.tensordot raises this TypeError when the integer axes argument is larger than the number of dimensions of either input array. The contraction rank cannot exceed min(a.ndim, b.ndim), so the operation is mathematically ill-formed and JAX rejects it before dispatching to lax.dot_general.","triggerScenarios":"Calling jnp.tensordot(a, b, axes=N) with N > min(a.ndim, b.ndim), e.g. jnp.tensordot(jnp.zeros((2,3)), jnp.zeros((3,4)), axes=3).","commonSituations":"Mismatch between the intended matrix/tensor ranks (e.g. passing a scalar or 1D vector where a 2D matrix was expected), or copying numpy code that assumed higher-rank batches.","solutions":["Check a.ndim and b.ndim before calling and pass axes <= min(a.ndim, b.ndim)","Fix the inputs so both arrays have at least the expected rank (e.g. add batch dims or reshape)","Pass explicit axis pairs instead of an integer count, e.g. axes=([1],[0])"],"exampleFix":"// before\njnp.tensordot(a, b, axes=3)  # a.ndim == 2\n// after\njnp.tensordot(a, b, axes=2)  # or axes=([1],[0])","handlingStrategy":"validation","validationCode":"assert isinstance(axes, int) and axes <= min(a.ndim, b.ndim), f\"axes={axes} exceeds ranks {a.ndim},{b.ndim}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log a.shape and b.shape before tensordot calls in data-driven pipelines","Centralize tensordot wrappers that validate ndim"],"tags":["jax","tensordot","shape-mismatch","numpy"],"backgroundTag":"invalid-axes-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}