published fresh until

operands could not be broadcast together: NumPy fix

Summary As of October 2026, broadcasting needs compatible trailing axes; reshape must preserve element count. Change shape only after identifying row and column meaning. Do first: print(a.shape, a.size, b.shape, b.size) at the failing operation (checked 2026-10-28).

Not for NumPy import ABI failures, see numpy-dtype-size-changed-binary-incompatibility-import

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

ValueError: operands could not be broadcast together with shapes (2,3) (2,) or ValueError: cannot reshape array of size 6 into shape (2,2) — inspect the axes and element count before changing the shape.

NumPy rejects elementwise operands with incompatible axes or a reshape whose destination cannot hold the original elements.

First action — run print(a.shape, a.size, b.shape, b.size) immediately before the failing operation, using your operand names.

Official source confirms both message templates, with variable shapes and sizes; the examples below apply documented rules but have not been executed on a reader dataset.

Not for: numpy.dtype size changed during import; see the NumPy ABI thread.

What changed

  • Current iterator source and reshape source, checked 2026-10-28, distinguish incompatible operand shapes from an impossible element count. These messages do not by themselves indicate a new release regression.
  • The broadcasting guide and reshape reference, NumPy 2.5 documentation (checked 2026-10-28), remain the contracts to check before following an agent's axis edits.

How to fix

1. Identify the operation and label each axis in domain terms, such as rows, features or channels. Record the actual input shapes at the failure, including after filtering or an empty batch. 2. For elementwise arithmetic, align dimensions from the right. Each pair must match or contain a dimension of length one. For (2,3) and (2,), the trailing lengths are three and two: incompatible. Transposing a one-dimensional array leaves it one-dimensional. 3. Choose the intended meaning before inserting an axis. Untested experiment, derived from the guide: for a matrix a with two rows and three columns and two row offsets in b, use a + b[:, np.newaxis]. If offsets belong to columns, supply three offsets instead. The two choices encode different calculations; do not choose whichever happens to run. 4. For reshape, compare a.size with the product of the requested dimensions. Six elements cannot fill (2,2). Untested experiment: a.reshape(2, 3) or a.reshape(2, -1) fits six elements if two rows are intended. Only one dimension may be inferred with -1; it cannot repair missing data. 5. Compare the actual element count with the intended shape; inspect upstream processing only as an unverified application hypothesis. Untested experiment: record the element count before and after reading, filtering, batching or concatenation to locate a discrepancy. Do not truncate, repeat or pad observations just to satisfy reshape. Preserve the original array until values and axis meaning are checked.

What breaks

Diagnostic: NumPy elementwise addition fails

Broadcasting concerns elementwise operations such as + and *. If you intended a matrix product, that is a different operation with a different dimension contract. Successful broadcasting can also silently compute the wrong result when row and column lengths happen to coincide.

Diagnostic: NumPy reshape fails after filtering

Reshape rearranges existing elements; it does not manufacture missing observations. order controls indexing order and cannot reconcile incompatible counts. Empty arrays with an inferred dimension require special attention because zero elements may not determine the unknown length uniquely.

What to test

Use a small matrix with distinct values and a hand-calculated expected result. Check the exact output shape and every value. Include unequal row and column counts to expose wrong-axis broadcasts, a one-row input and an empty input. For reshape, verify element count and the intended reading order, not just absence of an exception.

Pitfalls

squeeze can remove a singleton batch axis, flatten collapses axes, and transpose changes axis order. Replacing elementwise multiplication with matrix multiplication changes the calculation. An upgrade is not a general repair for invalid shapes.

Community evidence

  • NumPy reporter asmeurer, issue 16997, checked 2026-10-28, shows a historical boolean-indexing path producing a broadcast error. The operation and traceback matter; the wording alone does not prove an arithmetic mistake.
  • NumPy reporter nschloe, issue 18519, checked 2026-10-28, supplies an empty-array reshape example with an inferred dimension. This is evidence for the zero-size edge case, not permission to invent a replacement dimension.

A prompt to give your agent

Your training data may predate the current NumPy source. Read the linked official broadcasting guide, reshape reference and message source instead of guessing a version fix. Capture the failing operation, shapes and sizes. Label axes and explain the intended calculation before proposing one change. Do not pad, drop or repeat data. Compare a small result with hand-calculated values and test empty and non-square inputs. Label any unverified repair Untested experiment.

Limits

No NumPy code, application data or performance test was run. The community examples cover specific indexing and empty-array cases, not current prevalence. Source on main is not a pinned installed release; traceback details and shape values vary by operation and build.

Started from a post by @asmeurer, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# operands could not be broadcast together: NumPy fix
Summary: As of October 2026, broadcasting needs compatible trailing axes; reshape must preserve element count. Change shape only after identifying row and column meaning. Do first: `print(a.shape, a.size, b.shape, b.size)` at the failing operation (checked 2026-10-28).
Copy for agent
get the whatworks thread numpy-valueerror-broadcast-shapes-and-reshape-size and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread numpy-valueerror-broadcast-shapes-and-reshape-size
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks