haystack icon indicating copy to clipboard operation
haystack copied to clipboard

FileTypeRouter drops detected MIME type when metadata converts a Path to ByteStream

Open Quantum-Enigma-dot opened this issue 1 month ago • 0 comments

Describe the bug

When FileTypeRouter categorizes a Path or string path, it correctly detects the source MIME type, such as text/plain.

However, when metadata is supplied through the meta argument, FileTypeRouter converts the path into a ByteStream so the metadata can be attached. The resulting ByteStream has mime_type=None, even though the router already detected the MIME type and routed the source into the correct MIME bucket.

This creates different downstream behavior depending only on whether metadata was supplied:

  • Without metadata, the router returns the original Path, allowing downstream generic converters to determine its MIME type.
  • With metadata, the router returns a ByteStream with mime_type=None, preventing those converters from recovering the MIME type from the original path.

Error message

When the resulting ByteStream is passed to FileToFileContent, Haystack logs:

Failed to guess the MIME type of the file. Omitting the MIME type may result in processing errors or incorrect handling of the file by LLM providers.

To Reproduce

import tempfile
from pathlib import Path

from haystack.components.converters.file_to_file_content import FileToFileContent
from haystack.components.routers.file_type_router import FileTypeRouter


with tempfile.NamedTemporaryFile(
    suffix=".txt",
    delete=False,
    mode="w",
    encoding="utf-8",
) as file:
    file.write("Hello Haystack!")
    path = Path(file.name)

try:
    router = FileTypeRouter(mime_types=["text/plain"])
    converter = FileToFileContent()

    without_meta = router.run(sources=[path])
    without_meta_source = without_meta["text/plain"][0]
    without_meta_content = converter.run(
        sources=[without_meta_source]
    )["file_contents"][0]

    with_meta = router.run(
        sources=[path],
        meta={"source_id": "123"},
    )
    with_meta_source = with_meta["text/plain"][0]
    with_meta_content = converter.run(
        sources=[with_meta_source]
    )["file_contents"][0]

    print(
        "Without metadata:",
        type(without_meta_source).__name__,
        without_meta_content.mime_type,
    )
    print(
        "With metadata:",
        type(with_meta_source).__name__,
        with_meta_source.mime_type,
        with_meta_content.mime_type,
    )
finally:
    path.unlink(missing_ok=True)

Actual behavior

Without metadata: WindowsPath text/plain
With metadata: ByteStream None None

The metadata path also produces the warning shown above.

The source is correctly placed in the text/plain output bucket, but the ByteStream returned from that bucket has mime_type=None.

Expected behavior

When FileTypeRouter converts a Path or string path into a ByteStream solely to attach metadata, I would expect the resulting ByteStream to retain the MIME type that the router already detected.

For this example:

with_meta_source.mime_type == "text/plain"

This would keep the metadata and no-metadata paths consistent and prevent downstream MIME information loss.

Additional context

The relevant flow in FileTypeRouter.run() is:

  1. The router detects the path MIME type with _guess_mime_type(source).
  2. When metadata is present, it calls get_bytestream_from_source(source).
  3. That utility defaults to guess_mime_type=False.
  4. Metadata is attached using dataclasses.replace, but the detected MIME type is not transferred to the new ByteStream.

I confirmed the downstream effect with FileToFileContent:

  • A routed Path produces FileContent.mime_type == "text/plain".
  • The same routed file with metadata produces a ByteStream and then FileContent.mime_type == None.

Two possible implementation directions appear to be:

  1. Reuse the MIME type already detected by FileTypeRouter when it converts a path source into a ByteStream.
  2. Ask get_bytestream_from_source to guess the MIME type during conversion, although this may repeat detection already performed by the router.

The first option appears narrower, but I would like to confirm the intended component contract before implementing it.

Is populating ByteStream.mime_type during this path-to-ByteStream conversion the desired behavior?

If so, I would be happy to submit a focused pull request with a regression test.

FAQ Check

  • I reviewed the available documentation and searched existing issues and pull requests.
  • I did not find an exact report for MIME information being lost specifically when FileTypeRouter attaches metadata.

System

  • OS: Windows 11
  • Python: 3.11
  • Haystack commit: 76fc2d3d7492ec243eed40eb939a836f2a8b29da
  • DocumentStore: Not applicable
  • Reader: Not applicable
  • Retriever: Not applicable

Quantum-Enigma-dot avatar Oct 23 '26 16:10 Quantum-Enigma-dot