Part 1 – Introduction to LangGraph & Understanding State, Nodes, Edges and Conditional Routing (with Typesafe Python)
Langgraph is a Python library for building complex, stateful AI workflows. It allows developers to construct graph-based pipelines where each node represents a computational step, such as a language model call, a decision, or a transformation. With stateful graphs, conditional routing, and type safety, Langgraph helps build robust and maintainable AI applications.
In this blog (Part 1), we will cover:
Introduction to Langgraph
Required packages
Understanding State, Nodes, Edges
Building START → END graphs
Implementing conditional nodes
We will use TypeSafe Python throughout to ensure correctness and readability.
Section 1: Imports and Environment Setup
Before working with Langgraph, we need to import required libraries and load environment variables.
import os
from dotenv import load_dotenv
# --- Langgraph Imports ---
from langgraph.graph import START, END, StateGraph
# Typing for state safety
from typing_extensions import TypedDict
from typing import Sequence, Literal
# OpenAI chat model
from langchain_openai.chat_models import ChatOpenAI
from langchain_core.messages import HumanMessage, BaseMessage
from langchain_core.runnables import Runnable
Explanation:
START and END: Special nodes in Langgraph that mark entry and exit points in the graph.
StateGraph: Core class to define and manage a graph of nodes.
TypedDict and Sequence: Provide type safety for Python dictionaries and lists.
ChatOpenAI: Interface to call OpenAI chat models.
HumanMessage and BaseMessage: Represent messages passed through the state.
Next, load environment variables:
load_dotenv()
This ensures any API keys (e.g., OpenAI) are loaded from .env.
Section 2: Define the State and a Basic Node
In Langgraph, state is the central object passed between nodes. It contains all information needed at any step.
class State(TypedDict):
messages: Sequence[BaseMessage]
# Initialize state with a human message
state = State(messages=[HumanMessage("Could you tell me a grook by Piet Hein?")])
Explanation:
State defines the structure of our state dictionary. Here it only contains messages.
TypedDict ensures each state key has a defined type (messages is a sequence of BaseMessage).
HumanMessage is from LangChain and represents user input.
In the previous two parts, we built a strong foundation of LangGraph fundamentals—nodes, edges, message states, conditional routing, reducers, summarization loops, and graph orchestration.
In Part-1 of this LangGraph Blog Series, we understood the foundation of LangGraph — Graph structure, Nodes, Edges, Conditional Routing, State system, and Graph Execution.
Now in Part-2, we upgrade our knowledge and turn LangGraph into a real conversation system.
Modern AI workflows need more than just a prompt and a model call. Real applications require memory, state transitions, branching logic, routing decisions, and orchestration of multiple AI models. This is where LangGraph enters the scene.