WEBVTT

00:00.000 --> 00:01.800
-: Hey, in this video we're gonna have a look at

00:01.800 --> 00:03.870
how you can manually update the state

00:03.870 --> 00:06.270
within a LangGraph execution.

00:06.270 --> 00:09.210
Okay, cool, so we've got a couple of imports to start with,

00:09.210 --> 00:12.110
and as well as that, I'm just gonna load my Tavily key in.

00:13.500 --> 00:16.200
Right, so manually updating the state is important

00:16.200 --> 00:18.060
because what that allows us to do

00:18.060 --> 00:20.460
is not only interrupt the state

00:20.460 --> 00:21.630
and let the human read the state,

00:21.630 --> 00:24.480
but maybe they also want to have some form of write access.

00:24.480 --> 00:27.960
We could do some sort of manuals updating of the state

00:27.960 --> 00:30.180
to change the agent's trajectory

00:30.180 --> 00:31.950
and see even modifying the past

00:31.950 --> 00:34.274
and how, you know, we want that

00:34.274 --> 00:35.190
to avoid making mistakes

00:35.190 --> 00:38.070
and also guide the agents towards their goals.

00:38.070 --> 00:40.470
So we're using the same kind of thing that we had before.

00:40.470 --> 00:41.730
Same exact code.

00:41.730 --> 00:43.260
We've also got the check pointer

00:43.260 --> 00:44.730
and the SQLite saver

00:44.730 --> 00:47.707
and we have a bit of user input which says,

00:47.707 --> 00:49.050
"I'm learning LangGraph.

00:49.050 --> 00:51.180
Could you do some research on it for me?"

00:51.180 --> 00:54.660
And we can get a state of that

00:54.660 --> 00:57.660
and we can get the values and we can pretty print those.

00:57.660 --> 00:59.610
So if we have a look, you can see we've got the tool call

00:59.610 --> 01:01.440
that is being made and understanding

01:01.440 --> 01:03.120
what's happening inside of LangGraph.

01:03.120 --> 01:07.110
And that's using that tally tool with the tool ID here.

01:07.110 --> 01:09.330
But one thing is that we want to do is we want

01:09.330 --> 01:11.880
to overwrite the state that happened before.

01:11.880 --> 01:14.460
So you can see this is the answer that we want to do.

01:14.460 --> 01:18.390
And what we wanna do is insert a tool message for that

01:18.390 --> 01:20.130
and then directly put that,

01:20.130 --> 01:22.380
put some words into the LLM's mouth.

01:22.380 --> 01:23.700
So we're sort of giving the answer.

01:23.700 --> 01:27.360
We are updating the existing tool calls,

01:27.360 --> 01:29.430
so we're getting that ID

01:29.430 --> 01:32.040
and then we're attaching that to the tool call ID

01:32.040 --> 01:33.330
for that tool message

01:33.330 --> 01:35.640
and also putting it in as an AI message.

01:35.640 --> 01:39.300
And then we are going to update the state of our graph

01:39.300 --> 01:42.270
and we're going to add in these new messages.

01:42.270 --> 01:44.550
And you'll see what happens here is

01:44.550 --> 01:47.730
we've got these two new messages, the LangGraph library

01:47.730 --> 01:51.090
and these last two messages, which is the tool message.

01:51.090 --> 01:53.980
And also we have the AI messages

01:55.154 --> 01:56.730
with the point that we had on it.

01:56.730 --> 01:59.940
Now, now that we've like done that on our graph, you know,

01:59.940 --> 02:02.010
the important thing is we're gonna do this

02:02.010 --> 02:03.570
graph.update_state

02:03.570 --> 02:06.450
and you can see for example, you've got this

02:06.450 --> 02:10.560
as_node chat point, which allows us to say this is the node

02:10.560 --> 02:13.080
that we're gonna go back into and update that state.

02:13.080 --> 02:14.610
So if we run this, you'll see

02:14.610 --> 02:17.250
that you can update a specific point.

02:17.250 --> 02:19.770
And if you were to check out the LangChain traces on this,

02:19.770 --> 02:22.420
you will see that we are basically updating the state

02:23.615 --> 02:25.350
of the graph in real time.

02:25.350 --> 02:27.750
And so that's a way for us to update the state

02:27.750 --> 02:29.820
and get the new state of the graph.

02:29.820 --> 02:32.070
The important point here is you've got this as_node

02:32.070 --> 02:35.670
that makes sure that you always run from the chatbot node

02:35.670 --> 02:38.430
onwards with the updated state.

02:38.430 --> 02:41.490
If we have a look at the sort of diagram,

02:41.490 --> 02:43.260
you haven't got any changes,

02:43.260 --> 02:46.980
but if we now have a look at the get_state config,

02:46.980 --> 02:48.750
what you'll see is, you know,

02:48.750 --> 02:51.810
we've got this ToolMessage, "LangGraph is a library

02:51.810 --> 02:53.310
for building stateful actors"

02:53.310 --> 02:56.490
and we don't have anything next to do.

02:56.490 --> 03:00.556
And that confirms that we already have called the tool

03:00.556 --> 03:05.556
and provided the tool messages back in here.

03:05.850 --> 03:08.970
And so we have actually finished the state machine.

03:08.970 --> 03:11.250
There is no snapshot.next.

03:11.250 --> 03:13.980
And that kind of shows us that we've done that.

03:13.980 --> 03:16.410
We've also added some AI messages to the state.

03:16.410 --> 03:18.330
But what happens if we want to overwrite something

03:18.330 --> 03:19.440
that happened in the past?

03:19.440 --> 03:21.810
Well you've got an example here where you know,

03:21.810 --> 03:23.760
I'm learning some LangGraph.

03:23.760 --> 03:27.420
We set up a new thread_id and we're gonna stream that

03:27.420 --> 03:29.400
and then it will use the tool call.

03:29.400 --> 03:32.850
So it's decided to call Tavily here

03:32.850 --> 03:35.640
and it's also decided to call Tavily again.

03:35.640 --> 03:38.280
Now we are just gonna update one of these tool calls.

03:38.280 --> 03:41.910
So what we do is do a graph.get_state of the config,

03:41.910 --> 03:44.820
we get the snapshot.values in that messages.

03:44.820 --> 03:46.230
And the important point here is

03:46.230 --> 03:50.250
that we are using the existing message.id

03:50.250 --> 03:51.810
because that is what we're gonna do

03:51.810 --> 03:53.370
to update an existing message.

03:53.370 --> 03:57.150
So you can see here we're getting the new tool call,

03:57.150 --> 03:59.580
we're then making some new args,

03:59.580 --> 04:02.760
and we're then adding an AI message.

04:02.760 --> 04:06.600
And in that message we're actually saying, you know,

04:06.600 --> 04:08.370
update the content of that message

04:08.370 --> 04:09.990
with these new tool calls.

04:09.990 --> 04:12.270
And this id is how LangGraph knows

04:12.270 --> 04:13.740
how to replace the message in the state

04:13.740 --> 04:15.330
rather than append to this.

04:15.330 --> 04:19.140
And so what you can see here is the original had this

04:19.140 --> 04:21.300
as the tool call for the first one,

04:21.300 --> 04:22.890
which is LangGraph overview,

04:22.890 --> 04:26.370
but we've now updated that so that it looks like this.

04:26.370 --> 04:28.800
So we're just basically, yeah, you know,

04:28.800 --> 04:30.000
making that new message

04:30.000 --> 04:32.970
and making sure it has the existing messages ID

04:32.970 --> 04:37.500
and then using the graph.update to update the messages

04:37.500 --> 04:39.810
based on those IDs.

04:39.810 --> 04:41.670
And then this last bit here is basically just getting

04:41.670 --> 04:44.490
that valued state so that you can see that

04:44.490 --> 04:46.680
the state has actually changed.

04:46.680 --> 04:49.830
And then once we can now just put none in for the input

04:49.830 --> 04:51.600
and just let the graph run,

04:51.600 --> 04:54.780
given that we've updated the previous tool calls before,

04:54.780 --> 04:56.790
and you can see if we have a look at this,

04:56.790 --> 05:00.270
it's gonna search for the Tavily results,

05:00.270 --> 05:01.950
and that's basically what's happening here.

05:01.950 --> 05:03.630
So you've got these events that are coming through

05:03.630 --> 05:05.640
and we've basically done that.

05:05.640 --> 05:09.510
So this is a bit of a segue into

05:09.510 --> 05:11.370
how we can update the state

05:11.370 --> 05:14.940
of a graph using the .update_state

05:14.940 --> 05:17.580
and passing in the config that you'll see here,

05:17.580 --> 05:19.050
which is a runnable config.

05:19.050 --> 05:21.690
And also we can get the state which allows us

05:21.690 --> 05:24.150
to manipulate things such as the tool calls

05:24.150 --> 05:25.680
or the previous messages.

05:25.680 --> 05:28.200
The other gotcha that is really important to remember

05:28.200 --> 05:29.700
is you also need to,

05:29.700 --> 05:31.980
if you're going to be updating the state

05:31.980 --> 05:34.170
inside the messages, the message history,

05:34.170 --> 05:36.540
make sure that you use these IDs

05:36.540 --> 05:37.950
because that is how LangGraph is going

05:37.950 --> 05:39.750
to deduplicate your state machine

05:39.750 --> 05:41.580
for the messages specifically.

05:41.580 --> 05:42.453
Alright, cool.

05:43.440 --> 05:44.730
In the next video we're gonna look at

05:44.730 --> 05:46.200
how we can customize the state

05:46.200 --> 05:49.080
beyond just looking at having a messages key

05:49.080 --> 05:51.713
with inside of LangGraph, I'll see you in the next one.
