NumPy 2.4+: einsum with optimize=True returns wrong results for bool and small-integer arrays
np.einsum('ij,k->k', mask, w) # [3. 1.5]
np.einsum('ij,k->k', mask, w, optimize=True) # [0.5 0.25]
Short answer: Since NumPy 2.4.0, np.einsum with optimize=True can return wrong numbers, with no error, when operands have different dtypes and one is bool or a small integer type. Drop the optimize argument, or cast every operand to the common result dtype first. Passing dtype= does not fix it.
No time to dig in? I find the affected calls, fix them and send you a pull request within 48 hours: $49, card, no account. Still failing, full refund. The free steps are below.
Checked on 16 November 2026 against the sources below.
Why it happens
With optimize on, einsum splits the work into steps. Since 2.4.0 (20 December 2025), an index that appears in only one operand and not in the output is summed away first, in that operand's own dtype, before the operands are combined. Summing a bool array in bool is a logical OR, so six True values become True (1) instead of 6. Summing uint8 or int8 values wraps around at 255 or 127. The default einsum (no optimize) does all its arithmetic in the common result dtype, so it does not have this problem. NumPy issue #33324 (13 November 2026) reports it, a NumPy contributor confirmed it as a valid bug the same day, and the first fix pull request was closed without being merged, so no release has a fix yet. We reproduced it on 14 November 2026 in clean virtual environments (Python 3.11, Windows): NumPy 2.3.5 gave the right answer for the two examples above, 2.4.4 and 2.4.6 gave the wrong one, with optimize=True, 'greedy', 'optimal' and an explicit einsum_path. We then ran 600 random contractions over bool, int8, uint8, int16, int32, int64, float32 and float64 on each version. On 2.4.4 and 2.4.6, 156 of 600 differed from the default einsum. On 2.3.5, 30 of 600 still differed, all with three operands, so going back to 2.3 fixes two-operand calls only. The issue says 2.5.0 and 2.5.4 are affected too. On 16 November 2026 we tested NumPy 2.5.5 (released 15 November) on Python 3.13: both examples above still give the wrong result, with optimize=True, 'greedy' and 'optimal'; in a new set of 600 random contractions, 173 differed from the default einsum and the cast-first helper below matched it in all 600. The 2.5.5 release notes do not mention einsum.
You may also see
np.einsum('i,j->j', a, b) # [ 0 400 800]
np.einsum('i,j->j', a, b, optimize=True) # [ 0 144 288]einsum result changes with optimize=True, optimize='greedy' or optimize='optimal'
Fix
- Check if you are exposed: search your code for
einsum(calls that passoptimize=(True,'greedy','optimal'or a path fromnp.einsum_path). The default isoptimize=False, so calls without the argument are not affected. Then look at the operands: the risk is a boolean mask, or an int8, uint8, int16 or int32 array, combined with an array of a wider dtype. - Confirm on your data: run the call once with and once without
optimizeand compare withnp.allclose. If they differ, the result withoutoptimizeis the correct one. - Simplest fix: remove the
optimizeargument. For two operands, or small arrays, the speed difference is usually small. - Need the speed of
optimize? Cast every operand to the common result dtype before the call. This helper gives the same answer as the default einsum on NumPy 2.3.5, 2.4.4 and 2.4.6 in all 600 of our random cases:
For one call,import numpy as np def einsum_cast(subscripts, *operands, **kwargs): """np.einsum with every operand cast to the common result dtype first.""" dt = np.result_type(*operands) return np.einsum(subscripts, *[np.asarray(o, dtype=dt) for o in operands], **kwargs) einsum_cast('ij,k->k', mask, w, optimize=True) # [3. 1.5]mask.astype(float)on the bool or small-integer operand does the same job. The cast copies that operand once, so memory use goes up by its size in the wider dtype. - Do not rely on
dtype=. In our test,np.einsum('i,j->j', a, b, optimize=True, dtype=np.int64)still returned the wrapped values[0 144 288]on 2.4.6. - Pinning
numpy<2.4is a partial fix only: it restores two-operand calls, but contractions with three or more operands of mixed small dtypes already differed on 2.3.5 in our test. Prefer the cast. - Add a regression test for each fixed call that compares the optimized result with the default one on a boolean or uint8 input, so a later NumPy upgrade can't bring the problem back silently.
Get it fixed: $49
Pay by card, no account needed. Pull request within 48 hours; build still failing, full refund.
How it works: 1. Pay on Gumroad; the checkout asks for the repository. 2. Invite my GitHub user, Donrulezz, to that repository only (private is fine), or email a zip with the failing log to hello@deadlinefix.com. 3. You get a pull request with a list of every change; you review, merge and deploy. I never ask for cloud passwords or production keys.
Who does the work: me, Ashish, the owner of this site, based in India. My public code and tools are on GitHub: github.com/Donrulezz.
Watch out
- No exception and no warning is raised. Results that came from an affected call on NumPy 2.4 or later may need to be recomputed.
- Not affected in our tests: operands that all share one dtype (bool with bool, uint8 with uint8), calls where every index is shared between operands or kept in the output (for example
'ij,jk->ik'with two operands), and small-integer values whose sums stay inside the type's range. - float32 mixed with float64 is summed in float32 first. The result is not wrong, but it is less precise: we measured a relative difference of about 5e-06 on a million-element sum.
- Libraries that call
np.einsum(..., optimize=True)for you pass the bug on. If a result changed after a NumPy upgrade, check what the library passes to einsum. - When NumPy merges a fix and names the release, we will add the version here. Until then, every release from 2.4.0 on that we tested (2.4.4, 2.4.6) has the bug.
Sources
- NumPy issue #33324: einsum with optimize=True gives wrong results for bool and small-integer operands, regression in 2.4 (13 November 2026)
- NumPy pull request #33329: first fix attempt, closed without merge
- NumPy pull request #23513: the optimized einsum rewrite that the issue's bisect points to
- NumPy documentation: numpy.einsum, the optimize parameter
- numpy on PyPI: release history (2.4.0 uploaded 20 December 2025)
Other errors breaking soon
- GitHub Actions: "Event 'pull_request_target' is not allowed to trigger Actions workflows"
- Unable to display RD Web Access: Chrome 158 and Edge 159 turn off XSLT
- OpenAI Agent Builder shuts down on 30 November 2026: export your workflows, keep ChatKit, move prompt objects
- Claude Sonnet 4.5 retires on 30 November 2026: the 400 errors on Sonnet 5.5 and the fix
- Exchange Online POP and IMAP: TLS 1.0 and 1.1 refused, imap-legacy.office365.com gone
- Windows KB5127648: 802.1X Wi-Fi, wired and VPN sign-in fails (NTLMv1 SSO blocked)
- Firebase: Runtime Node.js 20 was decommissioned
- Firebase Hosting deploy fails: 403 "Permission denied to get service [firebasehosting.googleapis.com]"
- GitHub webhooks: "Failed to connect to host" after GitHub added 3 webhook IP ranges (29 October 2026)
- AWS CodeBuild: Unknown runtime version named '20' of nodejs
- Google Cloud Python: non-supported Python version (3.10)
- Azure OpenAI o1, o3, o3-mini and o4-mini retired on 19 November 2026: 410 Gone
- OpenAI: "The model … has been deprecated" or "Model not found" after a shutdown
- Nextcloud desktop 35.0.1 won't start: "UNIQUE constraint failed: metadata.inode"
- Contact Form 7 6.2: "Composer detected issues in your platform" (PHP 8.3 required)
- Biome 2.6: biome check fails on README.md and YAML files after the upgrade
- pytest 9.2: xdist_group tests run on different workers (--dist loadgroup)
- WordPress 7.2 ignores data-wp-init--, data-wp-watch-- and data-wp-run-- directives
- Ubuntu 26.04 upgrade stops with "Unable to upgrade MySQL" (mysql_native_password accounts)
- Excel for Mac crashes when you refresh a pivot table (version 16.114)
- Outlook.com / Hotmail rejects your email: "Message lacks SPF/DKIM/DMARC Auth Records (AS9031519)"
- HubSpot Pipelines API V1 stops on 4 December 2026: move /crm-pipelines/v1/pipelines calls (and the n8n HubSpot node's stage lists)
- Typer 0.29: 'TyperOption' object has no attribute 'type', click_type and typer._types are gone
- Upload button does nothing in Office panes and small windows: "File chooser dialog was suppressed" (400x300)
- react-dropzone ignores dropped files in Chrome 157: SecurityError on FileSystemFileHandle.getFile()
- Playwright v1.64.1 Docker image now has Node 26: "Cannot read properties of undefined (reading 'prototype')"
- Playwright for Python 1.65 in Docker: driver/node: error while loading shared libraries: libatomic.so.1