DocsGPT icon indicating copy to clipboard operation
DocsGPT copied to clipboard

🐛 Bug Report: RST parser: chunk_by_token_count drops text and splits words

Open CephasCoots-12 opened this issue 1 month ago • 1 comments

📜 Description

File: docsgpt/parser/file/rst_parser.py (RstParser.chunk_by_token_count)

Problem Each chunk is cut back to its last space, but the next chunk starts at a fixed offset (i + chunk_size), so the text after that space is skipped and some words are split in the middle.

Reproduction from docsgpt.parser.file.rst_parser import RstParser text = " ".join(f"word{i:02d}" for i in range(20)) print(RstParser().chunk_by_token_count(text, max_tokens=5))

Output: ['word00 word01 word02', '03 word04 word05 word06', 'ord07 word08 word09', '0 word11 word12 word13', 'rd14 word15 word16', 'word18 word19']

word03, word07, word10, word14 and word17 are missing, and several others are cut mid-word.

Impact parse_file chunks every .rst file with max_tokens=1000 by default, so any section longer than about 5,000 characters loses text before embedding. The existing test only checks the number and size of chunks, so it did not catch this.

Proposed fix Start each chunk where the previous one ended, hard-split words longer than the chunk window, and skip whitespace-only chunks. I have a fix and regression tests ready.

I'd like to work on this for Hacktoberfest. Could you assign it to me?

👟 Reproduction steps

  1. Clone the repo and run uv sync.
  2. Run: uv run python -c "from docsgpt.parser.file.rst_parser import RstParser; t=' '.join(f'word{i:02d}' for i in range(20)); print(RstParser().chunk_by_token_count(t, max_tokens=5))"
  3. Compare the printed chunks with the input words word00 to word19.

👍 Expected behavior

Every word of the input appears exactly once across the chunks, in order, and no word is split. For example: ['word00 word01 word02', 'word03 word04 word05', ..., 'word18 word19'].

👎 Actual Behavior with Screenshots

Text is lost and words are cut in the middle: ['word00 word01 word02', '03 word04 word05 word06', 'ord07 word08 word09', '0 word11 word12 word13', 'rd14 word15 word16', 'word18 word19'] word03, word07, word10, word14 and word17 are missing. Cause: each chunk is cut back to its last space, but the next chunk starts at a fixed offset (i + chunk_size), so the text after that space is skipped.

💻 Operating system

Windows

What browsers are you seeing the problem on?

No response

🤖 What development environment are you experiencing this bug on?

Local dev server

🔒 Did you set the correct environment variables in the right path? List the environment variable names (not values please!)

None needed. The bug reproduces by calling the parser directly, with no running app, database or API keys.

📃 Provide any additional context for the Bug.

File: docsgpt/parser/file/rst_parser.py, RstParser.chunk_by_token_count. Impact: parse_file chunks every .rst file with max_tokens=1000 by default, so any section longer than about 5,000 characters loses text before embedding. The existing test only checks chunk count and size, so it did not catch this. Proposed fix: start each chunk where the previous one ended, hard-split words longer than the chunk window, and skip whitespace-only chunks. I have a fix and regression tests ready. I used AI assistance to investigate and prepare the fix, and I will test it myself.

📖 Relevant log output

['word00 word01 word02', '03 word04 word05 word06', 'ord07 word08 word09', '0 word11 word12 word13', 'rd14 word15 word16', 'word18 word19']

👀 Have you spent some time to check if this bug has been raised before?

  • [x] I checked and didn't find similar issue

🔗 Are you willing to submit PR?

Yes, I am willing to submit a PR!

🧑‍⚖️ Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

CephasCoots-12 avatar Oct 12 '26 11:10 CephasCoots-12

@CephasCoots-12, you're assigned.

  • Open a PR or post progress within 14 days; otherwise, the issue will be available for others again.
  • Read CONTRIBUTING.md and include regression tests for text preservation, long words and whitespace-only input.
  • Include a screenshot or short recording showing the fix in your PR.

Automated triage. A maintainer will follow up.

arc53-machine avatar Oct 12 '26 11:10 arc53-machine