-: Hey, in this video we're gonna have a look at
how you can manually update the state
within a LangGraph execution.
Okay, cool, so we've got a couple of imports to start with,
and as well as that, I'm just gonna load my Tavily key in.
Right, so manually updating the state is important
because what that allows us to do
is not only interrupt the state
and let the human read the state,
but maybe they also want to have some form of write access.
We could do some sort of manuals updating of the state
to change the agent's trajectory
and see even modifying the past
and how, you know, we want that
to avoid making mistakes
and also guide the agents towards their goals.
So we're using the same kind of thing that we had before.
Same exact code.
We've also got the check pointer
and the SQLite saver
and we have a bit of user input which says,
"I'm learning LangGraph.
Could you do some research on it for me?"
And we can get a state of that
and we can get the values and we can pretty print those.
So if we have a look, you can see we've got the tool call
that is being made and understanding
what's happening inside of LangGraph.
And that's using that tally tool with the tool ID here.
But one thing is that we want to do is we want
to overwrite the state that happened before.
So you can see this is the answer that we want to do.
And what we wanna do is insert a tool message for that
and then directly put that,
put some words into the LLM's mouth.
So we're sort of giving the answer.
We are updating the existing tool calls,
so we're getting that ID
and then we're attaching that to the tool call ID
for that tool message
and also putting it in as an AI message.
And then we are going to update the state of our graph
and we're going to add in these new messages.
And you'll see what happens here is
we've got these two new messages, the LangGraph library
and these last two messages, which is the tool message.
And also we have the AI messages
with the point that we had on it.
Now, now that we've like done that on our graph, you know,
the important thing is we're gonna do this
graph.update_state
and you can see for example, you've got this
as_node chat point, which allows us to say this is the node
that we're gonna go back into and update that state.
So if we run this, you'll see
that you can update a specific point.
And if you were to check out the LangChain traces on this,
you will see that we are basically updating the state
of the graph in real time.
And so that's a way for us to update the state
and get the new state of the graph.
The important point here is you've got this as_node
that makes sure that you always run from the chatbot node
onwards with the updated state.
If we have a look at the sort of diagram,
you haven't got any changes,
but if we now have a look at the get_state config,
what you'll see is, you know,
we've got this ToolMessage, "LangGraph is a library
for building stateful actors"
and we don't have anything next to do.
And that confirms that we already have called the tool
and provided the tool messages back in here.
And so we have actually finished the state machine.
There is no snapshot.next.
And that kind of shows us that we've done that.
We've also added some AI messages to the state.
But what happens if we want to overwrite something
that happened in the past?
Well you've got an example here where you know,
I'm learning some LangGraph.
We set up a new thread_id and we're gonna stream that
and then it will use the tool call.
So it's decided to call Tavily here
and it's also decided to call Tavily again.
Now we are just gonna update one of these tool calls.
So what we do is do a graph.get_state of the config,
we get the snapshot.values in that messages.
And the important point here is
that we are using the existing message.id
because that is what we're gonna do
to update an existing message.
So you can see here we're getting the new tool call,
we're then making some new args,
and we're then adding an AI message.
And in that message we're actually saying, you know,
update the content of that message
with these new tool calls.
And this id is how LangGraph knows
how to replace the message in the state
rather than append to this.
And so what you can see here is the original had this
as the tool call for the first one,
which is LangGraph overview,
but we've now updated that so that it looks like this.
So we're just basically, yeah, you know,
making that new message
and making sure it has the existing messages ID
and then using the graph.update to update the messages
based on those IDs.
And then this last bit here is basically just getting
that valued state so that you can see that
the state has actually changed.
And then once we can now just put none in for the input
and just let the graph run,
given that we've updated the previous tool calls before,
and you can see if we have a look at this,
it's gonna search for the Tavily results,
and that's basically what's happening here.
So you've got these events that are coming through
and we've basically done that.
So this is a bit of a segue into
how we can update the state
of a graph using the .update_state
and passing in the config that you'll see here,
which is a runnable config.
And also we can get the state which allows us
to manipulate things such as the tool calls
or the previous messages.
The other gotcha that is really important to remember
is you also need to,
if you're going to be updating the state
inside the messages, the message history,
make sure that you use these IDs
because that is how LangGraph is going
to deduplicate your state machine
for the messages specifically.
Alright, cool.
In the next video we're gonna look at
how we can customize the state
beyond just looking at having a messages key
with inside of LangGraph, I'll see you in the next one.