Langgraph
- Langgraph is a framework which helps in creating a controlled and reliable agent.
- The steps required to solve a problem will be defined by us as a workflow.
- Basic idea is to create a graph (workflow)
Example scenario
-
Student Assignment Tracker

-
Langgraph is perfect for these kind of scenarios
Langgraph Way of solving above problem
- Langgraph defines graph and refers it as StateGraph
- State is share data across nodes
- node is a computation unit (function)
- edge connects nodes
Lets create our first langgraph
-
A state can be
- TypedDict
- Dataclass
- Pydantic Model
-
create a new folder
hello-langgraph - Initialize with uv and add langgraph pacakge
uv init
uv add langgraph langgraph-cli
- A node in a langraph will have state as input and can have following outputs
- complete state
- partial state
- Create a file called as hello.py with following code
"""Hello langgraph
"""
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
# lets create state
class MyState(TypedDict):
"""This class represents the state of the graph
"""
name: str
friends: list[str]
family: list[str]
# node 1
def find_friends(state: MyState) -> MyState:
"""This function finds friends
"""
state['friends'] = ['Ram', 'Shyam']
return state
# node 2
def find_family(state: MyState) -> MyState:
"""This function finds family
"""
state['family'] = ['sita', 'gita']
return state
# graph which takes state as argument
graph = StateGraph(MyState)
graph.add_node("friends", find_friends)
graph.add_node("family", find_family)
graph.add_edge(START, "friends")
graph.add_edge("friends", "family")
graph.add_edge("family", END)
# compile the graph
compiled_graph = graph.compile()
if __name__ == "__main__":
response = compiled_graph.invoke({
"name": "abc"
})
print(response)
- Now create a file called as langgraph.json in the root of project with following values
{
"dependencies": ["."],
"graphs": {
"hello": "hello:graph"
},
"env": ".env"
}
- If you want to run the graph
uv run hello.py - If you want to view in langgraph studio
langgraph dev -
Ensure virtual enviornment is activated other wise use
uv run langgraph dev -
Refer Here for changes
