Skip to content

fix: return an empty string when RegexTextExtractor's capture group is not in the match - #12896

Closed
gauravch-code wants to merge 1 commit into
deepset-ai:mainfrom
gauravch-code:fix-regex-extractor-unmatched-group
Closed

gauravch-code wants to merge 1 commit into
deepset-ai:mainfrom
gauravch-code:fix-regex-extractor-unmatched-group

Conversation

@gauravch-code

Copy link
Copy Markdown
Contributor

Related Issues

  • No existing issue; found while reading RegexTextExtractor. Reproduction below.

Proposed Changes:

RegexTextExtractor returns match.group(1) whenever the pattern has a capture group. When the pattern matches but that group is not part of the match, group(1) is None. This happens with the other branch of an alternation, or with an optional group. The component then returns {"captured_text": None} even though its output type is str. For "nothing captured" it should return {"captured_text": ""}, which is what #10152 / #10304 settled on for the no-match case.

A common place this shows up is an extract-then-route pipeline. The router fails on None instead of taking the fallback route:

from haystack import Pipeline
from haystack.components.extractors import RegexTextExtractor
from haystack.components.routers import ConditionalRouter

routes = [
    {"condition": "{{ captured_text|length > 0 }}", "output": "{{ captured_text }}", "output_name": "answer", "output_type": str},
    {"condition": "{{ captured_text|length == 0 }}", "output": "{{ captured_text }}", "output_name": "fallback", "output_type": str},
]
p = Pipeline()
p.add_component("extractor", RegexTextExtractor(regex_pattern=r"Answer: (\w+)|No answer found"))
p.add_component("router", ConditionalRouter(routes=routes))
p.connect("extractor.captured_text", "router.captured_text")
p.run({"extractor": {"text_or_messages": "No answer found"}})
# main:        raises "Error evaluating condition for route ..." (captured_text is None)
# this branch: {'router': {'fallback': ''}}

The fix returns match.group(1) or "", so a group that is not part of the match gives an empty string, the same as no match at all. Matches where the group does take part are unchanged. The docstrings of run and _extract_from_text now mention this case.

How did you test it?

  • Added test_first_capture_group_not_in_match_returns_empty_string, parametrized over an alternation (Answer: (\w+)|No answer found) and an optional leading group ((https://\S+)?\s*DONE). It checks both string input and ChatMessage input. Both cases fail on main with {'captured_text': None} and pass with the fix.
  • hatch run test:unit test/components/extractors/test_regex_text_extractor.py: 27 passed.
  • Ran the pipeline above on main and on this branch.
  • ruff check, ruff format --check, and hatch run test:types on the changed files are clean; pre-commit hooks pass.

Notes for the reviewer

I chose "" over returning the whole match, so that "the pattern matched but captured nothing" behaves the same as "no match". The existing test_optional_capture_group* tests cover an optional second group and still pass.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@gauravch-code
gauravch-code requested a review from a team as a code owner September 23, 2026 20:09
@gauravch-code
gauravch-code requested review from bogdankostic and removed request for a team September 23, 2026 20:09
@vercel

vercel Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@gauravch-code is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @gauravch-code, thanks for your interest in contributing to Haystack! 🙏

⛔ First-time contributors can have at most 1 open pull request in this repository until it has been approved, so this PR was closed automatically. Your open pull request #12863 is unaffected. Once it has been approved by a maintainer, you are welcome to open more PRs. Feel free to reopen this one at that point.

See the contributing guidelines for details.

This is an automated message to help us keep the review queue healthy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant