-
After watching the Intro to LangGraph video I thought I understood these concepts, but after building some code I'm confused. Any assistance to clarify would be helpful. Here's my situation: I have two fields in my state:
I have two functions:
These are called in parallel:
In my first attempt I coded each of the function (similar but with a different key for the other function)
When I run it I got an error:
Since the keys were different of what I modified I thought I was OK, but I guess not. So next I updated the definition to:
Then changed the return to:
This caused the state to actually have duplicates of each element in the array. The only combination that worked was defining without the annotation and return just changed item
This doesn't really make sense to me from what I read/heard in the video. Could someone help explain? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Could you actually provide a full example? It's a bit hard to follow what exactly you're trying to return from each node running in parallel (assuming In general, this issue should only arise if both of the nodes running at the same superstep are trying to write an update to the same state key. In this case, LangGraph doesn't know how to apply the updates unless you provide a way for LangGraph to combine the values. TO do so, you can specify a reducer function in the state schema annotation: it tells LangGraph that instead of overwriting the values you need to accumulate them. So state value for the key after the nodes run will be the result of calling If you're returning the same values from both nodes and have a reducer with operator.add, it's not surprising that you would see a duplicated list. If you need to only keep distinct values, you would probably need to use a set instead of a list. I would recommend checking out these document pages:
Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hi, I appreciate your comment. All of what you said makes sense but doesn't seem to line up with what I'm seeing If you check these two files: You will see in the _agent file functions If you look at Any guidance is really appreciated. |
Beta Was this translation helpful? Give feedback.
first off, you shouldn't be manually mutating state inside node functions and return mutated state, this is an antipattern. you don't also need to return the full updated state from the node, only the relevant updates to the state. state updates should only be handled via outputs of node functions (or manual calls to
graph.update_state()
). e.g. this partshould always be done as
for your specific issue, if BOTH
get_subreddits
andget_marketing_suggestions
returnmarketing_suggestions
key, then you need a reducer for that key. If both of them return the same list ofm…