Skip to content

feat: Add regex pattern support for recursive chunking#499

Open
bolzzzz wants to merge 5 commits intochonkie-inc:mainfrom
bolzzzz:feature/recursive_chunker/split_by_pattern
Open

feat: Add regex pattern support for recursive chunking#499
bolzzzz wants to merge 5 commits intochonkie-inc:mainfrom
bolzzzz:feature/recursive_chunker/split_by_pattern

Conversation

@bolzzzz
Copy link
Contributor

@bolzzzz bolzzzz commented Feb 17, 2026

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bolzzzz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the RecursiveChunker by introducing robust support for regular expression patterns in text splitting. This allows for more granular and flexible control over how text is broken down into chunks, enabling users to define custom splitting logic beyond simple delimiters or whitespace. The changes include a new dedicated method for regex processing, updates to the core splitting logic to integrate this new capability, and a suite of tests to ensure its correctness and reliability.

Highlights

  • Regex Pattern Support: Implemented regex pattern support for text splitting within the RecursiveChunker, allowing for more flexible and custom text segmentation.
  • New Splitting Method: Introduced a new private method, _split_text_pattern, to manage regex-based text splitting, supporting both split and extract modes.
  • Delimiter Inclusion for Regex: Enabled flexible handling of delimiters in regex splits with include_delim options ('prev', 'next', or none), similar to existing delimiter-based splitting.
  • Refactored Splitting Logic: Updated the _split_text method to prioritize regex patterns for splitting if defined in a RecursiveLevel, and refactored _recursive_chunk to correctly apply merging logic based on the chosen splitting method.
  • Comprehensive Testing: Added extensive unit tests to validate various regex splitting scenarios, including basic splits, delimiter inclusion, extraction mode, invalid patterns, mutual exclusivity with other splitting methods, and multi-level rule application.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/chonkie/chunker/recursive.py
    • Added re import for regular expression operations.
    • Moved chonkie_core import to the top of the file for consistency.
    • Implemented _split_text_pattern method to handle text splitting using regex patterns.
    • Modified _split_text to conditionally call _split_text_pattern if a regex pattern is specified in the RecursiveLevel.
    • Updated _recursive_chunk to include curr_rule.pattern in the should_merge condition, ensuring correct merging behavior for regex-based splits.
  • tests/chunkers/test_recursive_chunker.py
    • Added regex_paragraph_rules and regex_sentence_rules pytest fixtures for reusable regex configurations.
    • Introduced test_recursive_chunker_regex_split_basic to verify fundamental regex splitting functionality.
    • Added test_recursive_chunker_regex_split_include_next to test delimiter inclusion mode 'next' for regex splits.
    • Implemented test_recursive_chunker_regex_extract_mode to validate the extraction of pattern matches.
    • Included test_recursive_chunker_regex_paragraph_reconstruction to ensure text can be fully reconstructed after regex splitting.
    • Added test_recursive_chunker_regex_indices to verify the correctness of start and end indices for regex-generated chunks.
    • Created test_recursive_chunker_regex_token_count to confirm token count adherence with regex splitting.
    • Added test_recursive_chunker_regex_invalid_pattern to ensure ValueError is raised for malformed regex patterns.
    • Introduced test_recursive_chunker_regex_and_delimiters_mutually_exclusive and test_recursive_chunker_regex_and_whitespace_mutually_exclusive to enforce that regex patterns cannot be used concurrently with delimiters or whitespace splitting.
    • Added test_recursive_chunker_regex_multilevel to test the application of regex patterns in multi-level recursive rules.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable feature by adding support for regex-based splitting in the recursive chunker, implemented with a new _split_text_pattern method and comprehensive tests. However, the current implementation has several high-severity issues. A significant logic bug exists in how it handles regex patterns with capturing groups, leading to data corruption during the split-and-reconstruct process. Additionally, the use of the standard re module for user-supplied patterns introduces a risk of ReDoS. Other issues include dropping leading delimiters when include_delim='prev' and filtering out small chunks, both contributing to data loss and preventing accurate text reconstruction.

Comment on lines +154 to +167
split_pattern = f"({pattern})"
parts = re.split(split_pattern, text)

# Reconstruct splits based on include_delim mode
splits = []
i = 0
while i < len(parts):
part = parts[i]
if not part:
i += 1
continue

# Check if this part is a delimiter (odd indices are captured groups)
is_delimiter = i % 2 == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

The regex splitting logic is flawed when the user-provided pattern contains capturing groups. The code wraps the pattern in a capturing group f"({pattern})" at line 154 and then assumes that every odd-indexed element in the re.split result is a delimiter (line 167). If the user's pattern also contains capturing groups, re.split will return those groups as well, shifting the indices and causing the logic to incorrectly identify content as delimiters and vice versa. This leads to incorrect reconstruction of the text and data corruption in the resulting chunks. Consider using re.finditer to identify match spans and manually splitting the text to ensure only the full match is treated as a delimiter, regardless of internal capturing groups.

bolzzzz and others added 2 commits February 17, 2026 20:13
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@bolzzzz bolzzzz closed this Feb 17, 2026
@bolzzzz bolzzzz reopened this Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant