Graph Definitions¶
Graphs are the core abstraction of LangGraph. Each StateGraph implementation is used to create graph workflows. Once compiled, you can run the CompiledGraph to run the application.
Graph
¶
Source code in langgraph/graph/graph.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
add_conditional_edges(source, path, path_map=None, then=None)
¶
Add a conditional edge from the starting node to any number of destination nodes.
Parameters:
-
source(str) –The starting node. This conditional edge will run when exiting this node.
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_entry_point(key)
¶
Specifies the first node to be called in the graph.
Parameters:
-
key(str) –The key of the node to set as the entry point.
Returns:
-
None–None
set_conditional_entry_point(path, path_map=None, then=None)
¶
Sets a conditional entry point in the graph.
Parameters:
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
MessageGraph
¶
Bases: StateGraph
A StateGraph where every node receives a list of messages as input and returns one or more messages as output.
MessageGraph is a subclass of StateGraph whose entire state is a single, append-only* list of messages.
Each node in a MessageGraph takes a list of messages as input and returns zero or more
messages as output. The add_messages function is used to merge the output messages from each node
into the existing list of messages in the graph's state.
Examples:
from langgraph.graph.message import MessageGraph
builder = MessageGraph()
builder.add_node("chatbot", lambda state: [("assistant", "Hello!")])
builder.set_entry_point("chatbot")
builder.set_finish_point("chatbot")
builder.compile().invoke([("user", "Hi there.")])
# {'messages': [HumanMessage(content="Hi there.", id='b8b7d8f4-7f4d-4f4d-9c1d-f8b8d8f4d9c1'),
# AIMessage(content="Hello!", id='f4d9c1d8-8d8f-4d9c-b8b7-d8f4f4d9c1d8')]}
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langgraph.graph.message import MessageGraph
builder = MessageGraph()
builder.add_node(
"chatbot",
lambda state: [
AIMessage(
content="Hello!",
tool_calls=[{"name": "search", "id": "123", "args": {"query": "X"}}],
)
],
)
builder.add_node(
"search", lambda state: [ToolMessage(content="Searching...", tool_call_id="123")]
)
builder.set_entry_point("chatbot")
builder.add_edge("chatbot", "search")
builder.set_finish_point("search")
builder.compile().invoke([HumanMessage(content="Hi there. Can you search for X?")])
# {'messages': [HumanMessage(content="Hi there. Can you search for X?", id='b8b7d8f4-7f4d-4f4d-9c1d-f8b8d8f4d9c1'),
# AIMessage(content="Hello!", id='f4d9c1d8-8d8f-4d9c-b8b7-d8f4f4d9c1d8'),
# ToolMessage(content="Searching...", id='d8f4f4d9-c1d8-4f4d-b8b7-d8f4f4d9c1d8', tool_call_id="123")]}
Source code in langgraph/graph/message.py
add_node(key, action)
¶
Adds a new node to the state graph.
Parameters:
-
key(str) –The key of the node.
-
action(RunnableLike) –The action associated with the node.
Raises:
-
ValueError–If the key is already being used as a state key.
Returns:
-
None–None
Source code in langgraph/graph/state.py
add_edge(start_key, end_key)
¶
Adds a directed edge from the start node to the end node.
If the graph transitions to the start_key node, it will always transition to the end_key node next.
Parameters:
-
start_key(Union[str, list[str]]) –The key(s) of the start node(s) of the edge.
-
end_key(str) –The key of the end node of the edge.
Raises:
-
ValueError–If the start key is 'END' or if the start key or end key is not present in the graph.
Returns:
-
None–None
Source code in langgraph/graph/state.py
add_conditional_edges(source, path, path_map=None, then=None)
¶
Add a conditional edge from the starting node to any number of destination nodes.
Parameters:
-
source(str) –The starting node. This conditional edge will run when exiting this node.
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_entry_point(key)
¶
Specifies the first node to be called in the graph.
Parameters:
-
key(str) –The key of the node to set as the entry point.
Returns:
-
None–None
set_conditional_entry_point(path, path_map=None, then=None)
¶
Sets a conditional entry point in the graph.
Parameters:
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_finish_point(key)
¶
Marks a node as a finish point of the graph.
If the graph reaches this node, it will cease execution.
Parameters:
-
key(str) –The key of the node to set as the finish point.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
compile(checkpointer=None, interrupt_before=None, interrupt_after=None, debug=False)
¶
Compiles the state graph into a CompiledGraph object.
The compiled graph implements the Runnable interface and can be invoked,
streamed, batched, and run asynchronously.
Parameters:
-
checkpointer(Optional[BaseCheckpointSaver], default:None) –An optional checkpoint saver object. This serves as a fully versioned "memory" for the graph, allowing the graph to be paused and resumed, and replayed from any point.
-
interrupt_before(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt before.
-
interrupt_after(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt after.
-
debug(bool, default:False) –A flag indicating whether to enable debug mode.
Returns:
-
CompiledGraph(CompiledGraph) –The compiled state graph.
Source code in langgraph/graph/state.py
StateGraph
¶
Bases: Graph
A graph whose nodes communicate by reading and writing to a shared state.
The signature of each node is State -> Partial
Each state key can optionally be annotated with a reducer function that will be used to aggregate the values of that key received from multiple nodes. The signature of a reducer function is (Value, Value) -> Value.
Source code in langgraph/graph/state.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
add_conditional_edges(source, path, path_map=None, then=None)
¶
Add a conditional edge from the starting node to any number of destination nodes.
Parameters:
-
source(str) –The starting node. This conditional edge will run when exiting this node.
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_entry_point(key)
¶
Specifies the first node to be called in the graph.
Parameters:
-
key(str) –The key of the node to set as the entry point.
Returns:
-
None–None
set_conditional_entry_point(path, path_map=None, then=None)
¶
Sets a conditional entry point in the graph.
Parameters:
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_finish_point(key)
¶
Marks a node as a finish point of the graph.
If the graph reaches this node, it will cease execution.
Parameters:
-
key(str) –The key of the node to set as the finish point.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
add_node(key, action)
¶
Adds a new node to the state graph.
Parameters:
-
key(str) –The key of the node.
-
action(RunnableLike) –The action associated with the node.
Raises:
-
ValueError–If the key is already being used as a state key.
Returns:
-
None–None
Source code in langgraph/graph/state.py
add_edge(start_key, end_key)
¶
Adds a directed edge from the start node to the end node.
If the graph transitions to the start_key node, it will always transition to the end_key node next.
Parameters:
-
start_key(Union[str, list[str]]) –The key(s) of the start node(s) of the edge.
-
end_key(str) –The key of the end node of the edge.
Raises:
-
ValueError–If the start key is 'END' or if the start key or end key is not present in the graph.
Returns:
-
None–None
Source code in langgraph/graph/state.py
compile(checkpointer=None, interrupt_before=None, interrupt_after=None, debug=False)
¶
Compiles the state graph into a CompiledGraph object.
The compiled graph implements the Runnable interface and can be invoked,
streamed, batched, and run asynchronously.
Parameters:
-
checkpointer(Optional[BaseCheckpointSaver], default:None) –An optional checkpoint saver object. This serves as a fully versioned "memory" for the graph, allowing the graph to be paused and resumed, and replayed from any point.
-
interrupt_before(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt before.
-
interrupt_after(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt after.
-
debug(bool, default:False) –A flag indicating whether to enable debug mode.
Returns:
-
CompiledGraph(CompiledGraph) –The compiled state graph.
Source code in langgraph/graph/state.py
add_messages(left, right)
¶
Merges two lists of messages, updating existing messages by ID.
By default, this ensures the state is "append-only", unless the new message has the same ID as an existing message.
Parameters:
-
left(Messages) –The base list of messages.
-
right(Messages) –The list of messages (or single message) to merge into the base list.
Returns:
-
Messages–A new list of messages with the messages from
rightmerged intoleft. -
Messages–If a message in
righthas the same ID as a message inleft, the -
Messages–message from
rightwill replace the message fromleft.
Examples:
msgs1 = [HumanMessage(content="Hello", id="1")]
msgs2 = [AIMessage(content="Hi there!", id="2")]
add_messages(msgs1, msgs2)
# [HumanMessage(content="Hello", id="1"), AIMessage(content="Hi there!", id="2")]
msgs1 = [HumanMessage(content="Hello", id="1")]
msgs2 = [HumanMessage(content="Hello again", id="1")]
add_messages(msgs1, msgs2)
# [HumanMessage(content="Hello again", id="1")]
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
class State(TypedDict):
messages: Annotated[list, add_messages]
builder = StateGraph(State)
builder.add_node("chatbot", lambda state: {"messages": [("assistant", "Hello")]})
builder.set_entry_point("chatbot")
builder.set_finish_point("chatbot")
graph = builder.compile()
graph.invoke({})
# {'messages': [AIMessage(content='Hello', id='f657fb65-b6af-4790-a5b5-1d266a2ed26e')]}
Source code in langgraph/graph/message.py
CompiledGraph¶
Bases: Pregel
Source code in langgraph/graph/graph.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
output_channels: Union[str, Sequence[str]]
instance-attribute
¶
Channels to output, defaults to channel named 'output'.
stream_channels: Optional[Union[str, Sequence[str]]] = None
class-attribute
instance-attribute
¶
Channels to stream, defaults to all channels not in reserved channels
is_lc_serializable()
classmethod
¶
get_state(config)
¶
Get the current state of the graph.
Source code in langgraph/pregel/__init__.py
aget_state(config)
async
¶
Get the current state of the graph.
Source code in langgraph/pregel/__init__.py
get_state_history(config, *, before=None, limit=None)
¶
Get the history of the state of the graph.
Source code in langgraph/pregel/__init__.py
aget_state_history(config, *, before=None, limit=None)
async
¶
Get the history of the state of the graph.
Source code in langgraph/pregel/__init__.py
update_state(config, values, as_node=None)
¶
Update the state of the graph with the given values, as if they came from
node as_node. If as_node is not provided, it will be set to the last node
that updated the state, if not ambiguous.
Source code in langgraph/pregel/__init__.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
stream(input, config=None, *, stream_mode=None, output_keys=None, input_keys=None, interrupt_before=None, interrupt_after=None, debug=None)
¶
Stream graph steps for a single input.
Source code in langgraph/pregel/__init__.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | |
invoke(input, config=None, *, stream_mode='values', output_keys=None, input_keys=None, interrupt_before=None, interrupt_after=None, debug=None, **kwargs)
¶
Run the graph with a single input and config.
Parameters:
-
input(Union[dict[str, Any], Any]) –The input data for the graph. It can be a dictionary or any other type.
-
config(Optional[RunnableConfig], default:None) –Optional. The configuration for the graph run.
-
stream_mode(StreamMode, default:'values') –Optional[str]. The stream mode for the graph run. Default is "values".
-
output_keys(Optional[Union[str, Sequence[str]]], default:None) –Optional. The output keys to retrieve from the graph run.
-
input_keys(Optional[Union[str, Sequence[str]]], default:None) –Optional. The input keys to provide for the graph run.
-
interrupt_before(Optional[Union[All, Sequence[str]]], default:None) –Optional. The nodes to interrupt the graph run before.
-
interrupt_after(Optional[Union[All, Sequence[str]]], default:None) –Optional. The nodes to interrupt the graph run after.
-
debug(Optional[bool], default:None) –Optional. Enable debug mode for the graph run.
-
**kwargs(Any, default:{}) –Additional keyword arguments to pass to the graph run.
Returns:
-
Union[dict[str, Any], Any]–The output of the graph run. If stream_mode is "values", it returns the latest output.
-
Union[dict[str, Any], Any]–If stream_mode is not "values", it returns a list of output chunks.
Source code in langgraph/pregel/__init__.py
ainvoke(input, config=None, *, stream_mode='values', output_keys=None, input_keys=None, interrupt_before=None, interrupt_after=None, debug=None, **kwargs)
async
¶
Asynchronously invoke the graph on a single input.
Parameters:
-
input(Union[dict[str, Any], Any]) –The input data for the computation. It can be a dictionary or any other type.
-
config(Optional[RunnableConfig], default:None) –Optional. The configuration for the computation.
-
stream_mode(StreamMode, default:'values') –Optional. The stream mode for the computation. Default is "values".
-
output_keys(Optional[Union[str, Sequence[str]]], default:None) –Optional. The output keys to include in the result. Default is None.
-
input_keys(Optional[Union[str, Sequence[str]]], default:None) –Optional. The input keys to include in the result. Default is None.
-
interrupt_before(Optional[Union[All, Sequence[str]]], default:None) –Optional. The nodes to interrupt before. Default is None.
-
interrupt_after(Optional[Union[All, Sequence[str]]], default:None) –Optional. The nodes to interrupt after. Default is None.
-
debug(Optional[bool], default:None) –Optional. Whether to enable debug mode. Default is None.
-
**kwargs(Any, default:{}) –Additional keyword arguments.
Returns:
-
Union[dict[str, Any], Any]–The result of the computation. If stream_mode is "values", it returns the latest value.
-
Union[dict[str, Any], Any]–If stream_mode is "chunks", it returns a list of chunks.
Source code in langgraph/pregel/__init__.py
get_graph(config=None, *, xray=False)
¶
Returns a drawable representation of the computation graph.
Source code in langgraph/graph/graph.py
MessageGraph¶
Bases: StateGraph
A StateGraph where every node receives a list of messages as input and returns one or more messages as output.
MessageGraph is a subclass of StateGraph whose entire state is a single, append-only* list of messages.
Each node in a MessageGraph takes a list of messages as input and returns zero or more
messages as output. The add_messages function is used to merge the output messages from each node
into the existing list of messages in the graph's state.
Examples:
from langgraph.graph.message import MessageGraph
builder = MessageGraph()
builder.add_node("chatbot", lambda state: [("assistant", "Hello!")])
builder.set_entry_point("chatbot")
builder.set_finish_point("chatbot")
builder.compile().invoke([("user", "Hi there.")])
# {'messages': [HumanMessage(content="Hi there.", id='b8b7d8f4-7f4d-4f4d-9c1d-f8b8d8f4d9c1'),
# AIMessage(content="Hello!", id='f4d9c1d8-8d8f-4d9c-b8b7-d8f4f4d9c1d8')]}
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langgraph.graph.message import MessageGraph
builder = MessageGraph()
builder.add_node(
"chatbot",
lambda state: [
AIMessage(
content="Hello!",
tool_calls=[{"name": "search", "id": "123", "args": {"query": "X"}}],
)
],
)
builder.add_node(
"search", lambda state: [ToolMessage(content="Searching...", tool_call_id="123")]
)
builder.set_entry_point("chatbot")
builder.add_edge("chatbot", "search")
builder.set_finish_point("search")
builder.compile().invoke([HumanMessage(content="Hi there. Can you search for X?")])
# {'messages': [HumanMessage(content="Hi there. Can you search for X?", id='b8b7d8f4-7f4d-4f4d-9c1d-f8b8d8f4d9c1'),
# AIMessage(content="Hello!", id='f4d9c1d8-8d8f-4d9c-b8b7-d8f4f4d9c1d8'),
# ToolMessage(content="Searching...", id='d8f4f4d9-c1d8-4f4d-b8b7-d8f4f4d9c1d8', tool_call_id="123")]}
Source code in langgraph/graph/message.py
add_node(key, action)
¶
Adds a new node to the state graph.
Parameters:
-
key(str) –The key of the node.
-
action(RunnableLike) –The action associated with the node.
Raises:
-
ValueError–If the key is already being used as a state key.
Returns:
-
None–None
Source code in langgraph/graph/state.py
add_edge(start_key, end_key)
¶
Adds a directed edge from the start node to the end node.
If the graph transitions to the start_key node, it will always transition to the end_key node next.
Parameters:
-
start_key(Union[str, list[str]]) –The key(s) of the start node(s) of the edge.
-
end_key(str) –The key of the end node of the edge.
Raises:
-
ValueError–If the start key is 'END' or if the start key or end key is not present in the graph.
Returns:
-
None–None
Source code in langgraph/graph/state.py
add_conditional_edges(source, path, path_map=None, then=None)
¶
Add a conditional edge from the starting node to any number of destination nodes.
Parameters:
-
source(str) –The starting node. This conditional edge will run when exiting this node.
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_entry_point(key)
¶
Specifies the first node to be called in the graph.
Parameters:
-
key(str) –The key of the node to set as the entry point.
Returns:
-
None–None
set_conditional_entry_point(path, path_map=None, then=None)
¶
Sets a conditional entry point in the graph.
Parameters:
-
path(Union[Callable, Runnable]) –The callable that determines the next node or nodes. If not specifying
path_mapit should return one or more nodes. If it returns END, the graph will stop execution. -
path_map(Optional[dict[str, str]], default:None) –Optional mapping of paths to node names. If omitted the paths returned by
pathshould be node names. -
then(Optional[str], default:None) –The name of a node to execute after the nodes selected by
path.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
set_finish_point(key)
¶
Marks a node as a finish point of the graph.
If the graph reaches this node, it will cease execution.
Parameters:
-
key(str) –The key of the node to set as the finish point.
Returns:
-
None–None
Source code in langgraph/graph/graph.py
compile(checkpointer=None, interrupt_before=None, interrupt_after=None, debug=False)
¶
Compiles the state graph into a CompiledGraph object.
The compiled graph implements the Runnable interface and can be invoked,
streamed, batched, and run asynchronously.
Parameters:
-
checkpointer(Optional[BaseCheckpointSaver], default:None) –An optional checkpoint saver object. This serves as a fully versioned "memory" for the graph, allowing the graph to be paused and resumed, and replayed from any point.
-
interrupt_before(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt before.
-
interrupt_after(Optional[Sequence[str]], default:None) –An optional list of node names to interrupt after.
-
debug(bool, default:False) –A flag indicating whether to enable debug mode.
Returns:
-
CompiledGraph(CompiledGraph) –The compiled state graph.
Source code in langgraph/graph/state.py
add_messages¶
Merges two lists of messages, updating existing messages by ID.
By default, this ensures the state is "append-only", unless the new message has the same ID as an existing message.
Parameters:
-
left(Messages) –The base list of messages.
-
right(Messages) –The list of messages (or single message) to merge into the base list.
Returns:
-
Messages–A new list of messages with the messages from
rightmerged intoleft. -
Messages–If a message in
righthas the same ID as a message inleft, the -
Messages–message from
rightwill replace the message fromleft.
Examples:
msgs1 = [HumanMessage(content="Hello", id="1")]
msgs2 = [AIMessage(content="Hi there!", id="2")]
add_messages(msgs1, msgs2)
# [HumanMessage(content="Hello", id="1"), AIMessage(content="Hi there!", id="2")]
msgs1 = [HumanMessage(content="Hello", id="1")]
msgs2 = [HumanMessage(content="Hello again", id="1")]
add_messages(msgs1, msgs2)
# [HumanMessage(content="Hello again", id="1")]
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
class State(TypedDict):
messages: Annotated[list, add_messages]
builder = StateGraph(State)
builder.add_node("chatbot", lambda state: {"messages": [("assistant", "Hello")]})
builder.set_entry_point("chatbot")
builder.set_finish_point("chatbot")
graph = builder.compile()
graph.invoke({})
# {'messages': [AIMessage(content='Hello', id='f657fb65-b6af-4790-a5b5-1d266a2ed26e')]}