Open ahmed33033 opened 4 days ago
@ahmed33033 great question -- that's because the state type annotation (input schema) in the node function acts as a "filter" -- currently you use PurchasesState
in subtract_expenses
, so the node ignores the money
value (since PurchasesState
doesn't have that key). You need to change it to PersonState
, as that is what you're outputting from the previous node (pay_salary
). that's also why OverallState
works too, as it also includes money
key.
def subtract_expenses(state: PersonState) -> OverallState:
...
hope this helps!
@ahmed33033 great question -- that's because the state type annotation (input schema) in the node function acts as a "filter" -- currently you use
PurchasesState
insubtract_expenses
, so the node ignores themoney
value (sincePurchasesState
doesn't have that key). You need to change it toPersonState
, as that is what you're outputting from the previous node (pay_salary
). that's also whyOverallState
works too, as it also includesmoney
key.def subtract_expenses(state: PersonState) -> OverallState: ...
hope this helps!
Thank you so much for the quick reply!
Sorry, I think I didn't highlight the issue clearly.
You're absolutely right about the input state acting as a filer. But, the issue is mainly centered around the routing function check_if_poor
, and not subtract_expenses
. The issue is that the input schema of subtract_expenses
affects the state fields that the routing function check_if_poor
receives. Essentially, the graph flow should be as follows:
1 - Node pay_salary
updates money
to 1000.
2 - Node subtract_expenses
issues another update to money
of -100. This means that after subtract_expenses
executes, money
should have a value of 900.
3 - Routing function check_if_poor
checks the value of money
. It's supposed to be 900. But, as you can see from the first console output, the routing function check_if_poor
somehow receives a money
value of -100. This is the inaccuracy.
The issue is somehow "fixed" by changing the input state of subtract_expenses
. But, the input state of subtract_expenses
should not affect the state values that the routing function check_if_poor
later receives. This is because langgraph nodes can "write to any state channel in the graph state" (langgraph glossary).
Hm ya this does feel surprising. The -100 is overwriting the state wholesale rather than being passed to the reducer (1000 - 900), despite the reducer of add
being set in both states (overall state via inheritence) - thanks for reporting, Ahmed! We'll investigate
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
(Derived from Discussion Post #2197 , please see discussion post for detailed description)
The Console Output line that is inaccurate:
How does
money
have a value of -100? In the methodpay_salary
, themoney
attribute is set to 1000. Then, in thesubtract_expenses
method, I subtract 100 from it. So, themoney
attribute should have a value of 900, not -100.The line that is causing the weird output (after debugging)
How is it causing the issue?
When I change the inputted state from
PurchasesState
toOverallState
, it works as expected, i.e. the output printed in the routing functioncheck_if_poor
is:What's my question?
Why does changing the type of the inputted state in the function
subtract_expenses
lead me to receive different values for themoney
attribute in the routing functioncheck_if_poor
?Thanks in advance!
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies