ScrapeGraphAI/Scrapegraph-ai · error · ValueError

Missing or unbalanced parentheses in expression.

Error message

Missing or unbalanced parentheses in expression.

What it means

Raised after the character scan when the number of '(' characters does not equal the number of ')' characters in the input-key expression. Parenthesized groups like "(a | b) & c" are supported, but they must balance.

Source

Thrown at scrapegraphai/nodes/base_node.py:193

            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
            # Check for invalid operator sequences
            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: str) -> List[str]:
            """Evaluate an expression without parentheses."""

            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: str) -> List[str]:
            """Evaluate an expression with parentheses."""

            while "(" in expression:
                start = expression.rfind("(")
                end = expression.find(")", start)

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Count opening and closing parentheses and add/remove the missing one
  2. Prefer flatter expressions without parentheses when possible
  3. Use a linter/editor paren-matching when writing complex input expressions

Example fix

// before
input="(parsed_docs | raw_docs & user_prompt"
// after
input="(parsed_docs | raw_docs) & user_prompt"
Defensive patterns

Strategy: validation

Validate before calling

def balanced(expr: str) -> bool:
    return expr.count('(') == expr.count(')')

Prevention

When it happens

Trigger: input="(key1 | key2 & key3" (missing closing paren) or input="key1) & key2" (stray closing paren).

Common situations: Manually nesting boolean groups and dropping a parenthesis; refactoring a complex expression and losing one side of a pair.

Related errors


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