ValueError
builtins.ValueError
Stack trace
ValueError: chunking empty document after split error
File "app.py", line 42, in process_chunks
chunks = splitter.split_text(document)
File "langchain/text_splitter.py", line 88, in split_text
raise ValueError("chunking empty document after split error") Why it happens
This error happens because the document splitter receives an empty or whitespace-only string or the splitting parameters cause all text to be filtered out, resulting in zero chunks. Downstream code expects at least one chunk and fails when none are returned.
Detection
Check the output of your document splitter before further processing. Assert that the chunk list is not empty and log the original document length and content to detect empty inputs early.
Causes & fixes
Input document is empty or contains only whitespace
Validate and sanitize input documents before splitting to ensure they contain meaningful text.
Splitter parameters (chunk size, overlap) are too restrictive, filtering out all content
Adjust chunk size and overlap parameters to allow at least one chunk to be created from the input.
Splitter logic removes all content due to aggressive filtering or incorrect regex patterns
Review and correct splitter filtering logic or regex to preserve valid text chunks.
Code: broken vs fixed
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
document = "" # Empty document
chunks = splitter.split_text(document) # This line raises ValueError
print(chunks) import os
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
document = "" # Empty document
chunks = splitter.split_text(document)
if not chunks:
print("Warning: No chunks created from document. Skipping processing.")
else:
print(chunks) # Proceed only if chunks exist Workaround
Wrap the splitter call in try/except ValueError, and if caught, log the input document and skip chunk processing to avoid crashes.
Prevention
Validate input documents for non-empty content before splitting and configure splitter parameters to ensure at least one chunk is generated for any valid input.