unclecode/crawl4ai · error · ValueError

Invalid regex for '{lbl}': {e}

Error message

Invalid regex for '{lbl}': {e}

What it means

Error "Invalid regex for '{lbl}': {e}" thrown in unclecode/crawl4ai.

Source

Thrown at crawl4ai/extraction_strategy.py:2825

            extra_args=kwargs,
        )

        # ── clean & load JSON (fix common escape mistakes *before* json.loads)
        raw = resp.choices[0].message.content
        raw = raw.replace("\x08", "\\b")                     # stray back-space → \b
        raw = re.sub(r'(?<!\\)\\(?![\\u"])', r"\\\\", raw)   # lone \ → \\

        try:
            pattern_dict = json.loads(raw)
        except Exception as exc:
            raise ValueError(f"LLM did not return valid JSON: {raw}") from exc

        # quick sanity-compile
        for lbl, pat in pattern_dict.items():
            try:
                re.compile(pat)
            except re.error as e:
                raise ValueError(f"Invalid regex for '{lbl}': {e}") from None

        return pattern_dict

View on GitHub (pinned to 7e80152142)

Solutions

  1. Fix the regular expression for the given label; test it with re.compile before passing it to the strategy.
  2. Escape special characters that are meant to be literal in the pattern.

Example fix

re.compile(pattern)  # validate before use in RegexExtractionStrategy

When it happens

Trigger: Thrown at crawl4ai/extraction_strategy.py:2825 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of unclecode/crawl4ai@7e80152142 (2026-08-14). Data as JSON: /api/errors/2606f9dcb831503a. Report an issue: GitHub.