DocsGPT icon indicating copy to clipboard operation
DocsGPT copied to clipboard

🐛 Bug Report: Severe RAG degradation and network latency due to sequential embedding and mid-word chunking

Open abhay-2108 opened this issue 6 months ago • 5 comments

📜 Description

There are two severe architectural bugs actively bottlenecking the speed of document ingestion and degrading the quality of RAG retrieval:

Sequential Network Latency: In application/parser/embedding_pipeline.py, the embed_and_store_documents function iterates through chunks sequentially via store.add_texts([doc.page_content]). A document split into 1,000 chunks results in 1,000 separate synchronous HTTP requests. For remote embeddings (like OpenAI), this sequential I/O creates massive latency and artificial bottlenecks. Semantic Context Loss: In application/parser/chunking.py, the classic_chunk strategy uses a naive token-counting cut (body_tokens[current_position:end_position]). This mathematically slices words and sentences in half and provides zero token overlap across boundaries. This destroys the semantic context the LLM relies on during retrieval, breaks sentence structures, and generates orphaned, meaningless chunks.

👟 Reproduction steps

  1. Upload a large 50+ page document using a remote embedding model (e.g., OpenAI).
  2. Observe the severe ingestion delay due to hundreds of sequential, synchronous HTTP requests in the backend logs.
  3. Once ingested, query the document for information that was split across a token boundary.
  4. Observe that the context is lost or hallucinated because the classic_chunk algorithm severed the sentence in half without any token overlap.

👍 Expected behavior

Embedding: embed_and_store_documents should utilize vector store batching (store.add_texts(batch)) to send chunks in batches (e.g., 100 at a time), radically reducing HTTP overhead and resolving the ingestion delay.

Chunking: Document chunking should utilize recursive text splitting (e.g., Langchain's RecursiveCharacterTextSplitter, which is already in requirements.txt) to respect sentence boundaries and support token overlap, ensuring context survives boundary splits.

👎 Actual Behavior with Screenshots

Ingestion of large files hangs for an exceptionally long time.

The RAG pipeline returns degraded responses because the provided chunks contain sentences cut cleanly in half by the token limiter. (No screenshots applicable as this is a backend architectural bug).

💻 Operating system

Windows

What browsers are you seeing the problem on?

Chrome

🤖 What development environment are you experiencing this bug on?

Docker

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

Yes

VECTOR_STORE EMBEDDINGS_KEY LLM_NAME

📃 Provide any additional context for the Bug.

I have already set up a local development environment and verified these architectural bugs. I plan to refactor the loop in embedding_pipeline.py to accumulate docs.page_content into batch arrays and update the _record_progress checkpointing to track per batch. For chunking.py, I will replace/augment classic_chunk with RecursiveCharacterTextSplitter.from_tiktoken_encoder.

📖 Relevant log output


👀 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

abhay-2108 avatar May 26 '26 04:05 abhay-2108

Give it a try please.

Also in your ingestion pipeline comparison please include some benchmark files for comparison to compare results

dartpain avatar May 26 '26 17:05 dartpain

Hey @abhay-2108, thanks for the detailed writeup this is a great catch. I saw you mentioned you're planning to submit a PR for this. Are you actively working on it, or would it be okay if I took a shot at it? Happy to coordinate (e.g. split the two fixes batching vs. chunking) if you'd rather split the work. Let me know either way!

Aditya2OO4 avatar Aug 03 '26 10:08 Aditya2OO4

No activity for 30 days and no open PR for this issue, so the assignment has been cleared. This is open for anyone to work on again.

Automated triage. A maintainer will follow up.

arc53-machine avatar Oct 01 '26 07:10 arc53-machine

Apologies for the delay @dartpain ! I'm ready to wrap this up and open a PR. Could you please assign this issue back to me?

abhay-2108 avatar Oct 01 '26 15:10 abhay-2108

Hi @dartpain,

I have opened PR #2919 to address this issue: https://github.com/arc53/DocsGPT/pull/2919.

It implements the token overlap changes using the recursive chunker and sets it as the default strategy. Whenever you have a moment, could you please take a look and share your feedback?

Thanks!

abhay-2108 avatar Oct 02 '26 16:10 abhay-2108