{"record":{"id":"2e77f8cbbf0efc40","repo":"deepset-ai/haystack","slug":"pattern-pattern-contains-multiple-capture-grou","errorCode":null,"errorMessage":"Pattern '{pattern}' contains multiple capture groups. Please specify a pattern with at most one capture group.","messagePattern":"Pattern '(.+?)' contains multiple capture groups\\. Please specify a pattern with at most one capture group\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/builders/answer_builder.py","lineNumber":312,"sourceCode":"                        start, end = int(start_str), int(end_str)\n                        if start > end:\n                            continue\n                        # Clamp the range end to the number of documents to avoid materializing a huge\n                        # set from an out-of-range citation like `[1-999999999]` in the Generator output.\n                        if num_documents is not None:\n                            end = min(end, num_documents)\n                        idxs.update(range(start - 1, end))\n                    else:\n                        idxs.add(int(part) - 1)\n            else:\n                idxs.add(int(match) - 1)\n        return idxs\n\n    @staticmethod\n    def _check_num_groups_in_regex(pattern: str) -> None:\n        num_groups = re.compile(pattern).groups\n        if num_groups > 1:\n            raise ValueError(\n                f\"Pattern '{pattern}' contains multiple capture groups. \"\n                f\"Please specify a pattern with at most one capture group.\"\n            )\n","sourceCodeStart":294,"sourceCodeEnd":316,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/builders/answer_builder.py#L294-L316","documentation":"AnswerBuilder._check_num_groups_in_regex raises this when the extraction pattern has more than one regex capture group. AnswerBuilder uses exactly one capture group to extract the answer substring, so patterns with 2+ groups are ambiguous and rejected.","triggerScenarios":"Passing a pattern with multiple capturing parentheses to AnswerBuilder.run(pattern=...) or the AnswerBuilder(pattern=...) constructor — e.g. r\"(Answer: )(\\d+)\".","commonSituations":"Writing an extraction regex with a non-capturing delimiter accidentally wrapped in parentheses; copying a validation regex into the extraction pattern; converting from another regex-based extractor that supported multiple groups.","solutions":["Rewrite the pattern to have exactly one capture group around the content you want extracted.","Convert helper parentheses to non-capturing groups with (?:...).","Preprocess/strip fixed prefixes with string operations instead of capturing them."],"exampleFix":"// before\npattern = r\"(Answer: )(\\d+)\"\n// after\npattern = r\"Answer: (\\d+)\"","handlingStrategy":"validation","validationCode":"import re\nnum_groups = re.compile(pattern).groups\nassert num_groups <= 1, f\"pattern '{pattern}' has {num_groups} capture groups\"","typeGuard":null,"tryCatchPattern":"try:\n    result = answer_builder.run(replies=replies, pattern=pattern)\nexcept ValueError as e:\n    if \"multiple capture groups\" in str(e):\n        logging.error(\"Bad extraction pattern: %s\", e)\n    raise","preventionTips":["Count capture groups with re.compile(p).groups before passing patterns.","Use non-capturing (?:...) for structural grouping.","Keep extraction patterns to exactly one group around the answer text."],"tags":["haystack","answer-builder","regex","validation"],"backgroundTag":"regex-capture-groups","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}