jax-ml/jax · error · ValueError

The argument `side` must be either 'right' or 'left'.

Error message

The argument `side` must be either 'right' or 'left'.

What it means

polar validates the side keyword, which controls whether the factorization is A = UH (side='right') or A = HU (side='left'). Only the exact lowercase strings 'right' and 'left' are accepted; anything else hits the ValueError branch before any computation starts.

Source

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

    ...     print(P)
    [[4.79 3.25 1.23]
     [3.25 3.06 2.01]
     [1.23 2.01 2.91]]

    The original matrix can be reconstructed by multiplying the U and P:

    >>> a_reconstructed = U @ P
    >>> jnp.allclose(a, a_reconstructed)
    Array(True, dtype=bool)

  .. _QDWH: https://epubs.siam.org/doi/abs/10.1137/090774999
  """
  arr = jnp.asarray(a)
  if arr.ndim < 2:
    raise ValueError("The input `a` must be at least a 2-D array.")

  if side not in ["right", "left"]:
    raise ValueError("The argument `side` must be either 'right' or 'left'.")

  sig = "(m,n)->(m,n),(n,n)" if side == "right" else "(m,n)->(m,n),(m,m)"
  return jnp_vectorize.vectorize(
      partial(_polar_2d, side=side, method=method, eps=eps,
              max_iterations=max_iterations),
      signature=sig)(arr)


@jit
def _sqrtm_triu(T: Array) -> Array:
  """
  Implements Björck, Å., & Hammarling, S. (1983).
      "A Schur method for the square root of a matrix". Linear algebra and
      its applications", 52, 127-140.
  """
  diag = jnp.sqrt(jnp.diag(T))
  n = diag.size
  U = jnp.diag(diag)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass side='right' or side='left' exactly (lowercase)
  2. Omit side to use the default 'right'
  3. Validate/normalize the config value before calling

Example fix

# before
U, H = polar(a, side='both')
# after
U, H = polar(a, side='left')
Defensive patterns

Strategy: validation

Validate before calling

side = side.lower() if isinstance(side, str) else side
if side not in ('right', 'left'):
    raise ValueError(f"side must be 'right' or 'left', got {side!r}")
U, H = polar(a, side=side)

Type guard

def is_valid_side(s) -> bool:
    return isinstance(s, str) and s in ('right', 'left')

Try / catch

try:
    polar(a, side=side)
except ValueError as e:
    if "side must be either" in str(e):
        side = 'right'; polar(a, side=side)
    else: raise

Prevention

When it happens

Trigger: Calling polar(a, side='RIGHT'), side='both', side=None, or passing a user/config-supplied string without validation.

Common situations: Config-driven code where side comes from a settings file; case or spelling mistakes when porting code from another library.

Related errors


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