Skip to main content
In this section, you will learn the basics of building a LangGraph by constructing a simple “Hello World” graph.

Objectives

  1. Understand and define the AgentState structure.
  2. Create simple node functions to process and update state.
  3. Set up a basic LangGraph structure.
  4. Compile and invoke a LangGraph graph.
  5. Understand how data flows through a single node in LangGraph.

Project Setup

Before writing any code, let’s set up a clean, isolated project environment using uv and install the required dependencies:

1. Initialize the Project

Create a new directory for your project and initialize it with uv:

2. Install Dependencies

Install the required langgraph package using uv add:
This will automatically create a virtual environment (.venv) and lock exact versions in uv.lock. Now you can create a file (e.g., main.py) and start editing!

Graph 0: Hello World

Goal

Build a simple graph that updates a state containing only one variable msg to "Hello, World!".

Sample Input

Sample Output

Plan

  1. Define the state schema (AgentState0) with a single variable msg.
  2. Define the node function hello_node that sets msg to "Hello, World!".
  3. Build and compile the graph.
  4. Invoke the graph with an initial empty message.

Code Implementation

1. Define the State Schema

We define a state schema with a single field msg of type str:

2. Define the Node Function

The node function updates the msg field in the state to "Hello, World!":

3. Build and Compile the Graph

We build a single-node graph:

4. Invoke the Graph

We invoke the graph by passing an initial state with an empty string:

Graph I: Hello World Graph

Goal

Build a simple graph that takes a user’s name as input and outputs a personalized greeting.

Sample Input

Sample Output

Plan

  1. Define the AgentState schema containing name and greet variables.
  2. Define the node function greeting_node that reads the input name and constructs the personalized greeting.
  3. Build and compile the graph.
  4. Invoke the graph with a custom name.

Code Implementation

1. Define the State Schema

The state defines the data structure shared across the graph. We use a TypedDict for this:

2. Define the Node Function

A node is just a Python function that takes the current state as input and returns an updated dictionary of fields to merge back into the state:

3. Build and Compile the Graph

We use StateGraph to define our nodes and layout:

4. Invoke the Graph

We invoke the compiled application by passing the initial state:

Exercise: Personalized Compliment Agent 🏗

Goal

Create a Personalized Compliment Agent using LangGraph.

Sample Input

Sample Output

Plan

  1. Define a state with name and compliment keys.
  2. Use a single node that updates compliment using the input name.
  3. Build, compile, and invoke the graph.

Practice & Exercises

To reinforce what you’ve learned in this section (defining schemas, creating nodes, compiling and invoking graphs), practice with the interactive notebook:

Practice & Exercises

Practice variable state schemas, node functions, building/compiling graphs, and implementing a personalized compliment agent.💻 VS Code | 🚀 Colab | 📥 Download