Repository navigation
Python: Fix text chunker merging the last paragraph past max_tokens - #14567
Closed
Franco Ceballos Rastello (xThreeh) wants to merge 1 commit into
Closed
Franco Ceballos Rastello (xThreeh) wants to merge 1 commit into
Franco Ceballos Rastello (xThreeh) wants to merge 1 commit into
Conversation
_split_text_paragraph merges a short last paragraph into the previous
one, but the merge check counted words (split(" ")) instead of calling
token_counter. A word is usually more than one token, so the merged
paragraph could exceed max_tokens.
Measure the merged text with token_counter and only merge when it fits.
The merged text itself is unchanged.
Eight existing tests expected a last chunk above max_token_per_line
(16 tokens with a limit of 15, 18 with 17); their last chunk is now
split instead. A new test checks that no paragraph exceeds the limit.
Fixes microsoft#14566
Franco Ceballos Rastello (xThreeh)
requested a review
from a team
as a code owner
October 7, 2026 19:09
Franco Ceballos Rastello (xThreeh)
temporarily deployed
to
github-app-auth
October 7, 2026 19:09 — with
GitHub Actions
Inactive
Franco Ceballos Rastello (xThreeh)
temporarily deployed
to
github-app-auth
October 7, 2026 19:09 — with
GitHub Actions
Inactive
Author
|
Closing: #14238 already fixes the same merge check in |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #14566
split_plaintext_paragraphandsplit_markdown_paragraphcan return a paragraph abovemax_tokens. When the last paragraph is short,_split_text_paragraphmerges it into the previous one, but the merge check counts words (split(" ")) instead of usingtoken_counter. A word is usually more than one token, so the merged paragraph can go over the limit. Withtiktokenas the counter I saw chunks of up to 314 tokens formax_tokens=256.Description
token_counter(merged) <= max_tokens. The merged text is the same as before; only the condition changes.test_split_text_paragraph_evenly_respects_max_tokens. It fails onmain(last chunk 16 tokens, limit 15) and passes with this change.max_token_per_line(16 for 15, 18 for 17). Everything before the last chunk is unchanged; the last chunk is now split in two.pytest tests/unit/text tests/unit/core_pluginspasses,mypyis clean ontext_chunker.py, and the repo's pre-commit hooks pass on the changed files.Contribution Checklist