Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The order of state updates is sorted globally, not within each step (Module 4 - Lesson 1: Parallelization) #42

Open
labdmitriy opened this issue Oct 11, 2024 · 0 comments

Comments

@labdmitriy
Copy link

Hi @rlancemartin,

In the section "Setting the order of state updates" you told that we want to control the ordering within each step, and you use sorting_reducer for it. But as I understand this reducer will sort all the steps in the graph globally. The easy way to check it is to rename node a to node z and re-execute the same code:

def sorting_reducer(left, right):
    """ Combines and sorts the values in a list"""
    if not isinstance(left, list):
        left = [left]

    if not isinstance(right, list):
        right = [right]
    
    return sorted(left + right, reverse=False)

class State(TypedDict):
    # sorting_reducer will sort the values in state
    state: Annotated[list, sorting_reducer]

# Add nodes
builder = StateGraph(State)

# Initialize each node with node_secret 
builder.add_node("z", ReturnNodeValue("I'm Z"))
builder.add_node("b", ReturnNodeValue("I'm B"))
builder.add_node("b2", ReturnNodeValue("I'm B2"))
builder.add_node("c", ReturnNodeValue("I'm C"))
builder.add_node("d", ReturnNodeValue("I'm D"))

# Flow
builder.add_edge(START, "z")
builder.add_edge("z", "b")
builder.add_edge("z", "c")
builder.add_edge("b", "b2")
builder.add_edge(["b2", "c"], "d")
builder.add_edge("d", END)
graph = builder.compile()

graph.invoke({"state": []})

Then you will see that after each super-step we will get the list of all steps sorted globally:

Adding I'm Z to []
Adding I'm B to ["I'm Z"]
Adding I'm C to ["I'm Z"]
Adding I'm B2 to ["I'm B", "I'm C", "I'm Z"]
Adding I'm D to ["I'm B", "I'm B2", "I'm C", "I'm Z"]
{'state': ["I'm B", "I'm B2", "I'm C", "I'm D", "I'm Z"]}

Maybe the more correct way to do what you describe as the goal of this section is to implement the method similar to the example from LangGraph documentation, where you use additional sink node after super-step with multiple sub-steps and change the order of sub-steps in this sink node.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant