Alpha Notice: These docs cover the v1-alpha release. Content is incomplete and subject to change.For the latest stable version, see the v0 LangChain Python or LangChain JavaScript docs.
create_agent()
instead.
Prerequisites
First, make sure you havepytest
installed:
Getting started
Because many LangGraph agents depend on state, a useful pattern is to create your graph before each test where you use it, then compile it within tests with a new checkpointer instance. The below example shows how this works with a simple, linear graph that progresses throughnode1
and node2
. Each node updates the single state key my_key
:
Testing individual nodes and edges
Compiled LangGraph agents expose references to each individual node asgraph.nodes
. You can take advantage of this to test individual nodes within your agent. Note that this will bypass any checkpointers passed when compiling the graph:
Partial execution
For agents made up of larger graphs, you may wish to test partial execution paths within your agent rather than the entire flow end-to-end. In some cases, it may make semantic sense to restructure these sections as subgraphs, which you can invoke in isolation as normal. However, if you do not wish to make changes to your agent graph’s overall structure, you can use LangGraph’s persistence mechanisms to simulate a state where your agent is paused right before the beginning of the desired section, and will pause again at the end of the desired section. The steps are as follows:- Compile your agent with a checkpointer (the in-memory checkpointer
InMemorySaver
will suffice for testing). - Call your agent’s
update_state
method with anas_node
parameter set to the name of the node before the one you want to start your test. - Invoke your agent with the same
thread_id
you used to update the state and aninterrupt_after
parameter set to the name of the node you want to stop at.