diff --git a/src/backend/base/langflow/graph/graph/utils.py b/src/backend/base/langflow/graph/graph/utils.py index 4cea54a23..c1e6737d4 100644 --- a/src/backend/base/langflow/graph/graph/utils.py +++ b/src/backend/base/langflow/graph/graph/utils.py @@ -743,13 +743,15 @@ def sort_chat_inputs_first( if not chat_input: return vertices_layers - # If chat input already in first layer, just move it to index 0 if chat_input_layer_idx == 0: - first_layer = vertices_layers[0] - first_layer.remove(chat_input) - first_layer.insert(0, chat_input) - return vertices_layers + # If chat input is alone in first layer, keep as-is + if len(vertices_layers[0]) == 1: + return vertices_layers + + # Otherwise move chat input to its own layer at the start + vertices_layers[0].remove(chat_input) + return [[chat_input], *vertices_layers] # Otherwise create new layers with chat input first result_layers = [] @@ -865,7 +867,7 @@ def get_sorted_vertices( # Sort chat inputs first and sort each layer by dependencies all_layers = [first_layer, *remaining_layers] - if get_vertex_predecessors is not None: + if get_vertex_predecessors is not None and start_component_id is None: all_layers = sort_chat_inputs_first(all_layers, get_vertex_predecessors) if get_vertex_successors is not None: all_layers = sort_layer_by_dependency(all_layers, get_vertex_successors) diff --git a/src/backend/tests/unit/graph/graph/test_utils.py b/src/backend/tests/unit/graph/graph/test_utils.py index 9293dfe74..359d5ded3 100644 --- a/src/backend/tests/unit/graph/graph/test_utils.py +++ b/src/backend/tests/unit/graph/graph/test_utils.py @@ -483,10 +483,11 @@ def test_chat_inputs_at_start(): return [] result = utils.sort_chat_inputs_first(vertices_layers, get_vertex_predecessors) - assert len(result) == 3 # [chat_input] + original 3 layers - assert result[0] == ["ChatInput1", "B"] - assert result[1] == ["C"] # Original second layer - assert result[2] == ["D"] # Original third layer + assert len(result) == 4 # [chat_input] + original 3 layers + assert result[0] == ["ChatInput1"] # First layer contains only ChatInput1 + assert result[1] == ["B"] # Second layer contains B + assert result[2] == ["C"] # Original second layer + assert result[3] == ["D"] # Original third layer # Test that multiple chat inputs raise an error vertices_layers_multiple = [["ChatInput1", "B"], ["ChatInput2", "C"], ["D"]] diff --git a/src/backend/tests/unit/test_chat_endpoint.py b/src/backend/tests/unit/test_chat_endpoint.py index b9436c087..3a443358b 100644 --- a/src/backend/tests/unit/test_chat_endpoint.py +++ b/src/backend/tests/unit/test_chat_endpoint.py @@ -73,7 +73,7 @@ async def consume_and_assert_stream(r): assert parsed["event"] == "vertices_sorted" ids = parsed["data"]["ids"] ids.sort() - assert ids == ["ChatInput-CIGht", "Memory-amN4Z"] + assert ids == ["ChatInput-CIGht"] to_run = parsed["data"]["to_run"] to_run.sort() diff --git a/src/backend/tests/unit/test_endpoints.py b/src/backend/tests/unit/test_endpoints.py index 7e2e9dbc0..99073f60b 100644 --- a/src/backend/tests/unit/test_endpoints.py +++ b/src/backend/tests/unit/test_endpoints.py @@ -257,7 +257,7 @@ async def test_get_vertices(client, added_flow_webhook_test, logged_in_headers): # The important part is before the - (ConversationBufferMemory, PromptTemplate, ChatOpenAI, LLMChain) ids = [_id.split("-")[0] for _id in response.json()["ids"]] - assert set(ids) == {"ChatInput", "Webhook"} + assert set(ids) == {"ChatInput"} async def test_build_vertex_invalid_flow_id(client, logged_in_headers):