Multiple States, 1 Conditional Edge, but 2 Results... #2197
-
Hi there! I was learning how to work with graphs in LangGraph, and I stumbled across a perplexing issue while working with conditional edges. Graph SummaryI have 3 nodes. The first node, States SummaryI have 3 different states. Both The Issue
Codefrom operator import add
from typing import TypedDict, Annotated
from langgraph.graph import StateGraph, START, END
class PersonState(TypedDict):
name: str
money: Annotated[int, add]
class PurchasesState(TypedDict):
purchases: Annotated[list[str], add]
total_cost: Annotated[int, add]
class OverallState(PersonState, PurchasesState):
pass
# node_1 operation.
def pay_salary(state: PersonState) -> OverallState:
print(f"Node 1: {state}")
return {"money": 1000}
# node_2 operation.
def subtract_expenses(state: PurchasesState) -> OverallState:
print(f"Node 2: {state}")
return {'money': -100, "purchases": ["new iphone"], 'total_cost': 100}
# node_3 operation.
def add_assistance(state: OverallState) -> OverallState:
print(f"Node 3: {state}")
return {"money": 100}
# Routing Function.
def check_if_poor(state: OverallState):
print(f"Routing function: {state}")
# if poor, add asssitance
if (state['money'] < 100): return "node_3"
else: return END
graph = StateGraph(OverallState, input = PersonState)
graph.add_edge(START, 'node_1')
graph.add_node('node_1', pay_salary)
graph.add_edge('node_1', 'node_2')
graph.add_node('node_2', subtract_expenses)
graph.add_conditional_edges('node_2', check_if_poor)
graph.add_node('node_3', add_assistance)
graph.add_edge("node_3", END)
workflow = graph.compile()
output_dict = workflow.invoke({"name": "Ahmed"})
print(f"Final output: {output_dict}") Code Output
The output line that I can't understand
How does The line that is causing the weird output (after debugging)def subtract_expenses(state: PurchasesState) -> OverallState: How is it causing the issue?When I change the inputted state from
What's my question?Why does changing the type of the inputted state in the function Thanks in advance! P.S. Please don't mind how inefficient the graph setup is for the problem I'm trying to solve. I had simplified my original code a good bit of my code to make it easier to pinpoint what the issue was. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Not sure if you found an answer in the past month, but returning dictionaries in a node will overwrite that field in the state. So, by returning if you are wanting to add these values to the state object, the correct syntax should be:
relevant question (see response): #1788 |
Beta Was this translation helpful? Give feedback.
-
hi, running your code give me a different output from what you reported, probably it was a bug that got fixed meantime or you fixed the code already. here is the output :
|
Beta Was this translation helpful? Give feedback.
Sorry for the late response! This bug was detailed in Issue #2504 and later fixed for LangGraph v. 0.36. Thanks for reminding me to update this discussion!