The Problem: Slow, Independent Checks
Before a video script goes out, we check it three ways.
The issue
A script must pass three checks before we publish it: toxicity, copyright risk and cultural sensitivity. Each check is a model call.
The first try
A sequential graph: run the three checks one after another.
Why it fails
The checks do not need each other. Running them in a line makes us wait for the first before the second can start, so the waiting time adds up.
The fix
Run all three at the same time, as parallel branches. We wait only as long as the slowest check.
We tested this on your installed LangGraph. Three nodes that each waited one second finished in about one second in total, not three.
Fan-Out and Fan-In: The Shape
DefinitionFan-out means one node leads to several nodes that run at the same time. Fan-in means those nodes come back together.
builder.add_edge(START ,"toxicity_node") builder.add_edge(START ,"copyright_node") builder.add_edge(START ,"culture_node") builder.add_edge("toxicity_node",END) builder.add_edge("copyright_node",END) builder.add_edge("culture_node",END)
Fan-out
Three edges leave
START. Your own comment says it: “START goes to all 3 nodes, they run at the same time”.Fan-in
Three edges arrive at
END. The run finishes only when all three nodes are done.Nothing else changes
The nodes, the state and
compile() work as before. Only the shape of the edges is new.The Three Checks
Each check is a node with its own prompt. Each one returns a score from 0 to 100.
| Node | It checks for | It writes |
|---|---|---|
toxicity_node | profanity, aggression, hate speech | {"toxicity": score} |
copyright_node | plagiarism or a trademark risk | {"copyright_risk": score} |
culture_node | cultural insensitivity | {"cultural_insensitivity": score} |
def to_score(text:str) -> int: """LLM returns text like '85' or '85.' -> pull out the number so state really holds an int""" match = re.search(r"\d+", text) return int(match.group()) if match else -1 def toxicity_node(state :AnlayzerState) -> dict: chain = toxicity_prompt | model | parser score = chain.invoke({"raw_text":state["raw_text"]}) return {"safety_scores":{"toxicity":to_score(score)}}
From your
01_parallel_reducers.py. Each prompt asks for a plain number. to_score pulls the number out of the reply, so the state really holds an int. The other two nodes are the same, with another prompt and another key.Every node writes to the same key,
safety_scores, but with a different small dictionary inside. That is the problem for the next lesson tab.