deepset-ai/haystack · error · ValueError
Pattern '{pattern}' contains multiple capture groups. Please
Error message
Pattern '{pattern}' contains multiple capture groups. Please specify a pattern with at most one capture group. What it means
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.
Source
Thrown at haystack/components/builders/answer_builder.py:312
start, end = int(start_str), int(end_str)
if start > end:
continue
# Clamp the range end to the number of documents to avoid materializing a huge
# set from an out-of-range citation like `[1-999999999]` in the Generator output.
if num_documents is not None:
end = min(end, num_documents)
idxs.update(range(start - 1, end))
else:
idxs.add(int(part) - 1)
else:
idxs.add(int(match) - 1)
return idxs
@staticmethod
def _check_num_groups_in_regex(pattern: str) -> None:
num_groups = re.compile(pattern).groups
if num_groups > 1:
raise ValueError(
f"Pattern '{pattern}' contains multiple capture groups. "
f"Please specify a pattern with at most one capture group."
)
View on GitHub (pinned to e318778c9b)
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.
Example fix
// before pattern = r"(Answer: )(\d+)" // after pattern = r"Answer: (\d+)"
Defensive patterns
Strategy: validation
Validate before calling
import re
num_groups = re.compile(pattern).groups
assert num_groups <= 1, f"pattern '{pattern}' has {num_groups} capture groups" Try / catch
try:
result = answer_builder.run(replies=replies, pattern=pattern)
except ValueError as e:
if "multiple capture groups" in str(e):
logging.error("Bad extraction pattern: %s", e)
raise Prevention
- 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.
When it happens
Trigger: Passing a pattern with multiple capturing parentheses to AnswerBuilder.run(pattern=...) or the AnswerBuilder(pattern=...) constructor — e.g. r"(Answer: )(\d+)".
Common situations: 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.
Related errors
- Number of replies ({len(replies)}), and metadata ({len(meta)
- The following tool names are not valid: {invalid_tool_names}
- user_prompt must define exactly one message block, found {le
- system_prompt must define exactly one message block, found {
- 'response_fn' must return an assistant ChatMessage, got '{re
AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30).
Data as JSON: /api/errors/2e77f8cbbf0efc40.
Report an issue: GitHub.