Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions haystack/components/converters/file_to_file_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

import base64
import os
from pathlib import Path
from typing import Any

Expand All @@ -16,6 +17,20 @@
_EMPTY_BYTE_STRING = b""


def _filename_from_meta(meta: dict[str, Any]) -> str | None:
"""
Get a file name from ByteStream metadata.

:param meta: The ByteStream metadata.
:returns: The `file_name` value if set, otherwise the base name of `file_path`, otherwise `None`.
"""
if file_name := meta.get("file_name"):
return str(file_name)
if file_path := meta.get("file_path"):
return os.path.basename(str(file_path))
return None


@component
class FileToFileContent:
"""
Expand Down Expand Up @@ -46,6 +61,8 @@ def run(

:param sources:
List of file paths or ByteStream objects to convert.
For a ByteStream, the file name is taken from `meta["file_name"]`, or else from the base name of
`meta["file_path"]`.
:param extra:
Optional extra information to attach to the FileContent objects. Can be used to store provider-specific
information.
Expand All @@ -69,14 +86,14 @@ def run(
if isinstance(source, str):
source = Path(source)

filename = source.name if isinstance(source, Path) else None

try:
bytestream = get_bytestream_from_source(source, guess_mime_type=True)
except Exception as e:
logger.warning("Could not read {source}. Skipping it. Error: {error}", source=source, error=e)
continue

filename = source.name if isinstance(source, Path) else _filename_from_meta(meta=bytestream.meta)

if bytestream.data == _EMPTY_BYTE_STRING:
logger.warning("File {source} is empty. Skipping it.", source=source)
continue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
``FileToFileContent`` now sets ``FileContent.filename`` for ``ByteStream`` sources from ``meta["file_name"]``,
or else from the base name of ``meta["file_path"]``. Previously the filename was always ``None`` for
``ByteStream`` sources, so providers like OpenAI received a placeholder filename.
16 changes: 16 additions & 0 deletions test/components/converters/test_file_to_file_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ def test_run_with_bytestream_sources(self, file_path: str, mime_type: str) -> No
expected_base64 = base64.b64encode(byte_stream.data).decode("utf-8")
assert file_content.base64_data == expected_base64

@pytest.mark.parametrize(
("meta", "expected_filename"),
[
({"file_name": "report.pdf"}, "report.pdf"),
({"file_path": "/tmp/uploads/report.pdf"}, "report.pdf"),
({"file_name": "report.pdf", "file_path": "/tmp/uploads/1234.pdf"}, "report.pdf"),
],
)
def test_run_with_bytestream_uses_meta_filename(self, meta: dict[str, str], expected_filename: str) -> None:
byte_stream = ByteStream.from_file_path(Path("./test/test_files/pdf/sample_pdf_1.pdf"), meta=meta)

converter = FileToFileContent()
results = converter.run(sources=[byte_stream])

assert results["file_contents"][0].filename == expected_filename

def test_run_with_no_sources(self) -> None:
converter = FileToFileContent()
results = converter.run(sources=[])
Expand Down
Loading