How to install LangGraph
Quick answer
Install
langgraph via pip install langgraph to use stateful graph-based AI agents in Python. This package enables building and running AI workflows with nodes and edges using a simple API.PREREQUISITES
Python 3.8+pip installed
Setup
Install langgraph using pip. Ensure you have Python 3.8 or higher installed. No API key is required for the package itself.
pip install langgraph output
Collecting langgraph Downloading langgraph-<version>-py3-none-any.whl (xx kB) Installing collected packages: langgraph Successfully installed langgraph-<version>
Step by step
Here is a complete example to create a simple LangGraph stateful graph with one node that appends a response to messages, then runs the graph.
from langgraph.graph import StateGraph, END
from typing import TypedDict
class State(TypedDict):
messages: list
def my_node(state: State) -> State:
return {"messages": state["messages"] + ["response"]}
graph = StateGraph(State)
graph.add_node("my_node", my_node)
graph.set_entry_point("my_node")
graph.add_edge("my_node", END)
app = graph.compile()
result = app.invoke({"messages": ["Hello"]})
print(result) output
{'messages': ['Hello', 'response']} Common variations
You can define more complex state types with TypedDict and add multiple nodes and edges to build complex workflows. The invoke method runs the compiled graph synchronously. For persistence, use a checkpointer like MemorySaver when compiling.
from langgraph.graph import StateGraph, END
from langgraph.checkpointers.memory import MemorySaver
from typing import TypedDict
class State(TypedDict):
count: int
def increment_node(state: State) -> State:
return {"count": state["count"] + 1}
graph = StateGraph(State)
graph.add_node("increment", increment_node)
graph.set_entry_point("increment")
graph.add_edge("increment", END)
checkpointer = MemorySaver()
app = graph.compile(checkpointer=checkpointer)
result = app.invoke({"count": 0})
print(result) output
{'count': 1} Troubleshooting
- If you see
ModuleNotFoundError: No module named 'langgraph', ensure you installed the package withpip install langgraphin the correct Python environment. - If your graph does not run, verify that you set an entry point with
set_entry_point()and added edges toEND. - For type errors, confirm your
TypedDictstate matches the node function input and output.
Key Takeaways
- Install LangGraph with
pip install langgraphfor stateful AI agent graphs. - Define your state with
TypedDictand add nodes and edges to build workflows. - Use
graph.compile()andapp.invoke()to run your graph synchronously. - Set an entry point and edges to
ENDto ensure graph execution completes. - Troubleshoot missing modules by verifying your Python environment and installation.