jax-ml/jax · error · ValueError

mode must be 'right' or 'left', got {mode!r}

Error message

mode must be 'right' or 'left', got {mode!r}

What it means

jax.scipy.linalg.qr_multiply applies Q from a QR decomposition to another matrix c, either from the left (mode='left', computes Q @ c) or right (mode='right', computes c @ Q). The mode argument must be one of those two strings; anything else raises ValueError.

Source

Thrown at jax/_src/scipy/linalg.py:1118

  Examples:
    Use :func:`qr_multiply` to efficiently solve a least-squares problem.
    For an overdetermined system ``A @ x ≈ b``, pass ``b`` as a 1-D row
    via ``mode='right'`` to obtain ``Q^T @ b`` and ``R`` in one step:

    >>> import jax
    >>> import jax.numpy as jnp
    >>> A = jnp.array([[1., 1.], [1., 2.], [1., 3.], [1., 4.]])
    >>> b = jnp.array([2., 4., 5., 4.])
    >>> Qtb, R = jax.scipy.linalg.qr_multiply(A, b, mode='right')
    >>> x = jax.scipy.linalg.solve_triangular(R, Qtb)
    >>> jnp.allclose(A.T @ A @ x, A.T @ b)
    Array(True, dtype=bool)
  """
  del overwrite_a, overwrite_c  # unused
  a, c = promote_dtypes_inexact(jnp.asarray(a), jnp.asarray(c))
  if mode not in ('right', 'left'):
    raise ValueError(f"mode must be 'right' or 'left', got {mode!r}")

  onedim = c.ndim == 1
  if onedim:
    c = c[:, None] if mode == 'left' else c[None, :]

  m, n = a.shape[-2:]
  k = min(m, n)

  if mode == 'left':
    if c.shape[-2] != k:
      raise ValueError(
          f"Array shapes are not compatible for Q @ c operation: "
          f"a has shape {tuple(a.shape)} so Q has {k} columns, "
          f"but c has {c.shape[-2]} rows (expected {k}).")
  else:
    if c.shape[-1] != m:
      raise ValueError(
          f"Array shapes are not compatible for c @ Q operation: "

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass mode='left' or mode='right' exactly (lowercase)
  2. If mode comes from user input/config, normalize with str(mode).lower() and validate against {'left','right'} before the call

Example fix

// before
qr_multiply(a, c, mode=cfg.qr_side)  # cfg.qr_side == 'LEFT'
// after
mode = str(cfg.qr_side).lower()
assert mode in ('left', 'right')
qr_multiply(a, c, mode=mode)
Defensive patterns

Strategy: validation

Validate before calling

mode = str(mode).lower(); assert mode in ('left', 'right'), f'bad mode {mode}'

Type guard

null

Prevention

When it happens

Trigger: Calling jax.scipy.linalg.qr_multiply(a, c, mode='L'), mode=0, mode=None, or a typo like 'Left'.

Common situations: Porting scipy.linalg.qr_multiply code that uses the shorthand 'left'/'right' but passing a variable or incorrectly cased value; SciPy is case-sensitive too, so silent config drift (e.g. mode read from a config file) triggers this.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/c39083744500855b. Report an issue: GitHub.