LangGraph Code Assistant
Author: Junseong Kim
Design: Junseong Kim
Peer Review:
Proofread : fastjw
This is a part of LangChain Open Tutorial
Overview
In this tutorial, we will build a simple code assistant workflow using langgraph and LangChain. We will demonstrate how to:
Load and parse documentation from a URL using
RecursiveUrlLoader.Create a Pydantic model (
code) to structure code-generation responses.Use an LLM (Anthropic’s Claude) with a specialized prompt to generate code solutions.
Integrate a custom parsing function to handle raw and structured outputs.
Construct a state machine with
langgraphto:Generate code (
generatenode)Check imports and execution (
check_codenode)Reflect and retry if needed (
reflectnode)
Visualize the workflow graph and finally display the generated code in a clean Markdown format.
By the end of this tutorial, you’ll be able to set up a multi-step code assistant pipeline that can iteratively generate, validate, and reflect on generated code.

Table of Contents
References
Environment Setup
Setting up your environment is the first step. See the Environment Setup guide for more details.
[Note]
The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.
Check out the langchain-opentutorial for more details.
You can set API keys in a .env file or set them manually.
[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.
Building the Code Assistant Workflow
Below, we will build a code assistant workflow using langgraph. This workflow can:
Load documentation with a custom loader.
Use a large language model (Anthropic’s Claude) with a structured output format.
Check generated code for errors and prompt for a retry if needed.
Reflect on errors and regenerate code.
We will demonstrate each step with a combination of Markdown explanations and code cells.
Loading and Preparing Documentation
We use the RecursiveUrlLoader (from langchain_community) to fetch and parse documentation. We’ll store the documents in docs, sort them, and then concatenate the page content into a single string for use in our prompt.
Defining a Pydantic Model for Code
We define a Pydantic model named code to capture structured outputs for code generation. This model enforces a specific schema with fields for prefix, imports, and code.
Setting Up the Prompt and LLM Chain
We construct a prompt that instructs the LLM (Anthropic’s Claude) to produce answers in our specified code format.
Next, we wrap this language model with with_structured_output to ensure it returns data that can be parsed into our code Pydantic model:
Finally, we define a function parse_output to extract the parsed solution from the LLM’s raw response, and then we combine everything into a single chain:
Parsing the LLM Output
We run a quick test by asking a sample question. The chain returns a structured code object with prefix, imports, and code.
Constructing the State Machine
To handle multiple steps—generating code, checking it, and reflecting on errors—we can use a langgraph state machine.
First, we create a TypedDict called GraphState to store our pipeline’s state. We also define some global parameters:
1. Define the Nodes
We have three main nodes in our state machine:
generate: Calls the LLM chain to produce code.
code_check: Attempts to
execthe code. If it fails, we raise a flag.reflect: Optionally reflect on errors and refine the solution.
We’ll define each one in its own cell for clarity.
1.1 generate Node
This node invokes the code_gen_chain to generate a new code solution. If there was a previous error, it adds a user message instructing the LLM to try again.
1.2 code_check Node
This node checks the generated code by attempting to exec the imports and the main code block. If anything fails, it appends an error message and flags the state.
1.3 reflect Node
If code checking fails, we can optionally reflect on errors and refine the solution. This node prompts the LLM for additional insights and attaches them to the current message list.
2. Defining the Workflow
Next, we tie these nodes together with langgraph to create our state machine. We also define a helper function decide_to_finish that determines whether we have encountered an error or exceeded our iteration limit.
3. Visualizing the Workflow
We can visualize the state machine using the visualize_graph function from langchain_opentutorial.graphs.

Final Invocation and Display
Finally, we pass a user question into our compiled state machine. The pipeline will:
Generate code.
Check it.
Reflect if there’s an error.
Repeat until success or until we reach our maximum number of attempts.
We define a helper function to display the resulting code in a Markdown cell:
The final output is displayed in Markdown, allowing you to immediately see the generated code solution
To pass a string directly through to a prompt, you can use the RunnablePassthrough class. This will take the input string and pass it through unchanged to be used in constructing the prompt input.
Last updated