Branch Creation for Parallel Node Execution

Open in ColabOpen in GitHub

Overview

Parallel execution of nodes is essential for improving the overall performance of graph-based workflows. LangGraph provides native support for parallel node execution, significantly enhancing the efficiency of workflows built with this framework.

This parallelization is achieved using fan-out and fan-in mechanisms, utilizing both standard edges and conditional_edges.

branching-graph

Table of Contents

References


Environment Setup

Setting up your environment is the first step. See the Environment Setup guide for more details.

[Note]

The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials. Check out the langchain-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Parallel Node Fan-out and Fan-in

Fan-out / Fan-in

In parallel processing, fan-out and fan-in describe the processes of dividing and consolidating tasks.

  • Fan-out (Expansion): A large task is divided into smaller, more manageable tasks. For example, when making a pizza, the dough, sauce, and cheese can be prepared independently. Dividing tasks to process them simultaneously is fan-out.

  • Fan-in (Consolidation): The divided smaller tasks are brought together to complete the overall task. Just like assembling the prepared ingredients to create a finished pizza, fan-in collects the results of parallel tasks to finalize the process.

In essence, fan-out distributes tasks, and fan-in gathers the results to produce the final output.

This example illustrates a fan-out from Node A to Node B and Node C, followed by a fan-in to Node D.

In the State, the reducer(add) operator is specified. This ensures that instead of simply overwriting existing values for a specific key in the State, the values are combined or accumulated. For lists, this means appending the new list to the existing one.

LangGraph uses the Annotated type to specify reducer functions for specific keys in the State. This approach allows attaching a reducer function (e.g., add) to the type without changing the original type (e.g., list) while maintaining compatibility with type checking.

Let's visualize the graph.

png

You can observe that the values added by each node are accumulated through the reducer.

Handling Exceptions during Parallel Processing

LangGraph executes nodes within a "superstep". This means that even if parallel branches are executed simultaneously, the entire superstep is processed in a transactional manner.

Superstep: A complete processing step involving multiple nodes.

As a result, if an exception occurs in any of the branches, no updates are applied to the state (the entire superstep is rolled back).

branching-graph

For tasks prone to errors (e.g., handling unreliable API calls), LangGraph offers two solutions:

  1. You can write standard Python code within nodes to catch and handle exceptions directly.

  2. Set up a retry_policy to instruct the graph to retry nodes that encounter specific types of exceptions. Only the failed branches are retried, so you don’t need to worry about unnecessary reprocessing.

These features enable complete control over parallel execution and exception handling.

Fan-out and Fan-in of Parallel Nodes with Additional Steps

The previous example demonstrated how to perform fan-out and fan-in when each path consists of a single step. But what happens when a path contains multiple steps?

Let's visualize the graph.

png

Conditional Branching

When the fan-out is non-deterministic, you can directly use add_conditional_edges.

If there is a known "sink" node to connect to after the conditional branching, you can specify then="node_name_to_execute" when creating the conditional edge.

Here is a reference code snippet. When using the then syntax, you can add then="e" and omit adding explicit edge connections.

Let's visualize the graph.

png

Sorting Based on Reliability of Fan-out Values

Nodes spread out in parallel are executed as part of a single "super-step". Updates from each super-step are sequentially applied to the state only after the super-step is completed.

If a consistent, predefined order of updates is required during a parallel super-step, the output values can be recorded in a separate field of the state with an identifying key. Then, use standard edges from each fan-out node to the convergence point, where a "sink" node combines these outputs.

For example, consider a scenario where you want to sort the outputs of parallel steps based on their "reliability".

Let's visualize the graph.

png

The results from executing nodes in parallel are then sorted based on their reliability.

Reference

  • b: reliability = 0.1

  • c: reliability = 0.9

  • d: reliability = 0.5

Last updated