Skip to content

Commit e97e0a1

Browse files
committed
fix: strip only trailing extension in LlamaCloud local file name
get_local_file_name used str.replace(file_ext, "") to drop the extension, which removes every occurrence of the extension substring. For files whose stem also ends with the extension (e.g. report.pdf.pdf) the stem was corrupted (report instead of report.pdf). Use os.path.splitext root instead.
1 parent 97a7d9b commit e97e0a1

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@create-llama/llama-index-server": patch
3+
---
4+
5+
fix: only strip the trailing extension when building the local LlamaCloud file name
6+
7+
`get_local_file_name` used `str.replace(file_ext, "")`, which removes every
8+
occurrence of the extension string. For files whose stem also ends with the
9+
extension (e.g. `report.pdf.pdf`) this corrupted the stem (`report` instead of
10+
`report.pdf`). It now uses the root returned by `os.path.splitext`.

python/llama-index-server/llama_index/server/utils/llamacloud.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ def get_local_file_name(
2222
if llamacloud_file_name is None or pipeline_id is None:
2323
raise ValueError("Couldn't find llamacloud_file_name and pipeline_id")
2424
# Construct the local file name
25-
file_ext = os.path.splitext(llamacloud_file_name)[1]
26-
file_name = llamacloud_file_name.replace(file_ext, "")
25+
file_root, file_ext = os.path.splitext(llamacloud_file_name)
26+
# Only strip the trailing extension; using str.replace would remove every
27+
# occurrence of the extension (e.g. "report.pdf.pdf" -> "report").
28+
file_name = file_root
2729
sanitized_file_name = re.sub(r"[^A-Za-z0-9_\-]", "_", file_name)
2830
return f"{sanitized_file_name}_{pipeline_id}{file_ext}"
2931

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from llama_index.server.utils.llamacloud import (
2+
get_local_file_name,
3+
is_llamacloud_file,
4+
)
5+
6+
7+
def test_get_local_file_name_single_extension() -> None:
8+
assert (
9+
get_local_file_name(llamacloud_file_name="report.pdf", pipeline_id="123")
10+
== "report_123.pdf"
11+
)
12+
13+
14+
def test_get_local_file_name_keeps_repeated_extension_in_stem() -> None:
15+
# The stem also ends with the extension string. Only the trailing
16+
# extension must be stripped, otherwise the stem is corrupted.
17+
assert (
18+
get_local_file_name(llamacloud_file_name="report.pdf.pdf", pipeline_id="123")
19+
== "report_pdf_123.pdf"
20+
)
21+
22+
23+
def test_get_local_file_name_extension_substring_in_stem() -> None:
24+
assert (
25+
get_local_file_name(llamacloud_file_name="backup.tar.tar", pipeline_id="abc")
26+
== "backup_tar_abc.tar"
27+
)
28+
29+
30+
def test_get_local_file_name_from_metadata() -> None:
31+
metadata = {"file_name": "my.report.pdf.pdf", "pipeline_id": "p1"}
32+
assert get_local_file_name(node_metadata=metadata) == "my_report_pdf_p1.pdf"
33+
34+
35+
def test_is_llamacloud_file() -> None:
36+
assert is_llamacloud_file({"pipeline_id": "p1"}) is True
37+
assert is_llamacloud_file({"file_name": "a.pdf"}) is False

0 commit comments

Comments
 (0)