Generative AIby Dhruv Narang

Putting the Graph Together

You already know these lines from the Building a Graph lesson. Here is the full pipeline.

graph = StateGraph(pipelinestate)

graph.add_node("editor", editor_node)
graph.add_node("scriptwriter",scriptwriter_node)
graph.add_node("translator",tranlator_node)


graph.add_edge(START,"editor")
graph.add_edge("editor","scriptwriter")
graph.add_edge("scriptwriter","translator")
graph.add_edge("translator",END)
STARTeditorscriptwritertranslatorEND
Three nodes, four edges. START to the editor, then each node to the next, and the translator to END. The order of the edges is the order of the work.

Run It and Read the Result

We invoke the graph with only the raw text.

app = graph.compile()
result = app.invoke({
    "raw_input" :"AI agents are the future of tech. They can think, plan, and act on their own. LangGraph helps you build these agents with proper control and memory."
})

print(result['final_output'])
StepState keyExample text
Inputraw_inputAI agents are the future of tech. They can think, plan, and act on their own. LangGraph helps you build these agents with proper control and memory.
Editoredited_textAI agents are the future of technology. They can think, plan and act on their own. LangGraph helps you build these agents with proper control and memory.
Scriptwriterscript_textImagine software that thinks, plans and acts all by itself. That is an AI agent! And LangGraph gives you the control and the memory to build one.
Translatorfinal_outputSocho, ek software jo khud sochta hai, plan banata hai aur kaam bhi karta hai. Wahi hai AI agent! Aur LangGraph tumhe control aur memory deta hai, taaki tum apna agent bana sako.
An example, written by hand to show the idea. It is not real model output. Your model will word it differently.
invoke returns the whole final state. It holds all four keys, so you can print any middle step, such as result['edited_text'], to see what a node did. print(result['final_output']) shows only the last one.

Why a Pipeline Beats One Big Prompt

It costs more calls, but it gives you more control.

One big promptSequential graph
Qualityone model call does three jobs, so each job gets less careeach node does one small job well
Finding a problemyou only see the final textevery step's result is in the state, so you see where it went wrong
Changing one jobyou rewrite the whole promptyou change one node, such as a new role for the translator
The costone model callthree model calls: more time and money
Use a sequential workflow when the steps always run in the same order, for example clean up, rewrite, then format. If a step may need a retry, or the path depends on the result, you need a loop or a conditional edge instead.

Quick Revision

The whole lesson on one page.

Idea
An assembly line
Small jobs, one after another. Each node reads what the one before wrote.
In code
Nodes, edges, state
Three node functions, four edges from START to END, and a state with one key for each result.
Trade-off
More calls, more control
Three calls cost more than one, but each step is easy to read, test and change.
LangGraph docsWorkflows and agents
Code
02_sequential.py02_sequential.py · 95 linesShow code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os

from dotenv import load_dotenv

load_dotenv()

from typing import TypedDict
from rich import print
"""State: the shared data that every node reads and updates"""
class pipelinestate(TypedDict):
    raw_input : str
    edited_text : str
    script_text :str
    final_output:str


from langchain.chat_models import init_chat_model


model = init_chat_model("mistralai:open-mistral-nemo")

"""Node = a normal Python function: takes the state, returns only the keys it changes"""
def editor_node(state:pipelinestate):
    """Stage 1 cleans up the grammer and removes types mistake and refine the tone"""
    
    prompt = (
        f"""
        You are an expert copyeditor. Clean up the following raw text. 
        Fix any grammatical errors, spelling mistakes, and smooth out the transition flow 
        while keeping the core message intact. Return only the edited text.\n\n"
        Text:\n{state['raw_input']}
        """
    )
    response = model.invoke(prompt)
    
    return {"edited_text":response.content.strip()}

def scriptwriter_node(state:pipelinestate):
    """Stage 2: Formats the clean text into an engaging video script style."""
    
    prompt = (
        "You are a charismatic YouTube content creator. Take this edited text and transform "
        "it into a highly engaging, punchy, conversational video script hook. Make it sound "
        "like a real person speaking passionately. Return only the script content.\n\n"
        f"Edited Text:\n{state['edited_text']}"
    )
    
    response = model.invoke(prompt)
    return {"script_text": response.content.strip()}
        
        
def tranlator_node(state:pipelinestate):
    """Stage 3: Translates the script into natural flowing Hinglish."""
        
    prompt = (
        "You are an expert content localizer for the Indian market. Take the following script "
        "and convert it into natural, flowing 'Hinglish'. Do not simply translate it sentence-by-sentence "
        "or repeat information. Alternating comfortably between Hindi and English phrases just like "
        "an intellectual tech educator would speak naturally on a live stream. Keep the energy high! "
        "Return only the final Hinglish text.\n\n"
        f"Script:\n{state['script_text']}"
    )
    
    response = model.invoke(prompt)
    return {"final_output": response.content.strip()}




from langgraph.graph import StateGraph ,START , END


"""Build the graph: add nodes (steps), then edges (the order)"""
graph = StateGraph(pipelinestate)

graph.add_node("editor", editor_node)
graph.add_node("scriptwriter",scriptwriter_node)
graph.add_node("translator",tranlator_node)


graph.add_edge(START,"editor")
graph.add_edge("editor","scriptwriter")
graph.add_edge("scriptwriter","translator")
graph.add_edge("translator",END)


"""compile() turns the graph into a Runnable, so we can .invoke() it"""
app = graph.compile()


result = app.invoke({
    "raw_input" :"AI agents are the future of tech. They can think, plan, and act on their own. LangGraph helps you build these agents with proper control and memory."
})

print(result['final_output'])