OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
fix: Data ingestion fails due to OpenSearch "documents" index not found exception
Mike Pawlowski5e01c2320936
diff --git a/src/config/settings.py b/src/config/settings.pyindex 51a7906f9..51a89b807 100644--- a/src/config/settings.py+++ b/src/config/settings.py@@ -356,9 +356,8 @@ class AppClients:)# Wait for Langflow to be healthy before generating API key- # Deferred import to avoid circular dependency (langflow_utils imports config.settings.clients)from utils.langflow_utils import wait_for_langflow- await wait_for_langflow()+ await wait_for_langflow(langflow_http_client=self.langflow_http_client)# Generate Langflow API key now that Langflow is confirmed readyawait get_langflow_api_key()diff --git a/src/utils/langflow_utils.py b/src/utils/langflow_utils.pyindex f48f33e70..ad1cf9896 100644--- a/src/utils/langflow_utils.py+++ b/src/utils/langflow_utils.py@@ -1,7 +1,8 @@import asyncioimport random-from config.settings import clients+import httpx+from utils.logging_config import get_loggerlogger = get_logger(__name__)@@ -11,10 +12,17 @@ class LangflowNotReadyError(Exception):"""Raised when Langflow fails to become ready within the retry limit."""-async def wait_for_langflow(max_retries: int = 10, base_delay: float = 1.0, max_delay: float = 20.0) -> None:+async def wait_for_langflow(+ langflow_http_client: httpx.AsyncClient | None = None,+ max_retries: int = 10,+ base_delay: float = 1.0,+ max_delay: float = 20.0,+) -> None:"""Wait for Langflow to be ready with exponential backoff and jitter.Args:+ langflow_http_client: The httpx client to use for health checks. If None,+ falls back to clients.langflow_http_client from config.settings.max_retries: Maximum number of retry attempts.base_delay: Initial delay in seconds before the first retry.max_delay: Upper bound in seconds for the retry delay.@@ -22,6 +30,10 @@ async def wait_for_langflow(max_retries: int = 10, base_delay: float = 1.0, max_Raises:LangflowNotReadyError: If Langflow fails to become ready within the retry limit."""+ if langflow_http_client is None:+ from config.settings import clients+ langflow_http_client = clients.langflow_http_client+for attempt in range(max_retries):display_attempt: int = attempt + 1@@ -32,7 +44,7 @@ async def wait_for_langflow(max_retries: int = 10, base_delay: float = 1.0, max_)try:- response = await clients.langflow_http_client.get("/health", timeout=5.0)+ response = await langflow_http_client.get("/health", timeout=5.0)status_code = response.status_codeif status_code == 200:diff --git a/tests/conftest.py b/tests/conftest.pyindex c332d33bf..1da7c20f4 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -15,9 +15,9 @@ load_dotenv()os.environ['GOOGLE_OAUTH_CLIENT_ID'] = ''os.environ['GOOGLE_OAUTH_CLIENT_SECRET'] = ''-from src.config.settings import clients-from src.session_manager import SessionManager-from src.main import generate_jwt_keys+from config.settings import clients+from session_manager import SessionManager+from main import generate_jwt_keys@pytest_asyncio.fixture(scope="session", autouse=True)@@ -48,7 +48,7 @@ async def onboard_system():await clients.initialize()# Create app and perform onboarding via API- from src.main import create_app, startup_tasks+ from main import create_app, startup_tasksimport httpxapp = await create_app()diff --git a/tests/integration/test_api_endpoints.py b/tests/integration/test_api_endpoints.pyindex bbc633aa7..fb47ef315 100644--- a/tests/integration/test_api_endpoints.py+++ b/tests/integration/test_api_endpoints.py@@ -46,7 +46,7 @@ async def wait_for_service_ready(client: httpx.AsyncClient, timeout_s: float = 3- POST /search with query "*" avoids embeddings and checks OpenSearch/index readiness."""# First test OpenSearch JWT directly- from src.session_manager import SessionManager, AnonymousUser+ from session_manager import SessionManager, AnonymousUserimport osimport hashlibimport jwt as jwt_lib@@ -124,26 +124,19 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_ingesimport sys# Clear cached modules so settings pick up env and router sees new flagfor mod in [- "src.api.router",- "api.router", # Also clear the non-src path- "src.api.connector_router",+ "api.router","api.connector_router",- "src.config.settings","config.settings",- "src.auth_middleware","auth_middleware",- "src.main",- "api", # Clear the api package itself- "src.api",- "services", # Clear services that import clients- "src.services",+ "main",+ "api",+ "services","services.search_service",- "src.services.search_service",]:sys.modules.pop(mod, None)- from src.main import create_app, startup_tasks- import src.api.router as upload_router- from src.config.settings import clients, get_index_name, DISABLE_INGEST_WITH_LANGFLOW+ from main import create_app, startup_tasks+ import api.router as upload_router+ from config.settings import clients, get_index_name, DISABLE_INGEST_WITH_LANGFLOW# Ensure a clean index before startupawait clients.initialize()@@ -159,7 +152,7 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_ingesawait startup_tasks(app.state.services)# Ensure index exists for tests (startup_tasks only creates it if DISABLE_INGEST_WITH_LANGFLOW=True)- from src.main import _ensure_opensearch_index+ from main import _ensure_opensearch_indexawait _ensure_opensearch_index()# Verify index is truly empty after startup@@ -242,7 +235,7 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_ingesassert "testing" in text.lower()finally:# Explicitly close global clients to avoid aiohttp warnings- from src.config.settings import clients+ from config.settings import clientstry:await clients.close()except Exception:@@ -349,32 +342,22 @@ async def test_langflow_chat_and_nudges_endpoints():import sysfor mod in [- "src.api.chat","api.chat",- "src.api.nudges","api.nudges",- "src.api.router","api.router",- "src.api.connector_router","api.connector_router",- "src.config.settings","config.settings",- "src.auth_middleware","auth_middleware",- "src.main",+ "main","api",- "src.api","services",- "src.services","services.search_service",- "src.services.search_service","services.chat_service",- "src.services.chat_service",]:sys.modules.pop(mod, None)- from src.main import create_app, startup_tasks- from src.config.settings import clients, LANGFLOW_CHAT_FLOW_ID, NUDGES_FLOW_ID+ from main import create_app, startup_tasks+ from config.settings import clients, LANGFLOW_CHAT_FLOW_ID, NUDGES_FLOW_IDassert LANGFLOW_CHAT_FLOW_ID, "LANGFLOW_CHAT_FLOW_ID must be configured for integration test"assert NUDGES_FLOW_ID, "NUDGES_FLOW_ID must be configured for integration test"@@ -443,7 +426,7 @@ async def test_langflow_chat_and_nudges_endpoints():assert isinstance(nudges_thread_data.get("response"), str)assert nudges_thread_data["response"].strip()finally:- from src.config.settings import clients+ from config.settings import clientstry:await clients.close()@@ -465,22 +448,17 @@ async def test_search_multi_embedding_models(import sysfor mod in [- "src.api.router","api.router",- "src.api.connector_router","api.connector_router",- "src.config.settings","config.settings",- "src.auth_middleware","auth_middleware",- "src.main",+ "main","services.search_service",- "src.services.search_service",]:sys.modules.pop(mod, None)- from src.main import create_app, startup_tasks- from src.config.settings import clients, get_index_name+ from main import create_app, startup_tasks+ from config.settings import clients, get_index_nameawait clients.initialize()try:@@ -492,7 +470,7 @@ async def test_search_multi_embedding_models(app = await create_app()await startup_tasks(app.state.services)- from src.main import _ensure_opensearch_index+ from main import _ensure_opensearch_indexawait _ensure_opensearch_index()@@ -588,7 +566,7 @@ async def test_search_multi_embedding_models(}assert {"text-embedding-3-small", "text-embedding-3-large"} <= result_modelsfinally:- from src.config.settings import clients+ from config.settings import clientstry:await clients.close()@@ -607,26 +585,19 @@ async def test_router_upload_ingest_traditional(tmp_path: Path, disable_langflowimport sysfor mod in [- "src.api.router",- "api.router", # Also clear the non-src path- "src.api.connector_router",+ "api.router","api.connector_router",- "src.config.settings","config.settings",- "src.auth_middleware","auth_middleware",- "src.main",- "api", # Clear the api package itself- "src.api",- "services", # Clear services that import clients- "src.services",+ "main",+ "api",+ "services","services.search_service",- "src.services.search_service",]:sys.modules.pop(mod, None)- from src.main import create_app, startup_tasks- import src.api.router as upload_router- from src.config.settings import clients, get_index_name, DISABLE_INGEST_WITH_LANGFLOW+ from main import create_app, startup_tasks+ import api.router as upload_router+ from config.settings import clients, get_index_name, DISABLE_INGEST_WITH_LANGFLOW# Ensure a clean index before startupawait clients.initialize()@@ -641,7 +612,7 @@ async def test_router_upload_ingest_traditional(tmp_path: Path, disable_langflowawait startup_tasks(app.state.services)# Ensure index exists for tests (startup_tasks only creates it if DISABLE_INGEST_WITH_LANGFLOW=True)- from src.main import _ensure_opensearch_index+ from main import _ensure_opensearch_indexawait _ensure_opensearch_index()# Verify index is truly empty after startup@@ -681,7 +652,7 @@ async def test_router_upload_ingest_traditional(tmp_path: Path, disable_langflowassert isinstance(data.get("task_id"), str)assert data.get("file_count") == 1finally:- from src.config.settings import clients+ from config.settings import clientstry:await clients.close()except Exception:diff --git a/tests/integration/test_startup_ingest.py b/tests/integration/test_startup_ingest.pyindex 4c43cd546..fca0c764f 100644--- a/tests/integration/test_startup_ingest.py+++ b/tests/integration/test_startup_ingest.py@@ -51,16 +51,16 @@ async def test_startup_ingest_creates_task(disable_langflow_ingest: bool):import sysfor mod in [- "src.api.router",- "src.api.connector_router",- "src.config.settings",- "src.auth_middleware",- "src.main",+ "api.router",+ "api.connector_router",+ "config.settings",+ "auth_middleware",+ "main",]:sys.modules.pop(mod, None)- from src.main import create_app, startup_tasks- from src.config.settings import clients, get_index_name+ from main import create_app, startup_tasks+ from config.settings import clients, get_index_name# Ensure a clean index before startupawait clients.initialize()@@ -74,7 +74,7 @@ async def test_startup_ingest_creates_task(disable_langflow_ingest: bool):await startup_tasks(app.state.services)# Ensure index exists for tests (startup_tasks only creates it if DISABLE_INGEST_WITH_LANGFLOW=True)- from src.main import _ensure_opensearch_index+ from main import _ensure_opensearch_indexawait _ensure_opensearch_index()transport = httpx.ASGITransport(app=app)@@ -114,7 +114,7 @@ async def test_startup_ingest_creates_task(disable_langflow_ingest: bool):assert newest.get("total_files") == expected_filesfinally:# Explicitly close global clients to avoid aiohttp warnings- from src.config.settings import clients+ from config.settings import clientstry:await clients.close()except Exception:diff --git a/tests/unit/test_langflow_utils.py b/tests/unit/test_langflow_utils.pyindex e009e0f15..c1fc5c294 100644--- a/tests/unit/test_langflow_utils.py+++ b/tests/unit/test_langflow_utils.py@@ -19,11 +19,8 @@ def _make_response(status_code: int) -> MagicMock:@pytest.fixturedef mock_langflow_client():- """Provide a mocked langflow HTTP client and patch it into settings."""- client = AsyncMock()- with patch("utils.langflow_utils.clients") as mock_clients:- mock_clients.langflow_http_client = client- yield client+ """Provide a mocked langflow HTTP client."""+ return AsyncMock()@pytest.fixture(autouse=True)@@ -41,7 +38,7 @@ async def test_ready_on_first_attempt(mock_langflow_client, no_sleep):"""Returns immediately when health check responds 200 on the first try."""mock_langflow_client.get.return_value = _make_response(200)- await wait_for_langflow(max_retries=3)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=3)mock_langflow_client.get.assert_called_once_with("/health", timeout=5.0)no_sleep.assert_not_called()@@ -58,7 +55,7 @@ async def test_ready_after_non_200_then_200(mock_langflow_client, no_sleep):_make_response(200),]- await wait_for_langflow(max_retries=3)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=3)assert mock_langflow_client.get.call_count == 2assert no_sleep.call_count == 1@@ -72,7 +69,7 @@ async def test_ready_after_exception_then_200(mock_langflow_client, no_sleep):_make_response(200),]- await wait_for_langflow(max_retries=3)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=3)assert mock_langflow_client.get.call_count == 2assert no_sleep.call_count == 1@@ -88,7 +85,7 @@ async def test_ready_after_mixed_failures(mock_langflow_client, no_sleep):_make_response(200),]- await wait_for_langflow(max_retries=5)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=5)assert mock_langflow_client.get.call_count == 4assert no_sleep.call_count == 3@@ -103,7 +100,7 @@ async def test_raises_after_all_retries_exhausted_non_200(mock_langflow_client):mock_langflow_client.get.return_value = _make_response(503)with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=3)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=3)assert mock_langflow_client.get.call_count == 3@@ -114,7 +111,7 @@ async def test_raises_after_all_retries_exhausted_exception(mock_langflow_clientmock_langflow_client.get.side_effect = ConnectionError("refused")with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=2)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=2)assert mock_langflow_client.get.call_count == 2@@ -125,7 +122,7 @@ async def test_single_retry_no_sleep_before_raise(mock_langflow_client, no_sleepmock_langflow_client.get.return_value = _make_response(500)with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=1)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=1)mock_langflow_client.get.assert_called_once()no_sleep.assert_not_called()@@ -142,7 +139,7 @@ async def test_sleep_delay_respects_bounds(mock_langflow_client, no_sleep):max_delay = 4.0with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=5, base_delay=base_delay, max_delay=max_delay)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=5, base_delay=base_delay, max_delay=max_delay)# 4 sleeps for 5 retries (no sleep after the last attempt)assert no_sleep.call_count == 4@@ -159,7 +156,7 @@ async def test_exponential_backoff_increases(mock_langflow_client, no_sleep):with patch("utils.langflow_utils.random.uniform", side_effect=lambda lo, hi: hi):with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=4, base_delay=1.0, max_delay=100.0)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=4, base_delay=1.0, max_delay=100.0)# With jitter pinned to the upper bound, delays should be 1, 2, 4delays = [call.args[0] for call in no_sleep.call_args_list]@@ -173,7 +170,7 @@ async def test_max_delay_cap(mock_langflow_client, no_sleep):with patch("utils.langflow_utils.random.uniform", side_effect=lambda lo, hi: hi):with pytest.raises(LangflowNotReadyError):- await wait_for_langflow(max_retries=6, base_delay=2.0, max_delay=5.0)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=6, base_delay=2.0, max_delay=5.0)delays = [call.args[0] for call in no_sleep.call_args_list]# base_delay * 2^attempt: 2, 4, 5(cap), 5(cap), 5(cap)@@ -188,7 +185,7 @@ async def test_default_parameters(mock_langflow_client, no_sleep):"""Works correctly with default parameter values."""mock_langflow_client.get.return_value = _make_response(200)- await wait_for_langflow()+ await wait_for_langflow(langflow_http_client=mock_langflow_client)mock_langflow_client.get.assert_called_once()@@ -199,4 +196,4 @@ async def test_error_message_content(mock_langflow_client):mock_langflow_client.get.return_value = _make_response(503)with pytest.raises(LangflowNotReadyError, match="Failed to verify"):- await wait_for_langflow(max_retries=1)+ await wait_for_langflow(langflow_http_client=mock_langflow_client, max_retries=1)