ScrapeGraphAI/Scrapegraph-ai · error · ValueError

Invalid operator placement: operators cannot be adjacent.

Error message

Invalid operator placement: operators cannot be adjacent.

What it means

ValueError from parse_expression: two operator characters are adjacent (e.g. '&|' or '|&' after spaces are stripped). This is a per-character scan complementing the doubled-operator checks.

Source

Thrown at scrapegraphai/utils/parse_state_keys.py:68

    if (
        expression[0] in "&|"
        or expression[-1] in "&|"
        or "&&" in expression
        or "||" in expression
        or "&|" in expression
        or "|&" in expression
    ):
        raise ValueError("Invalid operator usage.")

    open_parentheses = close_parentheses = 0
    for i, char in enumerate(expression):
        if char == "(":
            open_parentheses += 1
        elif char == ")":
            close_parentheses += 1
        if char in "&|" and i + 1 < len(expression) and expression[i + 1] in "&|":
            raise ValueError(
                "Invalid operator placement: operators cannot be adjacent."
            )

    if open_parentheses != close_parentheses:
        raise ValueError("Missing or unbalanced parentheses in expression.")

    def evaluate_simple_expression(exp):
        for or_segment in exp.split("|"):
            and_segment = or_segment.split("&")
            if all(elem.strip() in state for elem in and_segment):
                return [elem.strip() for elem in and_segment if elem.strip() in state]
        return []

    def evaluate_expression(expression):
        while "(" in expression:
            start = expression.rfind("(")
            end = expression.find(")", start)
            sub_exp = expression[start + 1 : end]

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Fix the expression to have exactly one operator between operands
  2. Build expressions programmatically from validated tokens instead of string concatenation

Example fix

# before
expr = 'a & | b'
# after
expr = 'a | b'
Defensive patterns

Strategy: validation

Validate before calling

compacted = expression.replace(" ", "")
assert not re.search(r"[&|]{2,}", compacted), "adjacent operators"

Try / catch

try:
    keys = parse_expression(expr, state)
except ValueError:
    expr = re.sub(r"\s*([&|])\s*([&|])\s*", r"\1", expr)  # collapse accidental doubles
    keys = parse_expression(expr, state)

Prevention

When it happens

Trigger: Expressions like 'a & | b' — after space removal it becomes 'a&|b', triggering this error during the character loop.

Common situations: Typos or malformed concatenation of condition fragments producing adjacent operators.

Related errors


AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28). Data as JSON: /api/errors/110e51baa38b238a. Report an issue: GitHub.