{"record":{"id":"a17a5b06027d5268","repo":"jax-ml/jax","slug":"broadcast-in-dim-target-broadcast-shape-must-have","errorCode":null,"errorMessage":"broadcast_in_dim target broadcast shape must have equal or higher rank to the operand shape; got operand ndim {} and target broadcast ndim {}.","messagePattern":"broadcast_in_dim target broadcast shape must have equal or higher rank to the operand shape; got operand ndim (.+?) and target broadcast ndim (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":6921,"sourceCode":"mlir.register_lowering(\n    ragged_dot_general_p, partial(_ragged_dot_general_lower, platform='tpu'),\n    platform='tpu')\n\n\ndef _broadcast_in_dim_shape_rule(operand, *, shape, broadcast_dimensions,\n                                 sharding):\n  _check_shapelike('broadcast_in_dim', 'shape', shape)\n  _check_shapelike('broadcast_in_dim', 'broadcast_dimensions',\n                   broadcast_dimensions)\n  operand_ndim = np.ndim(operand)\n  if operand_ndim != len(broadcast_dimensions):\n    msg = ('broadcast_in_dim broadcast_dimensions must have length equal to '\n           'operand ndim; got broadcast_dimensions {} for operand ndim {}.')\n    raise TypeError(msg.format(broadcast_dimensions, operand_ndim))\n  if len(shape) < operand_ndim:\n    msg = ('broadcast_in_dim target broadcast shape must have equal or higher rank '\n           'to the operand shape; got operand ndim {} and target broadcast ndim {}.')\n    raise TypeError(msg.format(operand_ndim, len(shape)))\n  if not set(broadcast_dimensions).issubset(set(range(len(shape)))):\n    msg = ('broadcast_in_dim broadcast_dimensions must be a subset of output '\n           'dimensions, got {} for operand ndim {} and shape {}.')\n    raise TypeError(msg.format(broadcast_dimensions, operand_ndim, shape))\n  if not all(core.definitely_equal_one_of_dim(operand.shape[i],\n                                              [1, shape[broadcast_dimensions[i]]])\n             for i in range(operand_ndim)):\n    msg = (\n        \"broadcast_in_dim operand dimension sizes must either be 1, or be \"\n        \"equal to their corresponding dimensions in the target broadcast \"\n        \"shape; got operand of shape {}, target broadcast shape {}, \"\n        \"broadcast_dimensions {} \")\n    raise TypeError(msg.format(\n        tuple(core.replace_tracer_for_error_message(d) for d in operand.shape),\n        shape, broadcast_dimensions))\n  if len(broadcast_dimensions) != len(set(broadcast_dimensions)):\n    msg = (\"broadcast_in_dim broadcast_dimensions must not contain duplicates, \"\n           \"got broadcast_dimensions {}\")","sourceCodeStart":6903,"sourceCodeEnd":6939,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L6903-L6939","documentation":"broadcast_in_dim can only add leading/trailing-style new dimensions, so the target shape must have rank >= operand rank. A lower-rank target is impossible to map and raises this TypeError.","triggerScenarios":"Calling broadcast_in_dim(operand, shape, ...) where len(shape) < np.ndim(operand).","commonSituations":"Passing an already-higher-rank operand with a scalar/low-rank shape (e.g. targeting shape=() with a vector); confusion between broadcasting up vs. reshaping down.","solutions":["Ensure the target shape rank is >= operand rank; broadcasting never removes dimensions","Use lax.reshape or slicing to reduce rank before broadcasting","Double-check the intended output shape arithmetic in your code"],"exampleFix":"// before\nx = jnp.zeros((2, 3))\ny = lax.broadcast_in_dim(x, (2,), (0, 1))  # target rank 1 < operand rank 2\n// after\ny = lax.broadcast_in_dim(x, (2, 3, 4), (0, 1))  # rank 3 >= 2","handlingStrategy":"validation","validationCode":"assert len(shape) >= np.ndim(operand), 'broadcast can only add dims'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember broadcasting never reduces rank"],"tags":["jax","broadcast-in-dim","rank-validation"],"backgroundTag":"invalid-broadcast-dimensions","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}