Skip to content

fix: return empty string when RegexTextExtractor's first capture group does not participate - #13648

Closed
Mystery01092000 wants to merge 1 commit into
deepset-ai:mainfrom
Mystery01092000:fix/regex-extractor-nonparticipating-group
Closed

Mystery01092000 wants to merge 1 commit into
deepset-ai:mainfrom
Mystery01092000:fix/regex-extractor-nonparticipating-group

Conversation

@Mystery01092000

@Mystery01092000 Mystery01092000 commented Nov 9, 2026 •

Copy link
Copy Markdown

Related Issues

Proposed Changes:

Problem. RegexTextExtractor declares captured_text: str, but it returned {"captured_text": None} when the pattern matched and its first capture group did not take part in the match. Two examples: an alternation like Score: (\d+)|N/A matched against "N/A", and an optional group like Answer:( \w+)? matched against "Answer:". A downstream component then receives None where it expects a string.

Cause. _extract_from_text returned match.group(1) whenever the pattern had groups. Python's re returns None for a group that did not participate in the match.

Fix. Return match.group(1) or "", so the result is "" in that case. I picked "" over falling back to the whole match (group(0)) because:

The run and _extract_from_text docstrings now describe this case. I also added a release note under fixes.

How did you test it?

The repro script from the issue, run with hatch -e test run python repro.py:

# before (upstream main 6fad0440ca)
{'captured_text': '7'}
{'captured_text': None}
{'captured_text': None}

# after
{'captured_text': '7'}
{'captured_text': ''}
{'captured_text': ''}

I added three unit tests to test/components/extractors/test_regex_text_extractor.py: an unmatched alternation branch, an unmatched optional group, and the same case passed in as a ChatMessage list.

$ hatch run test:unit test/components/extractors/test_regex_text_extractor.py --no-cov
# source change stashed:
FAILED ...::test_first_capture_group_in_unmatched_alternation_branch_returns_empty_string
FAILED ...::test_unmatched_optional_first_capture_group_returns_empty_string
FAILED ...::test_first_capture_group_not_participating_in_chat_message_returns_empty_string
3 failed, 25 passed
# with the fix:
28 passed

Other checks:

  • hatch run fmt-check: all checks passed, all files already formatted
  • hatch run test:types: Success: no issues found in 568 source files

I ran these locally on Windows with Python 3.12. I did not run the full unit suite.

Notes for the reviewer

#13500 made the same change and a reviewer confirmed it worked, but it was closed unmerged because its author never signed the CLA. Before that, #12896 was closed by the first-PR limit. This PR picks up the same approach, with an extra test for the ChatMessage path.

This PR was fully generated with an AI assistant, which also ran the tests and checks listed above.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes. (Not done separately; the analysis is in this PR.)
  • 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. (I ran ruff and mypy through hatch, not the pre-commit hooks themselves.)

…ot participate

When the pattern matched but its first capture group took no part in the
match (e.g. 'Score: (\d+)|N/A' on 'N/A', or an unmatched optional group),
match.group(1) returned None and the component emitted
{'captured_text': None} despite declaring a str output.

Return '' in that case, consistent with the no-match result.

Fixes deepset-ai#13484
@Mystery01092000
Mystery01092000 requested a review from a team as a code owner November 9, 2026 20:36
@Mystery01092000
Mystery01092000 requested review from anakin87 and removed request for a team November 9, 2026 20:36
@vercel

vercel Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@Mystery01092000 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Nov 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Mystery01092000

Copy link
Copy Markdown
Author

Hi @anakin87 @davidsbatista, this picks up the fix for #13484 after #13500 went stale over the CLA. It returns an empty string when the first capture group doesn't take part in the match, and adds three tests. Could one of you take a look when you have a moment? Thanks!

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Mystery01092000, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from anakin87 November 9, 2026 21:51
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Nov 9, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 9, 2026 21:51
@davidsbatista

Copy link
Copy Markdown
Contributor

Hi @anakin87 @davidsbatista, this picks up the fix for #13484 after #13500 went stale over the CLA. It returns an empty string when the first capture group doesn't take part in the match, and adds three tests. Could one of you take a look when you have a moment? Thanks!

In order for us to start the reviewing process please sign the CLA

@davidsbatista

davidsbatista commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

close in favor of #13649 since CLA was not signed

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

Labels

cla-pending PR is in draft until the contributor signs the CLA P3 Low priority, leave it in the backlog topic:tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RegexTextExtractor returns captured_text None when the capture group is not part of the match

4 participants