You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.
Example Code
importoperatorfromlanggraph.graphimportStateGraphfromtypingimportAnnotatedfromtypingimportTypedDictclassAgentState(TypedDict):
input: strhistory: Annotated[list[dict], operator.add]
defgetapp():
defstart(state):
print("run: START")
return {"history": [{}]}
defend(state):
print("run: END")
# 返回代理的结果return {"history": [{}]}
defrun_agent(name):
defrun(state):
print("run: "+name)
return {"history": [{}]}
returnrundefconditional(state):
# 返回代理的结果return ["agent2", "agent3"]
workflow=StateGraph(AgentState)
# scenario_one(conditional, end, run_agent, start, workflow)scenario_two(conditional, end, run_agent, start, workflow)
app=workflow.compile()
returnappdefscenario_one(conditional, end, run_agent, start, workflow):
""" Scenario 1 graph: start -> agent1 -> agent2 -> agen4 -> end -> agent3 Branch one is agent2. Branch tow is agent2. The number of nodes in branch one is equal to the number of nodes in branch two. """workflow.add_node("start", start)
workflow.add_node("end", end)
workflow.add_node("agent1", run_agent("agen1"))
workflow.add_node("agent2", run_agent("agen2"))
workflow.add_node("agent3", run_agent("agen3"))
workflow.add_node("agent4", run_agent("agen4"))
workflow.add_edge("start", "agent1")
workflow.add_conditional_edges("agent1", conditional, {"agent2": "agent2", "agent3": "agent3"})
workflow.add_edge("agent2", "agent4")
workflow.add_edge("agent3", "agent4")
workflow.add_edge("agent3", "agent4")
workflow.add_edge("agent4", "end")
workflow.set_entry_point("start")
workflow.set_finish_point("end")
defscenario_two(conditional, end, run_agent, start, workflow):
""" Scenario 1 graph start -> agent1 -> agent2 -> agen4 -> end -> agent3 -> agent3.5 Branch one is agent2. Branch tow is agent3 and agent3.5. The number of nodes in branch two is greater than that in branch one. """workflow.add_node("start", start)
workflow.add_node("end", end)
workflow.add_node("agent1", run_agent("agen1"))
workflow.add_node("agent2", run_agent("agen2"))
workflow.add_node("agent3", run_agent("agen3"))
workflow.add_node("agent3.5", run_agent("agen3.5"))
workflow.add_node("agent4", run_agent("agen4"))
workflow.add_edge("start", "agent1")
workflow.add_conditional_edges("agent1", conditional, {"agent2": "agent2", "agent3": "agent3"})
workflow.add_edge("agent2", "agent4")
workflow.add_edge("agent3", "agent4")
workflow.add_edge("agent3", "agent3.5")
workflow.add_edge("agent3.5", "agent4")
workflow.add_edge("agent4", "end")
workflow.set_entry_point("start")
workflow.set_finish_point("end")
if__name__=='__main__':
getapp().invoke(input={"input": ""}, config={"recursion_limit": 10})
Error Message and Stack Trace (if applicable)
Scenario 1:
run: START
run:agen1
run:agen2
run:agen3
run:agen4
run: END
Scenario 2:
run: START
run:agen1
run:agen2
run:agen3
run:agen3.5
run:agen4
run: END
run:agen4
run: END
Description
In the scenario of parallel branching and merging, when the number of nodes in multiple branches is the same, the merging node only runs once, which is in line with the Wait for All Strategy. However, when the number of nodes in the branches is not the same, the merging node and subsequent processes will run multiple times, which is inconsistent with the above strategy and barely belongs to the Parallel Merge Strategy.
System Info
LangGraph Version Description:
The text was updated successfully, but these errors were encountered:
This operates completely normally.
The method behind langgraph can be simply thought of as a BFS (Breadth-First Search) approach.
Specifically, it runs in a way similar to the method shown in the image below.
I'm not sure what result you were expecting.
If you want all the nodes pointing to each other on the mermaid diagram (agent4) to wait for one another, you just need to modify it as shown below.
If you want to better observe the flow of the graph, use langgraph-platform to track its movement.
You can easily insert a sleep function in the nodes to observe the flow direction. This will help you understand how the graph operates more intuitively.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
In the scenario of parallel branching and merging, when the number of nodes in multiple branches is the same, the merging node only runs once, which is in line with the Wait for All Strategy. However, when the number of nodes in the branches is not the same, the merging node and subsequent processes will run multiple times, which is inconsistent with the above strategy and barely belongs to the Parallel Merge Strategy.
System Info
LangGraph Version Description:
The text was updated successfully, but these errors were encountered: