numpy/numpy · error · ValueError
ValueError: order must be one of 'C', 'F', 'A', or 'K' (got
Error message
ValueError: order must be one of 'C', 'F', 'A', or 'K' (got '{order}') What it means
Raised by _parse_output_order when the order keyword passed to an optimized einsum contraction is not one of 'C', 'F', 'A', 'K' (compared case-insensitively after .upper()). Note the message redundantly prefixes itself with 'ValueError:'; the value of order is echoed. This parser decides the memory layout of the matmul result.
Source
Thrown at numpy/_core/einsumfunc.py:1138
perm_ab,
False, # pure_multiplication=False
)
@functools.lru_cache(maxsize=64)
def _parse_output_order(order, a_is_fcontig, b_is_fcontig):
order = order.upper()
if order == "K":
return None
elif order in "CF":
return order
elif order == "A":
if a_is_fcontig and b_is_fcontig:
return "F"
else:
return "C"
else:
raise ValueError(
"ValueError: order must be one of "
f"'C', 'F', 'A', or 'K' (got '{order}')"
)
def bmm_einsum(eq, a, b, out=None, **kwargs):
"""Perform arbitrary pairwise einsums using only ``matmul``, or
``multiply`` if no contracted indices are involved (plus maybe single term
``einsum`` to prepare the terms individually). The logic for each is cached
based on the equation and array shape, and each step is only performed if
necessary.
Parameters
----------
eq : str
The einsum equation.
a : array_like
The first array to contract.View on GitHub (pinned to e117b3ca4e)
Solutions
- Use one of 'C', 'F', 'A', 'K' (any case).
- Use 'K' (default) to keep input memory order, or 'C'/'F' to force a layout.
- Avoid trailing spaces; the value is uppercased but not stripped.
Example fix
// before
np.einsum('ij,jk->ik', a, b, optimize=True, order='Z')
// after
np.einsum('ij,jk->ik', a, b, optimize=True, order='K') # C, F, A, or K Defensive patterns
Strategy: validation
Validate before calling
VALID_ORDER = {'C', 'F', 'A', 'K'}
def safe_order(order):
if order is None:
return 'K'
o = str(order).upper()
if o not in VALID_ORDER:
raise ValueError(f"order must be one of C/F/A/K, got {order!r}")
return o Type guard
null
Try / catch
null
Prevention
- Default to 'K' to preserve memory layout.
- Avoid trailing whitespace in order strings (not stripped).
- Centralize layout constants.
When it happens
Trigger: np.einsum('ij,jk->ik', a, b, optimize=True, order='Z') or order='Row-major' or any string other than C/F/A/K (case-insensitive). Also triggered by a non-string order that has no matching upper().
Common situations: Passing a layout constant from another library; typo like 'c ' with trailing space (actually fine after upper/strip? no—no strip, so ' c' fails); confusing with numpy's set_printoptions or ndarray order semantics.
Related errors
- No input operands
- Character {s} is not a valid symbol.
- For this input type lists must contain either int or Ellipsi
- Subscripts can only contain one '->'.
- Invalid Ellipses.
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/5928ed36b6b408ea.
Report an issue: GitHub.