OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
fix: target agent component when updating chat flow system prompt (#2001)
Zephyrus915537b373c0
diff --git a/src/api/settings/langflow_sync.py b/src/api/settings/langflow_sync.pyindex 605bc3482..e1ce40951 100644--- a/src/api/settings/langflow_sync.py+++ b/src/api/settings/langflow_sync.py@@ -238,8 +238,7 @@ async def _update_langflow_model_values(async def _update_langflow_system_prompt(config, flows_service):"""Update system prompt in chat flow"""try:- llm_provider = config.agent.llm_provider.lower()- await flows_service.update_chat_flow_system_prompt(config.agent.system_prompt, llm_provider)+ await flows_service.update_chat_flow_system_prompt(config.agent.system_prompt)logger.info("Successfully updated chat flow system prompt")except Exception as e:logger.error(f"Failed to update chat flow system prompt: {str(e)}")diff --git a/src/services/flows_service.py b/src/services/flows_service.pyindex 8b5f5b789..89c5b56fa 100644--- a/src/services/flows_service.py+++ b/src/services/flows_service.py@@ -705,16 +705,16 @@ class FlowsService:LANGFLOW_CHAT_FLOW_ID, "model_name", model_name, node_display_name=target_llm_id)- async def update_chat_flow_system_prompt(self, system_prompt: str, provider: str):+ async def update_chat_flow_system_prompt(self, system_prompt: str):"""Helper function to update the system prompt in the chat flow"""if not LANGFLOW_CHAT_FLOW_ID:raise ValueError("LANGFLOW_CHAT_FLOW_ID is not configured")- # Determine target component IDs based on provider- target_agent_id = self._get_provider_component_ids(provider)[1]-await self._update_flow_field(- LANGFLOW_CHAT_FLOW_ID, "system_prompt", system_prompt, node_display_name=target_agent_id+ LANGFLOW_CHAT_FLOW_ID,+ "system_prompt",+ system_prompt,+ node_display_name=AGENT_COMPONENT_DISPLAY_NAME,)async def update_flow_docling_preset(self, preset: str, preset_config: dict):diff --git a/tests/unit/test_langflow_agent_system_prompt.py b/tests/unit/test_langflow_agent_system_prompt.pynew file mode 100644index 000000000..8d01c50cb--- /dev/null+++ b/tests/unit/test_langflow_agent_system_prompt.py@@ -0,0 +1,62 @@+import json+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock++import pytest++_REPO_ROOT = Path(__file__).parent.parent.parent+++def _load_flow(flow_path: str) -> dict:+ """Load a Langflow flow JSON file resolved relative to the repository root."""+ return json.loads((_REPO_ROOT / flow_path).read_text(encoding="utf-8"))+++def _find_node_by_display_name(flow: dict, display_name: str):+ """Return the first flow node whose display_name matches, or None."""+ return next(+ (+ node+ for node in flow["data"]["nodes"]+ if node.get("data", {}).get("node", {}).get("display_name") == display_name+ ),+ None,+ )+++def test_agent_flow_has_agent_node_with_system_prompt():+ """The Agent node must exist in openrag_agent.json and expose a system_prompt field."""+ flow = _load_flow("flows/openrag_agent.json")+ agent_node = _find_node_by_display_name(flow, "Agent")++ assert agent_node is not None, "No node with display_name='Agent' found in openrag_agent.json"+ template = agent_node.get("data", {}).get("node", {}).get("template", {})+ assert "system_prompt" in template, (+ "Agent node does not have a system_prompt field in its template"+ )+++async def test_update_chat_flow_system_prompt_updates_agent_node(monkeypatch):+ """update_chat_flow_system_prompt must write the new value into the Agent node's system_prompt field."""+ from services.flows_service import FlowsService++ get_response = MagicMock(status_code=200)+ get_response.json.return_value = _load_flow("flows/openrag_agent.json")+ patch_response = MagicMock(status_code=200)++ request = AsyncMock(side_effect=[get_response, patch_response])+ monkeypatch.setattr("services.flows_service.LANGFLOW_CHAT_FLOW_ID", "test-flow-id")+ monkeypatch.setattr("services.flows_service.clients.langflow_request", request)++ await FlowsService().update_chat_flow_system_prompt(+ "updated system prompt for testing purposes"+ )++ sent_flow = request.call_args_list[1].kwargs["json"]+ agent_node = _find_node_by_display_name(sent_flow, "Agent")+ assert agent_node is not None, "Agent node missing from PATCHed flow data"+ assert (+ agent_node["data"]["node"]["template"]["system_prompt"]["value"]+ == "updated system prompt for testing purposes"+ )