Replies: 4 comments 1 reply
-
I have also attempted to use I am unable to set I believe the root cause of this problem is that, in a real workflow, when dealing with the subsequent nodes of conditional nodes, it is not feasible to execute based solely on simple "AND" or "OR" conditions such as "all preceding nodes are satisfied" or "any one of the preceding conditions is met." |
Beta Was this translation helpful? Give feedback.
-
If you think the previous example is somewhat extreme, consider this example: when a node receives inputs from both conditional branches and regular nodes, according to the original LangGraph, this generally causes the node to execute twice. import operator
from typing import Annotated, Literal, Any
from typing_extensions import TypedDict
from langgraph.graph import END, START, StateGraph
def reduce_fanouts(left, right):
if left is None:
left = []
if not right:
# Overwrite
return []
return left + right
class State(TypedDict):
# The operator.add reducer fn makes this append-only
aggregate: Annotated[list, operator.add]
which: str
class ReturnNodeValue:
def __init__(self, node_secret: str):
self._value = node_secret
def __call__(self, state: State) -> Any:
print(f"Adding {self._value} to {state['aggregate']}")
return {"aggregate": [self._value]}
builder = StateGraph(State)
builder.add_node("a", ReturnNodeValue("I'm A"))
builder.add_edge(START, "a")
builder.add_node("b", ReturnNodeValue("I'm B"))
builder.add_node("c", ReturnNodeValue("I'm C"))
builder.add_node("e", ReturnNodeValue("I'm E"))
def route_b_or_e(state: State) -> Literal["b", "e"]:
if state["which"] == "b":
return ["b"]
return ["e"]
intermediates = ["b", "e"]
builder.add_conditional_edges(
"a",
route_b_or_e,
)
builder.add_edge(START, "a")
builder.add_edge(START, "c")
builder.add_edge("c", "e")
builder.add_edge("b","e")
builder.add_edge("e", END)
graph = builder.compile()
graph.invoke({"aggregate": [], "which": "b", "fanout_values": []}) originl LangGraphAdding I'm A to []
Adding I'm C to []
Adding I'm B to ["I'm A", "I'm C"]
Adding I'm E to ["I'm A", "I'm C"]
Adding I'm E to ["I'm A", "I'm C", "I'm B", "I'm E"] use workflow modeAdding I'm A to []
Adding I'm C to []
Adding I'm B to ["I'm A", "I'm C"]
Adding I'm E to ["I'm A", "I'm C", "I'm B"] |
Beta Was this translation helpful? Give feedback.
-
@nfcampos I sincerely apologize for disturbing you amidst your busy schedule. Could you please take some time to review my idea and the associated code to determine if they are reasonable? I will actively respond based on your feedback ! |
Beta Was this translation helpful? Give feedback.
-
Hi, I commented on the PR too, this would change langgraph into a DAG framework, we started langgraph precisely to support graphs either cycles that are needed for LLM applications |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
Building upon the existing LangGraph framework, I developed and implemented a complete workflow in my project. I discovered that using
add_conditional_edges
in the workflow had certain flaws in executing subsequent nodes. To address this issue, I temporarily applied a monkey patch in my project to fix the problems related to conditional node execution within the workflow. Additionally, I designed an Analyzer to maintain the directed graph of execution paths. This improvement allows users to enable the new execution mode by settingworkflow_mode=True
during compilation. If not set or set toFalse
, the original execution mode is used by default. I have submitted a PR and hope that this approach can be adopted into LangGraph.Problem Description
Prerequisite: In the workflow, each node should execute only once unless special handling logic is set.
When handling conditional branches with LangGraph, the following execution issues were discovered:
Set up a selector node (A) with two executable branches, Type 1 and Type 2:
When the results of both types point to Node E, LangGraph's parallel processing mechanism causes Node E to execute only once.
Attempts in Existing LangGraph:
Since each node is dynamically generated, specific branch paths cannot be determined before workflow execution. Therefore, it is necessary to dynamically choose the appropriate path during execution.
Solution
Added an Analyzer to LangGraph's source code to achieve the aforementioned requirements. Its main functionalities include:
Execution in this Case:
Through these improvements, each node executes only once within the workflow. The trigger conditions for subsequent nodes are dynamically adjusted based on the actual execution paths, catering to different types of text processing requirements.
Usage
To use the improved execution mode in LangGraph, set
workflow_mode=True
incompile
. The specific steps are as follows:Set the Option:
Add
workflow_mode=True
in the compilation configuration to enable the dynamic workflow mode.Note: When
workflow_mode=True
, either thepath_map
parameter or aLiteral
return is required. Only one of them is needed. Thepath_map
orLiteral
must cover all possible execution paths.Default Behavior:
If not set or if
workflow_mode
is set toFalse
, the original execution mode of LangGraph is used, maintaining backward compatibility.Here is my PR link
Beta Was this translation helpful? Give feedback.
All reactions