DocumentSplitter token mode ignores skip_empty_documents for Unicode token slices

Abierto
#13,209 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

  • #13210 de @dakjdakd — cerrado sin fusionar
  • #14046 de @theluckystrike — cerrado sin fusionar

Evaluación

Este issue todavía no se ha evaluado.

Descripción

Describe the bug

DocumentSplitter(split_by="token") can emit empty documents from nonempty Unicode text despite the default skip_empty_documents=True.

A Unicode character can span several tiktoken tokens. decode_with_offsets then returns equal character offsets for adjacent tokens, so some token windows contain no text. The token path appends those windows without checking skip_empty_documents, unlike the character and custom-function paths. This can send empty chunks into document indexing/embedding pipelines.

Expected behavior

Skip empty token slices when skip_empty_documents=True, while preserving skip_empty_documents=False, source character offsets, page metadata, and overlap information.

To Reproduce

from haystack import Document
from haystack.components.preprocessors import DocumentSplitter

splitter = DocumentSplitter(
    split_by="token", split_length=1, tokenizer_encoding="cl100k_base"
)
chunks = splitter.run(documents=[Document(content="🍕🍣🚀")])["documents"]
print([ascii(chunk.content) for chunk in chunks])

On current main (c5e1335), this returns nine chunks: six empty strings and the three emoji. With a guard before appending zero-character windows, it returns only the three original emoji. The same text is reconstructed without replacement characters.

Additional context

The local change is one condition in _split_by_token, with an offline regression covering both values of skip_empty_documents and overlap on/off. The regression also checks reconstruction, character offsets, source IDs, contiguous split IDs, page numbers, and overlap ranges. All 88 unit tests in test_document_splitter.py pass; focused mypy and Ruff checks pass. No public API change is needed.

This report and the proposed patch were prepared with Codex; the reproduction was run using real tiktoken in a Hatch-managed environment. A PR will follow.

System:

  • Windows, Python 3.13.2
  • Haystack: main at c5e1335
Lenguaje dominante
Python
Estrellas
27.2k
Forks
3.5k
Merge medio
1 d 18 h
PR fusionados (30 d)
254

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de deepset-ai/haystack

Todos los issues de deepset-ai/haystack

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.