{"record":{"id":"236ac92bb0a8fd01","repo":"jax-ml/jax","slug":"tensordot-requires-axes-lists-to-have-equal-length","errorCode":null,"errorMessage":"tensordot requires axes lists to have equal length, got {} and {}.","messagePattern":"tensordot requires axes lists to have equal length, got (.+?) and (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/tensor_contractions.py","lineNumber":576,"sourceCode":"  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 \"\n           \"of lists/tuples of ints.\")\n    raise TypeError(msg)\n  result = lax.dot_general(\n      a, b, (contracting_dims, ((), ())), precision=precision,\n      preferred_element_type=preferred_element_type,\n      out_sharding=out_sharding)\n  return lax._convert_element_type(result, preferred_element_type, output_weak_type)\n\n\n","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/tensor_contractions.py#L558-L594","documentation":"When tensordot's axes argument is a pair of sequences, both sequences must have the same length because each entry of ax1 contracts with the corresponding entry of ax2. JAX raises this TypeError when len(ax1) != len(ax2), since the contraction is undefined.","triggerScenarios":"jnp.tensordot(a, b, axes=([0,1],[2])) where the two axis lists differ in length.","commonSituations":"Typos or off-by-one in hand-written axis lists; refactoring code where an axis was added to one side only.","solutions":["Make both axis lists the same length","Verify each axis index is within range of the respective array's ndim"],"exampleFix":"// before\njnp.tensordot(a, b, axes=([0,1],[2]))\n// after\njnp.tensordot(a, b, axes=([0,1],[1,2]))","handlingStrategy":"validation","validationCode":"if isinstance(axes, tuple) and len(axes) == 2 and isinstance(axes[0], (list, tuple)):\n    assert len(axes[0]) == len(axes[1]), 'axis lists must match'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Construct axis pairs programmatically from a single list of paired indices"],"tags":["jax","tensordot","axes-mismatch","numpy"],"backgroundTag":"invalid-axes-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}