jax-ml/jax · error · ValueError

Compound factors should be one level, nested brackets are no

Error message

Compound factors should be one level, nested brackets are not allowed

What it means

Compound factors support only one level of parentheses, e.g. '(a,b)'. _parse_values tracks current_compound_dim and rejects a second '(' while already inside a compound factor.

Source

Thrown at jax/_src/custom_partitioning_sharding_rule.py:282

            "Ellipsis can only be used at the beginning of a dimension")
      if rule_index < rule_len and rule[rule_index].isdigit():
        batching_group_str = ""
        while rule_index < rule_len and rule[rule_index].isdigit():
          batching_group_str += rule[rule_index]
          rule_index += 1
        batching_group = str(int(batching_group_str))
      else:
        batching_group = "0"

      add_factor(f"{BATCHING}{batching_group}")
      continue
    if char in "(), ":
      if current_factor is not None:
        add_factor(current_factor)
        current_factor = None
      if char == "(":
        if current_compound_dim is not None:
          raise ValueError(
              "Compound factors should be one level, nested brackets are not"
              " allowed")
        current_compound_dim = []
      elif char == ")":
        if current_compound_dim is None:
          raise ValueError("Brackets are not balanced")
        if len(current_compound_dim) <= 1:
          raise ValueError("Brackets should contain at least two factors")
        value.append(CompoundFactor(*current_compound_dim))
        current_compound_dim = None
      elif char == ",":
        all_values.append(ArrayMapping(*value))
        value = []
    elif char == "_" or char.isdigit() or char.isalpha():
      if current_factor is None:
        if str.isdigit(char):
          raise ValueError(f"Factor names have to start with a letter, but got '{char}'")
        current_factor = char

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Flatten to a single level: '(a,b,c)'
  2. Express deeper structure via factor_sizes and separate factors instead of nesting

Example fix

# before
rule = '((a,b),c)->(a,b,c)'
# after
rule = '(a,b,c)->(a,b,c)'
Defensive patterns

Strategy: validation

Validate before calling

import re
assert not re.search(r'\([^)]*\(', rule), 'nested compound factors not allowed'

Type guard

import re
def is_flat_compounds(rule: str) -> bool:
    return re.search(r'\([^)]*\(', rule) is None

Prevention

When it happens

Trigger: Rule containing '((a,b),c)' or '(a,(b,c))'.

Common situations: Trying to express hierarchical factor trees like mesh axis trees in the rule string.

Related errors


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