Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: put correct file type in langflow-less ingestion (#1842)

Lucas Oliveira5bf8fe4ca379
diff --git a/src/api/router.py b/src/api/router.py
index b92f2c9b0..81165fb87 100644
--- a/src/api/router.py
+++ b/src/api/router.py
@@ -1,6 +1,8 @@
"""Router endpoints that automatically route based on configuration settings."""
import json
+import mimetypes
+import os
import tempfile
from fastapi import Depends, File, Form, UploadFile
@@ -105,8 +107,17 @@ async def _traditional_upload_ingest_task(
for upload_file in upload_files:
content = await upload_file.read()
original_filenames.append(upload_file.filename)
- # Generate unique temp file to avoid collisions
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".tmp")
+ # Generate unique temp file with the original extension to assist docling/format detection
+ suffix = os.path.splitext(upload_file.filename)[1] if upload_file.filename else ""
+ if not suffix and upload_file.content_type:
+ from utils.file_utils import get_file_extension
+
+ suffix = get_file_extension(upload_file.content_type)
+ if not suffix:
+ suffix = mimetypes.guess_extension(upload_file.content_type)
+ if not suffix:
+ suffix = ".tmp"
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
temp_path = temp_file.name
temp_file.close()
with open(temp_path, "wb") as f:
@@ -206,8 +217,17 @@ async def _langflow_upload_ingest_task(
for upload_file in upload_files:
content = await upload_file.read()
original_filenames.append(upload_file.filename)
- # Generate unique temp file to avoid collisions
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".tmp")
+ # Generate unique temp file with the original extension to assist docling/format detection
+ suffix = os.path.splitext(upload_file.filename)[1] if upload_file.filename else ""
+ if not suffix and upload_file.content_type:
+ from utils.file_utils import get_file_extension
+
+ suffix = get_file_extension(upload_file.content_type)
+ if not suffix:
+ suffix = mimetypes.guess_extension(upload_file.content_type)
+ if not suffix:
+ suffix = ".tmp"
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
temp_path = temp_file.name
temp_file.close()
with open(temp_path, "wb") as f:
diff --git a/tests/unit/config/test_disable_ingest_setting.py b/tests/unit/config/test_disable_ingest_setting.py
index a0a3bb0ac..c6ff0e571 100644
--- a/tests/unit/config/test_disable_ingest_setting.py
+++ b/tests/unit/config/test_disable_ingest_setting.py
@@ -115,3 +115,132 @@ async def test_traditional_upload_ingest_task(monkeypatch):
mock_task_service.create_upload_task.assert_called_once()
mock_ensure_index.assert_called_once_with("mock-jwt-token")
+
+ # Extract file_paths, assert extension is preserved, and clean them up
+ call_kwargs = mock_task_service.create_upload_task.call_args[1]
+ file_paths = call_kwargs.get("file_paths")
+ assert file_paths is not None
+ assert len(file_paths) == 1
+ assert file_paths[0].endswith(".txt")
+ import os
+
+ for path in file_paths:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+async def test_langflow_upload_ingest_task(monkeypatch):
+ """Verify that langflow ingestion invokes task_service.create_langflow_upload_task and preserves extension."""
+ from unittest.mock import AsyncMock, MagicMock
+
+ from fastapi import UploadFile
+
+ from api.router import _langflow_upload_ingest_task
+ from session_manager import User
+
+ mock_file = MagicMock(spec=UploadFile)
+ mock_file.filename = "presentation.pptx"
+ mock_file.read = AsyncMock(return_value=b"Dummy pptx content")
+
+ mock_task_service = MagicMock()
+ mock_task_service.create_langflow_upload_task = AsyncMock(return_value="langflow-task-123")
+
+ mock_langflow_file_service = MagicMock()
+ mock_session_manager = MagicMock()
+
+ mock_user = MagicMock(spec=User)
+ mock_user.user_id = "test-user-id"
+ mock_user.name = "Test User"
+ mock_user.email = "[email protected]"
+ mock_user.jwt_token = "mock-jwt-token"
+
+ response = await _langflow_upload_ingest_task(
+ upload_files=[mock_file],
+ session_id="session-456",
+ settings_json=None,
+ tweaks_json=None,
+ replace_duplicates=True,
+ create_filter=False,
+ langflow_file_service=mock_langflow_file_service,
+ session_manager=mock_session_manager,
+ task_service=mock_task_service,
+ user=mock_user,
+ )
+
+ assert response.status_code == 202
+ import json
+
+ data = json.loads(response.body.decode())
+ assert data["task_id"] == "langflow-task-123"
+ assert data["file_count"] == 1
+ assert data["filename"] == "presentation.pptx"
+
+ mock_task_service.create_langflow_upload_task.assert_called_once()
+
+ # Extract file_paths, assert extension is preserved, and clean them up
+ call_kwargs = mock_task_service.create_langflow_upload_task.call_args[1]
+ file_paths = call_kwargs.get("file_paths")
+ assert file_paths is not None
+ assert len(file_paths) == 1
+ assert file_paths[0].endswith(".pptx")
+ import os
+
+ for path in file_paths:
+ if os.path.exists(path):
+ os.unlink(path)
+
+
+async def test_traditional_upload_ingest_mime_fallback(monkeypatch):
+ """Verify that if the filename has no extension, the temporary file suffix is resolved from content_type."""
+ from unittest.mock import AsyncMock, MagicMock
+
+ from fastapi import UploadFile
+
+ from api.router import _traditional_upload_ingest_task
+ from session_manager import User
+
+ mock_file = MagicMock(spec=UploadFile)
+ mock_file.filename = "no_extension_file"
+ mock_file.content_type = "application/pdf"
+ mock_file.read = AsyncMock(return_value=b"Dummy content")
+
+ mock_task_service = MagicMock()
+ mock_task_service.create_upload_task = AsyncMock(return_value="task-mime-123")
+
+ mock_session_manager = MagicMock()
+ mock_user = MagicMock(spec=User)
+ mock_user.user_id = "test-user-id"
+ mock_user.name = "Test User"
+ mock_user.email = "[email protected]"
+ mock_user.jwt_token = "mock-jwt-token"
+
+ # Mock _ensure_index_exists to avoid calling opensearch
+ mock_ensure_index = AsyncMock()
+ monkeypatch.setattr("api.documents._ensure_index_exists", mock_ensure_index)
+
+ response = await _traditional_upload_ingest_task(
+ upload_files=[mock_file],
+ replace_duplicates=True,
+ create_filter=False,
+ session_manager=mock_session_manager,
+ task_service=mock_task_service,
+ user=mock_user,
+ )
+
+ assert response.status_code == 202
+ mock_task_service.create_upload_task.assert_called_once()
+
+ # Extract file_paths and verify the extension was resolved to .pdf from content_type
+ call_kwargs = mock_task_service.create_upload_task.call_args[1]
+ file_paths = call_kwargs.get("file_paths")
+ assert file_paths is not None
+ assert len(file_paths) == 1
+ assert file_paths[0].endswith(".pdf")
+
+ import os
+
+ for path in file_paths:
+ if os.path.exists(path):
+ os.unlink(path)