Compare commits

..
89 Commits
Author SHA1 Message Date
Vadym BardaandGitHub 4b3e07b67a langgraph: release 0.2.69 (#3256) 2025-01-30 19:56:20 -05:00
Nuno CamposandGitHub 6ba61cc768 Guard and cache calls to find_subgraph_pregel (#3255)
- The result of these doesnt change once a node is created, and it's
fairly expensive to run, so great thing to cache
- There's a variety of errors that can come from inspecting the source
code of a function (part of what this does) so adding a catch-all
try-except block as this should be best-effort, not crash your graph
2025-01-30 16:37:42 -08:00
Nuno Campos 5a79210904 Lint 2025-01-30 16:28:03 -08:00
Nuno Campos 4a60eaf32f Guard and cache calls to find_subgraph_pregel
- The result of these doesnt change once a node is created, and it's fairly expensive to run, so great thing to cache
- There's a variety of errors that can come from inspecting the source code of a function (part of what this does) so adding a catch-all try-except block as this should be best-effort, not crash your graph
2025-01-30 16:26:23 -08:00
Vadym BardaandGitHub cf7c3e7fd1 docs: update README to include built w/ langgraph (#3254) 2025-01-30 22:13:06 +00:00
Vadym BardaandGitHub 141b53ee6e docs: add reference file for config (#3253) 2025-01-30 16:58:00 -05:00
37e8e00f1f When using Command.PARENT also pass existing subgraph state to parent graph (#3134)
Co-authored-by: Vadym Barda <vadym@langchain.dev>
2025-01-30 15:54:44 -05:00
Vadym BardaandGitHub a0ec9017f2 langgraph: add get_stream_writer() (#3251)
Alternative to #3250
2025-01-30 20:45:44 +00:00
Eugene YurtsevandGitHub 80cef60405 docs: concepts update (#3248) 2025-01-30 16:32:17 +00:00
Vadym BardaandGitHub 46cba763be docs: small README update (#3244) 2025-01-29 17:46:32 -05:00
Vadym BardaandGitHub 830e5f4550 docs: update README (#3242) 2025-01-29 22:33:32 +00:00
Vadym BardaandGitHub 55e9409b6e docs: update multi-agent multi-turn how-to (#3241) 2025-01-29 22:28:03 +00:00
Vadym BardaandGitHub 39e65a1a62 langgraph: expose tags in the metadata for streamed message chunks (#3238) 2025-01-29 20:24:32 +00:00
Vadym BardaandGitHub 82148e9bf2 docs: update streaming docstring for Pregel and expose in api ref (#3229) 2025-01-29 12:07:07 -05:00
Harsh NevseandGitHub ed09a77d9f Update multi-agent-collaboration.ipynb (#3233)
grammar
2025-01-29 11:48:54 -05:00
William FHandGitHub 7e267897c9 Update auth file paths in build/up (#3231) 2025-01-29 05:09:10 -08:00
Andrew NguonlyandGitHub d279902156 docs: Add LangSmith Integration section for Cloud SaaS concepts (#3230) 2025-01-28 13:27:51 -08:00
Eugene YurtsevandGitHub 440158b969 Update functional_api.md (#3227) 2025-01-28 17:57:01 +00:00
0cf3a64d66 Do not inject args in RunnableCallable if arg already exists (#3185)
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-01-28 12:48:33 -05:00
Vadym BardaandGitHub 953e2907d4 docs: remove MessageGraph from concepts doc (#3226) 2025-01-28 10:16:54 -05:00
Vadym BardaandGitHub 0a861815b7 docs: replace 'state_modifier' with 'prompt' (#3190) 2025-01-28 02:51:40 +00:00
Eugene YurtsevandGitHub 928126f2d7 docs: functional api concepts paraphrase (#3223) 2025-01-27 21:26:13 -05:00
Vadym BardaandGitHub 0d8fea847a langgraph: release 0.2.68 (#3224) 2025-01-27 21:08:06 -05:00
Eugene YurtsevandGitHub b4dc3a851f functional api: remove generator support (#3220)
Remove generator support in entrypoint.
2025-01-27 19:42:35 -05:00
Vadym BardaandGitHub 7568862013 langgraph: more unittests for functional API (#3221) 2025-01-28 00:41:15 +00:00
Andrew NguonlyandGitHub 69dc5f326e docs: Add section for Automatic Deletion of LangGraph Cloud SaaS deployments (#3222) 2025-01-27 15:42:36 -08:00
90f1e66748 docs: functional api (#3125)
* Concepts page for the functional API
* How-to guides that show functional API implementations
* API reference for entrypoint, task, entrypoint.final
* Add functional API version to the workflows

---------

Co-authored-by: Vadym Barda <vadym@langchain.dev>
Co-authored-by: ccurme <chester.curme@gmail.com>
2025-01-27 16:31:43 -05:00
Vadym BardaandGitHub a910a1a341 langgraph: actually fix flaky test (#3219) 2025-01-27 16:06:31 -05:00
Vadym BardaandGitHub 554e763994 langgraph: use 'prompt' param for model input preprocessing in create_react_agent (#3173) 2025-01-27 19:03:01 +00:00
Vadym BardaandGitHub adff1df813 langgraph: fix flaky test (#3218) 2025-01-27 13:11:17 -05:00
Vadym BardaandGitHub 64b828def6 langgraph: handle task naming for reused class methods (#3216) 2025-01-27 17:05:39 +00:00
Jacob LeeandGitHub 9196cc2da8 docs: Fix broken links in README (#3211) 2025-01-27 09:15:17 -05:00
David DuongandGitHub e13a8bc320 fix: missing pgvector in CLI (#3165)
Fixes https://github.com/langchain-ai/langgraph/issues/3164
2025-01-25 04:27:42 +01:00
Tat Dat Duong 4b5b309152 Bump to 0.1.69 2025-01-25 04:18:18 +01:00
Tat Dat Duong 9d5166a0d3 Fix tests 2025-01-25 04:16:52 +01:00
Nuno CamposandGitHub 5b11683f4e Fix type hints for entrypoint decorator (#3198) 2025-01-24 15:07:21 -08:00
Vadym BardaandGitHub 3fae8d47c3 langgraph: handle more callable types in tasks (#3203) 2025-01-24 22:50:42 +00:00
Nuno Campos be9cb03dfa Lint 2025-01-24 13:51:16 -08:00
Vadym BardaandGitHub 6926c4bcc0 langgraph: add names for tasks (#3202) 2025-01-24 21:42:54 +00:00
Nuno Campos c04802a344 Fix type hints for entrypoint decorator 2025-01-24 09:31:27 -08:00
Nuno CamposandGitHub cf7f6691cf Improve typings for task, it now returns a mixed sync/async future protocol (#3186)
- note this type is never instantiated, it is only used for typing (we
cannot make it a protocol as it inherits from concurrent.futures.Future)
2025-01-24 09:19:51 -08:00
Vadym BardaandGitHub e1140f4fad ci: always test notebooks on all library versions (#3191) 2025-01-23 22:26:32 -05:00
Eugene YurtsevandGitHub ae8afb7677 reference: document entrypoint more (#3189)
add more documentation to the entrypoint primitive
2025-01-23 22:17:20 -05:00
Vadym BardaandGitHub 48040d8ea5 ci: run notebooks in 'development' for PRs (#3188) 2025-01-23 21:59:03 -05:00
Vadym BardaandGitHub ad51bfdf71 ci: only test changed notebooks using 'development' version of the library (#3187) 2025-01-23 21:41:47 -05:00
Nuno Campos 996b120613 Undo 2025-01-23 16:59:27 -08:00
Nuno Campos 1093dd55c8 Improve typings for task, it now returns a mixed sync/async future protocol
- note this type is never instantiated, it is only used for typing (we cannot make it a protocol as it inherits from concurrent.futures.Future)
2025-01-23 16:57:34 -08:00
Nuno CamposandGitHub d794875b32 Undo change to test watch mode (#3184) 2025-01-23 16:54:03 -08:00
Nuno Campos 6948cf5eb8 Undo change to test watch mode 2025-01-23 16:18:53 -08:00
Nuno CamposandGitHub bacc2955ae Remove implementation of get_graph for @entrypoint (#3183) 2025-01-23 15:48:22 -08:00
Nuno Campos f014d96d1c Remove implementation of get_graph for @entrypoint 2025-01-23 15:39:36 -08:00
RadiandGitHub 7b552ebf4b Update tool-calling.ipynb (#3178)
Typo fix
2025-01-23 18:02:43 -05:00
Vadym BardaandGitHub 1e61ddfdbe langgraph: release 0.2.67 (#3182) 2025-01-23 18:01:43 -05:00
Vadym BardaandGitHub d34846dc08 docs: fix a hub prompt (#3180) 2025-01-23 17:21:37 -05:00
Vadym BardaandGitHub 06823e327f langgraph: update docstrings/api ref for functional api (#3176) 2025-01-23 16:48:55 -05:00
Vadym BardaandGitHub 1059ef55d1 langgraph: handle node return annotations with unions (#3170) 2025-01-23 14:41:22 -05:00
39552255c8 langgraph: add support for BaseModel updates to Command (#2747)
Simple update that adds support for the `update` attribute of the
`Command` class to support Pydantic `BaseModel` type.

LangGraph already supports [Pydantic models for graph
states](https://langchain-ai.github.io/langgraph/how-tos/state-model/).

Extending support to the `update` attribute allows users to pass custom
BaseModel instances. Additionally, updates defined as `BaseModel` types
are type-validated when created.

https://github.com/langchain-ai/langgraph/issues/2804

---------

Co-authored-by: vbarda <vadym@langchain.dev>
2025-01-23 14:31:44 -05:00
Vadym BardaandGitHub 38bbe67469 langgraph: remove print (#3167) 2025-01-23 11:24:18 -05:00
Nuno CamposandGitHub 6335963674 Make scratchpad counters thread-safe (#3158)
- Same solution as used in python stdlib to name threads and asyncio
tasks
2025-01-23 07:37:04 -08:00
Nuno Campos 8a4c452317 Make test less flaky 2025-01-23 07:27:34 -08:00
Eugene YurtsevandGitHub 5dc5853161 Add entrypoint.final to decouple return value from save value (#3135)
* Introduce `entrypoint.final` that allows decoupling what's returned
from the state update.
* moving decorator to class object w/ call to support defining `final`
as a property on it -- this should play nicely w/ IDE tooling / type
checking.


```python
    previous_ = None

    @entrypoint(checkpointer=MemorySaver())
    def foo(msg: str, *, previous: Any) -> entrypoint.final[int, list[str]]:
        nonlocal previous_
        previous_ = previous
        previous = previous or []
        return entrypoint.final(value=len(previous), save=previous + [msg])

    assert foo.get_output_schema().model_json_schema() == {
        "title": "LangGraphOutput",
        "type": "integer",
    }

    config = {"configurable": {"thread_id": "1"}}
    assert foo.invoke("hello", config) == 0
    assert previous_ is None
    assert foo.invoke("goodbye", config) == 1
    assert previous_ == ["hello"]
    assert foo.invoke("definitely", config) == 2
    assert previous_ == ["hello", "goodbye"]

```
2025-01-23 10:18:23 -05:00
Vadym BardaandGitHub 9e066554ba langgraph: allow async state modifier in create_react_agent (#3161)
Fixes https://github.com/langchain-ai/langgraph/issues/2875
2025-01-23 09:29:25 -05:00
Tat Dat Duong 6ce9354ee4 fix: missing pgvector in CLI
Fixes https://github.com/langchain-ai/langgraph/issues/3164
2025-01-23 15:27:39 +01:00
Nuno Campos 211fd4337d Undo 2025-01-22 16:44:13 -08:00
Nuno Campos 44bf97ac0e Fix 2025-01-22 16:32:59 -08:00
Nuno Campos 23c73ae719 Update tests 2025-01-22 16:30:43 -08:00
Nuno CamposandGitHub 4165d479e9 langgraph: add test for interrupting multiple times from a task (#3148) 2025-01-22 16:25:03 -08:00
Nuno Campos c43a9a4bd0 Make scratchpad counters thread-safe
- Same solution as used in python stdlib to name threads and asyncio tasks
2025-01-22 16:07:59 -08:00
Nuno Campos c9613927dc Lint 2025-01-22 16:05:34 -08:00
Nuno Campos c697c2aa04 Undo 2025-01-22 16:02:09 -08:00
Nuno Campos 3f2557c9c9 Fix 2025-01-22 16:02:09 -08:00
Nuno Campos cbad17fa7d Fix 2025-01-22 16:02:09 -08:00
Nuno Campos 17dacb83a2 Fix 2025-01-22 16:02:09 -08:00
Nuno Campos 3a997be088 Fix 2025-01-22 16:02:09 -08:00
Chester CurmeandNuno Campos 020d10138d add test case 2025-01-22 16:02:09 -08:00
Vadym BardaandGitHub 303587c4ff docs: bring back image for concept doc (#3152) 2025-01-22 16:08:24 -05:00
Andrew NguonlyandGitHub 7d4e636313 docs: Add note about how to share Postgres instance for self-hosted deployments (#3139)
### Screenshot

![image](https://github.com/user-attachments/assets/1caa7581-9dda-4ffc-9fee-ffb0f72487ea)
2025-01-22 12:14:24 -08:00
Lance MartinandGitHub 86913caf89 Add agents and workflows overview (#3040) 2025-01-22 12:07:47 -08:00
Nuno CamposandGitHub 041faefe29 Remove write to RETURN channel for entrypoint func (#3149) 2025-01-22 11:46:14 -08:00
Nuno Campos b704cf30cc Lint 2025-01-22 11:34:30 -08:00
Nuno Campos 3915b44180 Format 2025-01-22 11:32:09 -08:00
Nuno Campos ac1407b23c Remove write to RETURN channel for entrypoint func 2025-01-22 09:44:52 -08:00
Vadym BardaandGitHub 44840aa23f docs: add missing api key to cross-thread persistence howto (#3141) 2025-01-21 21:24:01 -05:00
Vadym BardaandGitHub 51242e2a32 ci: pin codespell (#3140) 2025-01-21 21:22:23 -05:00
Vadym BardaandGitHub b358e2e7cd docs: fix typo in persistence howto (#3138) 2025-01-21 21:03:00 -05:00
Nuno CamposandGitHub 0d91ab1474 Disable recursing on runnables for task/entrypoint decorated funcs (#3136) 2025-01-21 15:47:51 -08:00
Nuno Campos 12ae297194 Disable recursing on runnables for task/entrypoint decorated funcs 2025-01-21 15:36:09 -08:00
Nuno Campos 0177565c6b Spell check 2025-01-21 15:14:00 -08:00
Bhavya DhimanandGitHub c48d495031 fix(requirements.txt): Set requirements path to posix expression so that it can work in windows OS as well. (#3101)
When I cloned langgraph example, I was not able to run the code because
of the requirements.txt file. The path was set to posix expression which
was not working in windows OS.
I have updated the path to posix expression so that it can work in
windows OS as well.

Try running `langgraph-example` in windows using the langgraph-cli in
windows OS. It was working for linux not in windows.
2025-01-21 14:30:13 -05:00
104 changed files with 7635 additions and 5458 deletions
+1 -1
View File
@@ -21,7 +21,7 @@
- name: Install Dependencies
run: |
pip install toml codespell jupytext
pip install toml codespell==2.3.0 jupytext
- name: Extract Ignore Words List
run: |
+7 -6
View File
@@ -21,10 +21,7 @@ LangGraph is inspired by [Pregel](https://research.google/pubs/pub37252/) and [A
### Why use LangGraph?
LangGraph provides fine-grained control over both the flow and state of your
agent applications. It implements a central
[persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/),
enabling features that are common to most agent architectures:
LangGraph powers [production-grade agents](https://www.langchain.com/built-with-langgraph), trusted by Linkedin, Uber, Klarna, GitLab, and many more. LangGraph provides fine-grained control over both the flow and state of your agent applications. It implements a central [persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/), enabling features that are common to most agent architectures:
- **Memory**: LangGraph persists arbitrary aspects of your application's state,
supporting memory of conversations and other updates within and across user
@@ -245,7 +242,7 @@ final_state["messages"][-1].content
We use <code>ChatAnthropic</code> as our LLM. <strong>NOTE:</strong> we need to make sure the model knows that it has these tools available to call. We can do this by converting the LangChain tools into the format for OpenAI tool calling using the <code>.bind_tools()</code> method.
</li>
<li>
We define the tools we want to use - a search tool in our case. It is really easy to create your own tools - see documentation here on how to do that <a href="https://python.langchain.com/docs/modules/agents/tools/custom_tools">here</a>.
We define the tools we want to use - a search tool in our case. It is really easy to create your own tools - see documentation here on how to do that <a href="https://python.langchain.com/docs/how_to/custom_tools/">here</a>.
</li>
</ul>
</details>
@@ -294,7 +291,7 @@ Then we define one normal and one conditional edge. Conditional edge means that
<ul>
<li>
When we compile the graph, we turn it into a LangChain
<a href="https://python.langchain.com/v0.2/docs/concepts/#runnable-interface">Runnable</a>,
<a href="https://python.langchain.com/docs/concepts/runnables/">Runnable</a>,
which automatically enables calling <code>.invoke()</code>, <code>.stream()</code> and <code>.batch()</code>
with your inputs
</li>
@@ -333,6 +330,10 @@ Then we define one normal and one conditional edge. Conditional edge means that
* [API Reference](https://langchain-ai.github.io/langgraph/reference/graphs/): Review important classes and methods, simple examples of how to use the graph and checkpointing APIs, higher-level prebuilt components and more.
* [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/#langgraph-platform): LangGraph Platform is a commercial solution for deploying agentic applications in production, built on the open-source LangGraph framework.
## Resources
* [Built with LangGraph](https://www.langchain.com/built-with-langgraph): Hear how industry leaders use LangGraph to ship powerful, production-ready AI applications.
## Contributing
For more information on how to contribute, see [here](https://github.com/langchain-ai/langgraph/blob/main/CONTRIBUTING.md).
+2 -2
View File
@@ -31,7 +31,7 @@ def request(self, method, url, body=None, headers=None):
The result of calling the parent request method.
"""
# Update the inner socket's timeout value to send the request.
# This only triggers if the connection is re-used.
# This only triggers if the connection is reused.
if getattr(self, "sock", None) is not None:
self.sock.settimeout(self.timeout)
@@ -90,4 +90,4 @@ def patch_urllib3():
return request(self, *args, **kwargs)
connection.HTTPConnection.request = new_request
_PATCHED = True
_PATCHED = True
+6 -11
View File
@@ -1,17 +1,11 @@
import importlib
import inspect
import logging
import os
import re
from typing import List, Literal, Optional
from typing_extensions import TypedDict
from functools import lru_cache
from typing import List, Literal, Optional
import nbformat
from nbconvert.preprocessors import Preprocessor
from typing_extensions import TypedDict
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@@ -52,6 +46,8 @@ MANUAL_API_REFERENCES_LANGGRAPH = [
(["langgraph.constants"], "langgraph.types", "Interrupt", "types"),
(["langgraph.constants"], "langgraph.types", "interrupt", "types"),
(["langgraph.constants"], "langgraph.types", "Command", "types"),
(["langgraph.func"], "langgraph.func", "entrypoint", "func"),
(["langgraph.func"], "langgraph.func", "task", "func"),
([], "langgraph.types", "RetryPolicy", "types"),
([], "langgraph.checkpoint.base", "Checkpoint", "checkpoints"),
([], "langgraph.checkpoint.base", "CheckpointMetadata", "checkpoints"),
@@ -88,8 +84,6 @@ _IMPORT_LANGCHAIN_RE = _make_regular_expression("langchain")
_IMPORT_LANGGRAPH_RE = _make_regular_expression("langgraph")
@lru_cache(maxsize=10_000)
def _get_full_module_name(module_path: str, class_name: str) -> Optional[str]:
"""Get full module name using inspect, with LRU cache to memoize results."""
@@ -109,6 +103,7 @@ def _get_full_module_name(module_path: str, class_name: str) -> Optional[str]:
logger.warning(f"API Reference: Failed to load for class {class_name}, {e}")
return None
def _get_doc_title(data: str, file_name: str) -> str:
try:
return re.findall(r"^#\s*(.*)", data, re.MULTILINE)[0]
@@ -287,4 +282,4 @@ def update_markdown_with_imports(markdown: str) -> str:
# Apply the replace_code_block function to all matches in the markdown
updated_markdown = code_block_pattern.sub(replace_code_block, markdown)
return updated_markdown
return updated_markdown
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
eNqNVVtMHFUYXnoleKnGSL01nG4qJHVnd2Z2C+z6oLBQ2yIXYZtKUcnZmcPOwMyc6cxZ2uVmhPqk0M5TE/WpLLtmxQKxalP1SUiqVmyaNkqjyIsxhgRjfCs2eGbZ5VJu3aeZ8//f/33//51/tjfZjgxTxlrOsKwRZECB0BfT6k0a6FQUmeRsQkVEwmK8rrYhNBg15KnnJUJ0M+DxQF12Q41IBtZlwS1g1dPOeVRkmjCCzHgYi7E7ObmdThWeaSa4DWmmMwA4lve5gDObRU+aOp0GVhB9ckZNZDhpVMBUikbsI0neD44WqSCMw85uF1jOhaYpm4TS3wc4IoNyHF4EBRUYFZEb1MgCAgQDFSECYji6HxzBp4EANXAUSEjR7TMaF2HspdUk6wg6LUFSZAI1BjSoIpr/pt0OFpFiR4U0I+NlDjEm1jREGAUSOkVnd1JCUKSjPheXsEmssTXDG4GCgHTCIE3AoqxFrE8jHbLuAiJqsWukBLte2h0r1YaQzkBFbkeJRZQ1CnVdkQVoxz2tlHs4o5khMR2tDafs1hhqgUasL8uyOjx1Me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4UhSAegjgyEwVBPhvSJsDDtwKyJvpJAQ535+A4x+oTX0EPIcrtIEGE9HhchM3udLOUjXKG3U7W5aZp2uFyvmAMRAaZMaopS4qKQ8Cg66ma0CdKBLrbmDLlFOmgnCjU1ej2x/AaCwJauEoygoCrMVkBvESzI951uBfdPQEeMAWop6taNlbU+byHNuH5SsL9cuZqnBYlKHKRSCoAFFQZfYjhJY1F41IBKWSrsaJFP+ghUPkcDocbsDY3S7rwSjSIctZu6G7AmLVpmkfcOxl9NEaVW8rsdsqyGhPocrcT1cm8QG9LZYb/8cJ/rNpxR4H5KUJ/16/vbDwlThJLxy4/eGjsn5aF31ulX261Vpyrfe3+XXsX3faTNp/6tcZL1sll266P7/74+32ptaW3HR6lnpk4t2vx2mV4ExZ8sEUbx8VFb5y8+vw7137/SXokPrX130t/fGg0OXXt1Hq9+PHzfMP7T2qDW99oZS+eK2wq/CrTOG6NXgq3PLLprcVr/+I4WxVPWt958a87qeOq7ed3/eDXh7esnHjO9Vnf8IWezxRctY2dSJ/+qKSJOOH/4eeeZPvCmrhg6bHJ4J1Pvby09g+WVRWX7ulfMTxy7+4g3n5V3/Nh4dk3fbcXP/u7Lw+c/i3+90fDpzEyNcK++Z87zvw35jjzRNknRQ+cqF/RPrLyXP/RwYqnWh87/8ALxwteOjauyncNBB5/+tVjV3sWfPeVK4+O/+ho166motd1dvmuHa+vvDwFOy//9BsfLbnnfTjufKW+deHTb28+6ftbD7l7avEF7pmOrQ7na7ef/1fbgevbrIs2Rnf0PbusefXe0XfvZF/905b94smPl4zX3FfLvBuurVzSt+qXF2vAmZdCBRPV7/3m3Nhg7wax7dqaD+7b/YUPdx55r6i9f0vxyskFqVPXmycTJ/voimWP3P3tL65YzH1+efzuK39eufDKEwd+1X5x+eJe4jv9w1NDP9trvRQhmy2T0W+ZVS0quH/Phbf3ohL/D1RMKFk=
+2 -2
View File
@@ -1,5 +1,5 @@
ERROR_FOUND=0
for file in $(find $1 -name "*.ipynb"); do
for file in $(find $1 -name "*.ipynb" | grep -v ".ipynb_checkpoints"); do
OUTPUT=$(cat "$file" | jupytext --from ipynb --to py:percent | codespell -)
if [ -n "$OUTPUT" ]; then
echo "Errors found in $file"
@@ -10,4 +10,4 @@ done
if [ "$ERROR_FOUND" -ne 0 ]; then
exit 1
fi
fi
+938
View File
@@ -0,0 +1,938 @@
# Functional API
!!! warning "Beta"
The Functional API is currently in **beta** and is subject to change. Please [report any issues](https://github.com/langchain-ai/langgraph/issues) or feedback to the LangGraph team.
## Overview
The **Functional API** allows you to add LangGraph's key features -- [persistence](./persistence.md), [memory](./memory.md), [human-in-the-loop](./human_in_the_loop.md), and [streaming](./streaming.md) — to your applications with minimal changes to your existing code.
It is designed to integrate these features into existing code that may use standard language primitives for branching and control flow, such as `if` statements, `for` loops, and function calls. Unlike many data orchestration frameworks that require restructuring code into an explicit pipeline or DAG, the Functional API allows you to incorporate these capabilities without enforcing a rigid execution model.
The Functional API uses two key building blocks:
- **`@entrypoint`** Marks a function as the starting point of a workflow, encapsulating logic and managing execution flow, including handling long-running tasks and interrupts.
- **`@task`** Represents a discrete unit of work, such as an API call or data processing step, that can be executed asynchronously within an entrypoint. Tasks return a future-like object that can be awaited or resolved synchronously.
This provides a minimal abstraction for building workflows with state management and streaming.
!!! tip
For users who prefer a more declarative approach, LangGraph's [Graph API](./low_level.md) allows you to define workflows using a Graph paradigm. Both APIs share the same underlying runtime, so you can use them together in the same application.
Please see the [Functional API vs. Graph API](#functional-api-vs-graph-api) section for a comparison of the two paradigms.
## Example
Below we demonstrate a simple application that writes an essay and [interrupts](human_in_the_loop.md) to request human review.
```python
from langgraph.func import entrypoint, task
from langgraph.types import interrupt
@task
def write_essay(topic: str) -> str:
"""Write an essay about the given topic."""
time.sleep(1) # A placeholder for a long-running task.
return f"An essay about topic: {topic}"
@entrypoint(checkpointer=MemorySaver())
def workflow(topic: str) -> dict:
"""A simple workflow that writes an essay and asks for a review."""
essay = write_essay("cat").result()
is_approved = interrupt({
# Any json-serializable payload provided to interrupt as argument.
# It will be surfaced on the client side as an Interrupt when streaming data
# from the workflow.
"essay": essay, # The essay we want reviewed.
# We can add any additional information that we need.
# For example, introduce a key called "action" with some instructions.
"action": "Please approve/reject the essay",
})
return {
"essay": essay, # The essay that was generated
"is_approved": is_approved, # Response from HIL
}
```
??? example "Detailed Explanation"
This workflow will write an essay about the topic "cat" and then pause to get a review from a human. The workflow can be interrupted for an indefinite amount of time until a review is provided.
When the workflow is resumed, it executes from the very start, but because the result of the `write_essay` task was already saved, the task result will be loaded from the checkpoint instead of being recomputed.
```python
import time
import uuid
from langgraph.func import entrypoint, task
from langgraph.types import interrupt
from langgraph.checkpoint.memory import MemorySaver
@task
def write_essay(topic: str) -> str:
"""Write an essay about the given topic."""
time.sleep(1) # This is a placeholder for a long-running task.
return f"An essay about topic: {topic}"
@entrypoint(checkpointer=MemorySaver())
def workflow(topic: str) -> dict:
"""A simple workflow that writes an essay and asks for a review."""
essay = write_essay("cat").result()
is_approved = interrupt({
# Any json-serializable payload provided to interrupt as argument.
# It will be surfaced on the client side as an Interrupt when streaming data
# from the workflow.
"essay": essay, # The essay we want reviewed.
# We can add any additional information that we need.
# For example, introduce a key called "action" with some instructions.
"action": "Please approve/reject the essay",
})
return {
"essay": essay, # The essay that was generated
"is_approved": is_approved, # Response from HIL
}
thread_id = str(uuid.uuid4())
config = {
"configurable": {
"thread_id": thread_id
}
}
for item in workflow.stream("cat", config):
print(item)
```
```pycon
{'write_essay': 'An essay about topic: cat'}
{'__interrupt__': (Interrupt(value={'essay': 'An essay about topic: cat', 'action': 'Please approve/reject the essay'}, resumable=True, ns=['workflow:f7b8508b-21c0-8b4c-5958-4e8de74d2684'], when='during'),)}
```
An essay has been written and is ready for review. Once the review is provided, we can resume the workflow:
```python
from langgraph.types import Command
# Get review from a user (e.g., via a UI)
# In this case, we're using a bool, but this can be any json-serializable value.
human_review = True
for item in workflow.stream(Command(resume=human_review), config):
print(item)
```
```pycon
{'workflow': {'essay': 'An essay about topic: cat', 'is_approved': False}}
```
The workflow has been completed and the review has been added to the essay.
## Entrypoint
The [`@entrypoint`][langgraph.func.entrypoint] decorator can be used to create a workflow from a function. It encapsulates workflow logic and manages execution flow, including handling *long-running tasks* and [interrupts](./low_level.md#interrupt).
### Definition
An **entrypoint** is defined by decorating a function with the `@entrypoint` decorator.
The function **must accept a single positional argument**, which serves as the workflow input. If you need to pass multiple pieces of data, use a dictionary as the input type for the first argument.
Decorating a function with an `entrypoint` produces a [`Pregel`][langgraph.pregel.Pregel.stream] instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing).
You will usually want to pass a **checkpointer** to the `@entrypoint` decorator to enable persistence and use features like **human-in-the-loop**.
=== "Sync"
```python
from langgraph.func import entrypoint
@entrypoint(checkpointer=checkpointer)
def my_workflow(some_input: dict) -> int:
# some logic that may involve long-running tasks like API calls,
# and may be interrupted for human-in-the-loop.
...
return result
```
=== "Async"
```python
from langgraph.func import entrypoint
@entrypoint(checkpointer=checkpointer)
async def my_workflow(some_input: dict) -> int:
# some logic that may involve long-running tasks like API calls,
# and may be interrupted for human-in-the-loop
...
return result
```
!!! important "Serialization"
The **inputs** and **outputs** of entrypoints must be JSON-serializable to support checkpointing. Please see the [serialization](#serialization) section for more details.
### Injectable Parameters
When declaring an `entrypoint`, you can request access to additional parameters that will be injected automatically at run time. These parameters include:
| Parameter | Description |
|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| **previous** | Access the the state associated with the previous `checkpoint` for the given thread. See [state management](#state-management). |
| **store** | An instance of [BaseStore][langgraph.store.base.BaseStore]. Useful for [long-term memory](#long-term-memory). |
| **writer** | For streaming custom data, to write custom data to the `custom` stream. Useful for [streaming custom data](#streaming-custom-data). |
| **config** | For accessing run time configuration. See [RunnableConfig](https://python.langchain.com/docs/concepts/runnables/#runnableconfig) for information. |
!!! important
Declare the parameters with the appropriate name and type annotation.
??? example "Requesting Injectable Parameters"
```python
from langchain_core.runnables import RunnableConfig
from langgraph.func import entrypoint
from langgraph.store.base import BaseStore
from langgraph.store.memory import InMemoryStore
in_memory_store = InMemoryStore(...) # An instance of InMemoryStore for long-term memory
@entrypoint(
checkpointer=checkpointer, # Specify the checkpointer
store=in_memory_store # Specify the store
)
def my_workflow(
some_input: dict, # The input (e.g., passed via `invoke`)
*,
previous: Any = None, # For short-term memory
store: BaseStore, # For long-term memory
writer: StreamWriter, # For streaming custom data
config: RunnableConfig # For accessing the configuration passed to the entrypoint
) -> ...:
```
### Executing
Using the [`@entrypoint`](#entrypoint) yields a [`Pregel`][langgraph.pregel.Pregel.stream] object that can be executed using the `invoke`, `ainvoke`, `stream`, and `astream` methods.
=== "Invoke"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
my_workflow.invoke(some_input, config) # Wait for the result synchronously
```
=== "Async Invoke"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
await my_workflow.ainvoke(some_input, config) # Await result asynchronously
```
=== "Stream"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
for chunk in my_workflow.stream(some_input, config):
print(chunk)
```
=== "Async Stream"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
async for chunk in my_workflow.astream(some_input, config):
print(chunk)
```
### Resuming
Resuming an execution after an [interrupt][langgraph.types.interrupt] can be done by passing a **resume** value to the [Command][langgraph.types.Command] primitive.
=== "Invoke"
```python
from langgraph.types import Command
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
my_workflow.invoke(Command(resume=some_resume_value), config)
```
=== "Async Invoke"
```python
from langgraph.types import Command
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
await my_workflow.ainvoke(Command(resume=some_resume_value), config)
```
=== "Stream"
```python
from langgraph.types import Command
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
for chunk in my_workflow.stream(Command(resume=some_resume_value), config):
print(chunk)
```
=== "Async Stream"
```python
from langgraph.types import Command
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
async for chunk in my_workflow.astream(Command(resume=some_resume_value), config):
print(chunk)
```
**Resuming after an error**
To resume after an error, run the `entrypoint` with a `None` and the same **thread id** (config).
This assumes that the underlying **error** has been resolved and execution can proceed successfully.
=== "Invoke"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
my_workflow.invoke(None, config)
```
=== "Async Invoke"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
await my_workflow.ainvoke(None, config)
```
=== "Stream"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
for chunk in my_workflow.stream(None, config):
print(chunk)
```
=== "Async Stream"
```python
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
async for chunk in my_workflow.astream(None, config):
print(chunk)
```
### State Management
When an `entrypoint` is defined with a `checkpointer`, it stores information between successive invocations on the same **thread id** in [checkpoints](persistence.md#checkpoints).
This allows accessing the state from the previous invocation using the `previous` parameter.
By default, the `previous` parameter is the return value of the previous invocation.
```python
@entrypoint(checkpointer=checkpointer)
def my_workflow(number: int, *, previous: Any = None) -> int:
previous = previous or 0
return number + previous
config = {
"configurable": {
"thread_id": "some_thread_id"
}
}
my_workflow.invoke(1, config) # 1 (previous was None)
my_workflow.invoke(2, config) # 3 (previous was 1 from the previous invocation)
```
#### `entrypoint.final`
[entrypoint.final][langgraph.func.entrypoint.final] is a special primitive that can be returned from an entrypoint and allows **decoupling** the value that is **saved in the checkpoint** from the **return value of the entrypoint**.
The first value is the return value of the entrypoint, and the second value is the value that will be saved in the checkpoint. The type annotation is `entrypoint.final[return_type, save_type]`.
```python
@entrypoint(checkpointer=checkpointer)
def my_workflow(number: int, *, previous: Any = None) -> entrypoint.final[int, int]:
previous = previous or 0
# This will return the previous value to the caller, saving
# 2 * number to the checkpoint, which will be used in the next invocation
# for the `previous` parameter.
return entrypoint.final(value=previous, save=2 * number)
config = {
"configurable": {
"thread_id": "1"
}
}
my_workflow.invoke(3, config) # 0 (previous was None)
my_workflow.invoke(1, config) # 6 (previous was 3 * 2 from the previous invocation)
```
## Task
A **task** represents a discrete unit of work, such as an API call or data processing step. It has two key characteristics:
* **Asynchronous Execution**: Tasks are designed to be executed asynchronously, allowing multiple operations to run concurrently without blocking.
* **Checkpointing**: Task results are saved to a checkpoint, enabling resumption of the workflow from the last saved state. (See [persistence](persistence.md) for more details).
### Definition
Tasks are defined using the `@task` decorator, which wraps a regular Python function.
```python
from langgraph.func import task
@task()
def slow_computation(input_value):
# Simulate a long-running operation
...
return result
```
!!! important "Serialization"
The **outputs** of tasks must be JSON-serializable to support checkpointing.
### Execution
**Tasks** can only be called from within an **entrypoint**, another **task**, or a [state graph node](./low_level.md#nodes).
Tasks *cannot* be called directly from the main application code.
When you call a **task**, it returns *immediately* with a future object. A future is a placeholder for a result that will be available later.
To obtain the result of a **task**, you can either wait for it synchronously (using `result()`) or await it asynchronously (using `await`).
=== "Synchronous Invocation"
```python
@entrypoint(checkpointer=checkpointer)
def my_workflow(some_input: int) -> int:
future = slow_computation(some_input)
return future.result() # Wait for the result synchronously
```
=== "Asynchronous Invocation"
```python
@entrypoint(checkpointer=checkpointer)
async def my_workflow(some_input: int) -> int:
return await slow_computation(some_input) # Await result asynchronously
```
## When to use a task
**Tasks** are useful in the following scenarios:
- **Checkpointing**: When you need to save the result of a long-running operation to a checkpoint, so you don't need to recompute it when resuming the workflow.
- **Human-in-the-loop**: If you're building a workflow that requires human intervention, you MUST use **tasks** to encapsulate any randomness (e.g., API calls) to ensure that the workflow can be resumed correctly. See the [determinism](#determinism) section for more details.
- **Parallel Execution**: For I/O-bound tasks, **tasks** enable parallel execution, allowing multiple operations to run concurrently without blocking (e.g., calling multiple APIs).
- **Observability**: Wrapping operations in **tasks** provides a way to track the progress of the workflow and monitor the execution of individual operations using [LangSmith](https://docs.smith.langchain.com/).
- **Retryable Work**: When work needs to be retried to handle failures or inconsistencies, **tasks** provide a way to encapsulate and manage the retry logic.
## Serialization
There are two key aspects to serialization in LangGraph:
1. `@entrypoint` inputs and outputs must be JSON-serializable.
2. `@task` outputs must be JSON-serializable.
These requirements are necessary for enabling checkpointing and workflow resumption. Use python primitives
like dictionaries, lists, strings, numbers, and booleans to ensure that your inputs and outputs are serializable.
Serialization ensures that workflow state, such as task results and intermediate values, can be reliably saved and restored. This is critical for enabling human-in-the-loop interactions, fault tolerance, and parallel execution.
Providing non-serializable inputs or outputs will result in a runtime error when a workflow is configured with a checkpointer.
## Determinism
To utilize features like **human-in-the-loop**, any randomness should be encapsulated inside of **tasks**. This guarantees that when execution is halted (e.g., for human in the loop) and then resumed, it will follow the same *sequence of steps*, even if **task** results are non-deterministic.
LangGraph achieves this behavior by persisting **task** and [**subgraph**](./low_level.md#subgraphs) results as they execute. A well-designed workflow ensures that resuming execution follows the *same sequence of steps*, allowing previously computed results to be retrieved correctly without having to re-execute them. This is particularly useful for long-running **tasks** or **tasks** with non-deterministic results, as it avoids repeating previously done work and allows resuming from essentially the same
While different runs of a workflow can produce different results, resuming a **specific** run should always follow the same sequence of recorded steps. This allows LangGraph to efficiently look up **task** and **subgraph** results that were executed prior to the graph being interrupted and avoid recomputing them.
## Idempotency
Idempotency ensures that running the same operation multiple times produces the same result. This helps prevent duplicate API calls and redundant processing if a step is rerun due to a failure. Always place API calls inside **tasks** functions for checkpointing, and design them to be idempotent in case of re-execution. Re-execution can occur if a **task** starts, but does not complete successfully. Then, if the workflow is resumed, the **task** will run again. Use idempotency keys or verify existing results to avoid duplication.
## Functional API vs. Graph API
The **Functional API** and the [Graph APIs (StateGraph)](./low_level.md#stategraph) provide two different paradigms to create applications with LangGraph. Here are some key differences:
- **Control flow**: The Functional API does not require thinking about graph structure. You can use standard Python constructs to define workflows. This will usually trim the amount of code you need to write.
- **State management**: The **GraphAPI** requires declaring a [**State**](./low_level.md#state) and may require defining [**reducers**](./low_level.md#reducers) to manage updates to the graph state. `@entrypoint` and `@tasks` do not require explicit state management as their state is scoped to the function and is not shared across functions.
- **Checkpointing**: Both APIs generate and use checkpoints. In the **Graph API** a new checkpoint is generated after every [superstep](./low_level.md). In the **Functional API**, when tasks are executed, their results are saved to an existing checkpoint associated with the given entrypoint instead of creating a new checkpoint.
- **Visualization**: The Graph API makes it easy to visualize the workflow as a graph which can be useful for debugging, understanding the workflow, and sharing with others. The Functional API does not support visualization as the graph is dynamically generated during runtime.
## Common Pitfalls
### Handling side effects
Encapsulate side effects (e.g., writing to a file, sending an email) in tasks to ensure they are not executed multiple times when resuming a workflow.
=== "Incorrect"
In this example, a side effect (writing to a file) is directly included in the workflow, so it will be executed a second time when resuming the workflow.
```python
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
# This code will be executed a second time when resuming the workflow.
# Which is likely not what you want.
# highlight-next-line
with open("output.txt", "w") as f:
# highlight-next-line
f.write("Side effect executed")
value = interrupt("question")
return value
```
=== "Correct"
In this example, the side effect is encapsulated in a task, ensuring consistent execution upon resumption.
```python
from langgraph.func import task
# highlight-next-line
@task
# highlight-next-line
def write_to_file():
with open("output.txt", "w") as f:
f.write("Side effect executed")
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
# The side effect is now encapsulated in a task.
write_to_file().result()
value = interrupt("question")
return value
```
### Non-deterministic control flow
Operations that might give different results each time (like getting current time or random numbers) should be encapsulated in tasks to ensure that on resume, the same result is returned.
* In a task: Get random number (5) → interrupt → resume → (returns 5 again) → ...
* Not in a task: Get random number (5) → interrupt → resume → get new random number (7) → ...
This is especially important when using **human-in-the-loop** workflows with multiple interrupts calls. LangGraph keeps a list
of resume values for each task/entrypoint. When an interrupt is encountered, it's matched with the corresponding resume value.
This matching is strictly **index-based**, so the order of the resume values should match the order of the interrupts.
If order of execution is not maintained when resuming, one `interrupt` call may be matched with the wrong `resume` value, leading to incorrect results.
Please read the section on [determinism](#determinism) for more details.
=== "Incorrect"
In this example, the workflow uses the current time to determine which task to execute. This is non-deterministic because the result of the workflow depends on the time at which it is executed.
```python
from langgraph.func import entrypoint
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
t0 = inputs["t0"]
# highlight-next-line
t1 = time.time()
delta_t = t1 - t0
if delta_t > 1:
result = slow_task(1).result()
value = interrupt("question")
else:
result = slow_task(2).result()
value = interrupt("question")
return {
"result": result,
"value": value
}
```
=== "Correct"
In this example, the workflow uses the input `t0` to determine which task to execute. This is deterministic because the result of the workflow depends only on the input.
```python
import time
from langgraph.func import task
# highlight-next-line
@task
# highlight-next-line
def get_time() -> float:
return time.time()
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
t0 = inputs["t0"]
# highlight-next-line
t1 = get_time().result()
delta_t = t1 - t0
if delta_t > 1:
result = slow_task(1).result()
value = interrupt("question")
else:
result = slow_task(2).result()
value = interrupt("question")
return {
"result": result,
"value": value
}
```
## Patterns
Below are a few simple patterns that show examples of **how to** use the **Functional API**.
When defining an `entrypoint`, input is restricted to the first argument of the function. To pass multiple inputs, you can use a dictionary.
```python
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
value = inputs["value"]
another_value = inputs["another_value"]
...
my_workflow.invoke({"value": 1, "another_value": 2})
```
### Parallel execution
Tasks can be executed in parallel by invoking them concurrently and waiting for the results. This is useful for improving performance in IO bound tasks (e.g., calling APIs for LLMs).
```python
@task
def add_one(number: int) -> int:
return number + 1
@entrypoint(checkpointer=checkpointer)
def graph(numbers: list[int]) -> list[str]:
futures = [add_one(i) for i in numbers]
return [f.result() for f in futures]
```
### Calling subgraphs
The **Functional API** and the [**Graph API**](./low_level.md) can be used together in the same application as they share the same underlying runtime.
```python
from langgraph.func import entrypoint
from langgraph.graph import StateGraph
builder = StateGraph()
...
some_graph = builder.compile()
@entrypoint()
def some_workflow(some_input: dict) -> int:
# Call a graph defined using the graph API
result_1 = some_graph.invoke(...)
# Call another graph defined using the graph API
result_2 = another_graph.invoke(...)
return {
"result_1": result_1,
"result_2": result_2
}
```
### Calling other entrypoints
You can call other **entrypoints** from within an **entrypoint** or a **task**.
```python
@entrypoint() # Will automatically use the checkpointer from the parent entrypoint
def some_other_workflow(inputs: dict) -> int:
return inputs["value"]
@entrypoint(checkpointer=checkpointer)
def my_workflow(inputs: dict) -> int:
value = some_other_workflow.invoke({"value": 1})
return value
```
### Streaming custom data
You can stream custom data from an **entrypoint** by using the `StreamWriter` type. This allows you to write custom data to the `custom` stream.
```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint, task
from langgraph.types import StreamWriter
@task
def add_one(x):
return x + 1
@task
def add_two(x):
return x + 2
checkpointer = MemorySaver()
@entrypoint(checkpointer=checkpointer)
def main(inputs, writer: StreamWriter) -> int:
"""A simple workflow that adds one and two to a number."""
writer("hello") # Write some data to the `custom` stream
add_one(inputs['number']).result() # Will write data to the `updates` stream
writer("world") # Write some more data to the `custom` stream
add_two(inputs['number']).result() # Will write data to the `updates` stream
return 5
config = {
"configurable": {
"thread_id": "1"
}
}
for chunk in main.stream({"number": 1}, stream_mode=["custom", "updates"], config=config):
print(chunk)
```
```pycon
('updates', {'add_one': 2})
('updates', {'add_two': 3})
('custom', 'hello')
('custom', 'world')
('updates', {'main': 5})
```
!!! important
The `writer` parameter is automatically injected at run time. It will only be injected if the
parameter name appears in the function signature with that *exact* name.
### Retry policy
```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint, task
from langgraph.types import RetryPolicy
attempts = 0
# Let's configure the RetryPolicy to retry on ValueError.
# The default RetryPolicy is optimized for retrying specific network errors.
retry_policy = RetryPolicy(retry_on=ValueError)
@task(retry=retry_policy)
def get_info():
global attempts
attempts += 1
if attempts < 2:
raise ValueError('Failure')
return "OK"
checkpointer = MemorySaver()
@entrypoint(checkpointer=checkpointer)
def main(inputs, writer):
return get_info().result()
config = {
"configurable": {
"thread_id": "1"
}
}
main.invoke({'any_input': 'foobar'}, config=config)
```
```pycon
'OK'
```
### Resuming after an error
```python
import time
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint, task
from langgraph.types import StreamWriter
# Global variable to track the number of attempts
attempts = 0
@task()
def get_info():
"""
Simulates a task that fails once before succeeding.
Raises an exception on the first attempt, then returns "OK" on subsequent tries.
"""
global attempts
attempts += 1
if attempts < 2:
raise ValueError("Failure") # Simulate a failure on the first attempt
return "OK"
# Initialize an in-memory checkpointer for persistence
checkpointer = MemorySaver()
@task
def slow_task():
"""
Simulates a slow-running task by introducing a 1-second delay.
"""
time.sleep(1)
return "Ran slow task."
@entrypoint(checkpointer=checkpointer)
def main(inputs, writer: StreamWriter):
"""
Main workflow function that runs the slow_task and get_info tasks sequentially.
Parameters:
- inputs: Dictionary containing workflow input values.
- writer: StreamWriter for streaming custom data.
The workflow first executes `slow_task` and then attempts to execute `get_info`,
which will fail on the first invocation.
"""
slow_task_result = slow_task().result() # Blocking call to slow_task
get_info().result() # Exception will be raised here on the first attempt
return slow_task_result
# Workflow execution configuration with a unique thread identifier
config = {
"configurable": {
"thread_id": "1" # Unique identifier to track workflow execution
}
}
# This invocation will take ~1 second due to the slow_task execution
try:
# First invocation will raise an exception due to the `get_info` task failing
main.invoke({'any_input': 'foobar'}, config=config)
except ValueError:
pass # Handle the failure gracefully
```
When we resume execution, we won't need to re-run the `slow_task` as its result is already saved in the checkpoint.
```python
main.invoke(None, config=config)
```
```pycon
'Ran slow task.'
```
### Human-in-the-loop
The functional API supports [human-in-the-loop](human_in_the_loop.md) workflows using the `interrupt` function and the `Command` primitive.
Please see the following examples for more details:
* [How to wait for user input (Functional API)](../how-tos/wait-user-input-functional.ipynb): Shows how to implement a simple human-in-the-loop workflow using the functional API.
* [How to review tool calls (Functional API)](../how-tos/review-tool-calls-functional.ipynb): Guide demonstrates how to implement human-in-the-loop workflows in a ReAct agent using the LangGraph Functional API.
### Short-term memory
[State management](#state-management) using the **previous** parameter and optionally using the `entrypoint.final` primitive can be used to implement [short term memory](memory.md).
Please see the following how-to guides for more details:
* [How to add thread-level persistence (functional API)](../how-tos/persistence-functional.ipynb): Shows how to add thread-level persistence to a functional API workflow and implements a simple chatbot.
### Long-term memory
[long-term memory](memory.md#long-term-memory) allows storing information across different **thread ids**. This could be useful for learning information
about a given user in one conversation and using it in another.
Please see the following how-to guides for more details:
* [How to add cross-thread persistence (functional API)](../how-tos/cross-thread-persistence-functional.ipynb): Shows how to add cross-thread persistence to a functional API workflow and implements a simple chatbot.
### Workflows
* [Workflows and agent](../tutorials/workflows/index.md) guide for more examples of how to build workflows using the Functional API.
### Agents
* [How to create a React agent from scratch (Functional API)](../how-tos/react-agent-from-scratch-functional.ipynb): Shows how to create a simple React agent from scratch using the functional API.
* [How to build a multi-agent network](../how-tos/multi-agent-network-functional.ipynb): Shows how to build a multi-agent network using the functional API.
* [How to add multi-turn conversation in a multi-agent application (functional API)](../how-tos/multi-agent-multi-turn-convo-functional.ipynb): allow an end-user to engage in a multi-turn conversation with one or more agents.
+13 -45
View File
@@ -1,58 +1,26 @@
# Why LangGraph?
LLMs are extremely powerful, particularly when connected to other systems such as a retriever or APIs. This is why many LLM applications use a control flow of steps before and / or after LLM calls. As an example [RAG](https://github.com/langchain-ai/rag-from-scratch) performs retrieval of relevant documents to a question, and passes those documents to an LLM in order to ground the response. Often a control flow of steps before and / or after an LLM is called a "chain." Chains are a popular paradigm for programming with LLMs and offer a high degree of reliability; the same set of steps runs with each chain invocation.
## LLM applications
However, we often want LLM systems that can pick their own control flow! This is one definition of an [agent](https://blog.langchain.dev/what-is-an-agent/): an agent is a system that uses an LLM to decide the control flow of an application. Unlike a chain, an agent gives an LLM some degree of control over the sequence of steps in the application. Examples of using an LLM to decide the control of an application:
LLMs make it possible to embed intelligence into a new class of applications. There are many patterns for building applications that use LLMs. [Workflows](https://www.anthropic.com/research/building-effective-agents) have scaffolding of predefined code paths around LLM calls. LLMs can direct the control flow through these predefined code paths, which some consider to be an "[agentic system](https://www.anthropic.com/research/building-effective-agents)". In other cases, it's possible to remove this scaffolding, creating autonomous agents that can [plan](https://huyenchip.com/2025/01/07/agents.html), take actions via [tool calls](https://python.langchain.com/docs/concepts/tool_calling/), and directly respond [to the feedback from their own actions](https://research.google/blog/react-synergizing-reasoning-and-acting-in-language-models/) with further actions.
- Using an LLM to route between two potential paths
- Using an LLM to decide which of many tools to call
- Using an LLM to decide whether the generated answer is sufficient or more work is need
![Agent Workflow](img/agent_workflow.png)
There are many different types of [agent architectures](https://blog.langchain.dev/what-is-a-cognitive-architecture/) to consider, which give an LLM varying levels of control. On one extreme, a router allows an LLM to select a single step from a specified set of options and, on the other extreme, a fully autonomous long-running agent may have complete freedom to select any sequence of steps that it wants for a given problem.
## What LangGraph provides
![Agent Types](img/agent_types.png)
LangGraph provides low-level supporting infrastructure that sits underneath *any* workflow or agent. It does not abstract prompts or architecture, and provides three central benefits:
Several concepts are utilized in many agent architectures:
### Persistence
- [Tool calling](agentic_concepts.md#tool-calling): this is often how LLMs make decisions
- Action taking: often times, the LLMs' outputs are used as the input to an action
- [Memory](agentic_concepts.md#memory): reliable systems need to have knowledge of things that occurred
- [Planning](agentic_concepts.md#planning): planning steps (either explicit or implicit) are useful for ensuring that the LLM, when making decisions, makes them in the highest fidelity way.
LangGraph has a [persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/), which offers a number of benefits:
## Challenges
- [Memory](https://langchain-ai.github.io/langgraph/concepts/memory/): LangGraph persists arbitrary aspects of your application's state, supporting memory of conversations and other updates within and across user interactions;
- [Human-in-the-loop](https://langchain-ai.github.io/langgraph/concepts/human_in_the_loop/): Because state is checkpointed, execution can be interrupted and resumed, allowing for decisions, validation, and corrections via human input.
In practice, there is often a trade-off between control and reliability. As we give LLMs more control, the application often become less reliable. This can be due to factors such as LLM non-determinism and / or errors in selecting tools (or steps) that the agent uses (takes).
### Streaming
![Agent Challenge](img/challenge.png)
LangGraph also provides support for [streaming](../how-tos/index.md#streaming) workflow / agent state to the user (or developer) over the course of execution. LangGraph supports streaming of both events ([such as feedback from a tool call](../how-tos/stream-updates.ipynb)) and [tokens from LLM calls](../how-tos/streaming-tokens.ipynb) embedded in an application.
## Core Principles
### Debugging and Deployment
The motivation of LangGraph is to help bend the curve, preserving higher reliability as we give the agent more control over the application. We'll outline a few specific pillars of LangGraph that make it well suited for building reliable agents.
![Langgraph](img/langgraph.png)
**Controllability**
LangGraph gives the developer a high degree of [control](../how-tos/index.md#controllability) by expressing the flow of the application as a set of nodes and edges. All nodes can access and modify a common state (memory). The control flow of the application can set using edges that connect nodes, either deterministically or via conditional logic.
**Persistence**
LangGraph gives the developer many options for [persisting](../how-tos/index.md#persistence) graph state using short-term or long-term (e.g., via a database) memory.
**Human-in-the-Loop**
The persistence layer enables several different [human-in-the-loop](../how-tos/index.md#human-in-the-loop) interaction patterns with agents; for example, it's possible to pause an agent, review its state, edit it state, and approve a follow-up step.
**Streaming**
LangGraph comes with first class support for [streaming](../how-tos/index.md#streaming), which can expose state to the user (or developer) over the course of agent execution. LangGraph supports streaming of both events ([like a tool call being taken](../how-tos/stream-updates.ipynb)) as well as of [tokens that an LLM may emit](../how-tos/streaming-tokens.ipynb).
## Debugging
Once you've built a graph, you often want to test and debug it. [LangGraph Studio](https://github.com/langchain-ai/langgraph-studio?tab=readme-ov-file) is a specialized IDE for visualization and debugging of LangGraph applications.
![Langgraph Studio](img/lg_studio.png)
## Deployment
Once you have confidence in your LangGraph application, many developers want an easy path to deployment. [LangGraph Platform](../concepts/index.md#langgraph-platform) offers a range of options for deploying LangGraph graphs.
LangGraph provides an easy onramp for testing, debugging, and deploying applications via [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/langgraph_platform/). This includes [Studio](https://langchain-ai.github.io/langgraph/concepts/langgraph_studio/), an IDE that enables visualization, interaction, and debugging of workflows or agents. This also includes numerous [options](https://langchain-ai.github.io/langgraph/tutorials/deployment/) for deployment.
Binary file not shown.

After

Width:  |  Height:  |  Size: 646 KiB

+1
View File
@@ -28,6 +28,7 @@ The conceptual guide does not cover step-by-step instructions or specific implem
- [Persistence](persistence.md): LangGraph has a built-in persistence layer, implemented through checkpointers. This persistence layer helps to support powerful capabilities like human-in-the-loop, memory, time travel, and fault-tolerance.
- [Memory](memory.md): Memory in AI applications refers to the ability to process, store, and effectively recall information from past interactions. With memory, your agents can learn from feedback and adapt to users' preferences.
- [Streaming](streaming.md): Streaming is crucial for enhancing the responsiveness of applications built on LLMs. By displaying output progressively, even before a complete response is ready, streaming significantly improves user experience (UX), particularly when dealing with the latency of LLMs.
- [Functional API (beta)](functional_api.md): An alternative to [Graph API (StateGraph)](low_level.md#stategraph) for development in LangGraph.
- [FAQ](faq.md): Frequently asked questions about LangGraph.
## LangGraph Platform
+22 -8
View File
@@ -21,6 +21,12 @@ Resource Allocation:
See the [how-to guide](../cloud/deployment/cloud.md#create-new-deployment) for creating a new deployment.
## Revision
A revision is an iteration of a [deployment](#deployment). When a new deployment is created, an initial revision is automatically created. To deploy new code changes or update environment variable configurations for a deployment, a new revision must be created. When a revision is created, a new container image is built automatically.
See the [how-to guide](../cloud/deployment/cloud.md#create-new-revision) for creating a new revision.
## Persistence
A dedicated database is automatically created for each deployment. The database serves as the [persistence layer](../concepts/persistence.md) for the deployment.
@@ -41,12 +47,6 @@ Scale down actions are delayed for 30 minutes before any action is taken. In oth
In the future, the autoscaling implementation may evolve to accommodate other metrics such as background run queue size.
## Revision
A revision is an iteration of a [deployment](#deployment). When a new deployment is created, an initial revision is automatically created. To deploy new code changes or update environment variable configurations for a deployment, a new revision must be created. When a revision is created, a new container image is built automatically.
See the [how-to guide](../cloud/deployment/cloud.md#create-new-revision) for creating a new revision.
## Asynchronous Deployment
Infrastructure for [deployments](#deployment) and [revisions](#revision) are provisioned and deployed asynchronously. They are not deployed immediately after submission. Currently, deployment can take up to several minutes.
@@ -55,12 +55,26 @@ Infrastructure for [deployments](#deployment) and [revisions](#revision) are pro
- When a subsequent revision is created for a deployment, there is no database creation step. The deployment time for a subsequent revision is significantly faster compared to the deployment time of the initial revision.
- The deployment process for each revision contains a build step, which can take up to a few minutes.
!!! info "Database creation for `Development` type deployments takes longer than database creation for `Production` type deployments."
## LangSmith Integration
A [LangSmith](https://docs.smith.langchain.com/) tracing project is automatically created for each deployemnt. The tracing project has the same name as the deployment. When creating a deployment, the `LANGCHAIN_TRACING_V2` and `LANGCHAIN_API_KEY` environment variables do not need to be specified; they are set internally, automatically. Traces are created for each run and are emitted to the tracing project automatically.
When a deployment is deleted, the traces and the tracing project are not deleted.
## Automatic Deletion
Deployments are automatically deleted after 28 consecutive days of non-use (it is in an unused state). A deployment is in an unused state if there are no traces emitted to LangSmith from the deployment after 28 consecutive days. On any given day, if a deployment emits a trace to LangSmith, the counter for consecutive days of non-use is reset.
- An email notification is sent after 7 consecutive days of non-use.
- A deployment is deleted after 28 consecutive days of non-use.
!!! danger "Data Cannot Be Recovered"
After a deployment is deleted, the data (i.e. [persistence](#persistence)) from the deployment cannot be recovered.
## Architecture
!!! warning "Subject to Change"
The Cloud SaaS deployment architecture may change in the future.
The Cloud SaaS deployment architecture may change in the future.
A high-level diagram of a Cloud SaaS deployment.
-4
View File
@@ -22,10 +22,6 @@ A super-step can be considered a single iteration over the graph nodes. Nodes th
The `StateGraph` class is the main graph class to use. This is parameterized by a user defined `State` object.
### MessageGraph
The `MessageGraph` class is a special type of graph. The `State` of a `MessageGraph` is ONLY a list of messages. This class is rarely used except for chatbots, as most applications require the `State` to be more complex than a list of messages.
### Compiling your graph
To build your graph, you first define the [state](#state), you then add [nodes](#nodes) and [edges](#edges), and then you compile it. What exactly is compiling your graph and why is it needed?
+2 -2
View File
@@ -828,13 +828,13 @@
"addition_expert = create_react_agent(\n",
" model,\n",
" [add, make_handoff_tool(agent_name=\"multiplication_expert\")],\n",
" state_modifier=\"You are an addition expert, you can ask the multiplication expert for help with multiplication.\",\n",
" prompt=\"You are an addition expert, you can ask the multiplication expert for help with multiplication.\",\n",
")\n",
"\n",
"multiplication_expert = create_react_agent(\n",
" model,\n",
" [multiply, make_handoff_tool(agent_name=\"addition_expert\")],\n",
" state_modifier=\"You are a multiplication expert, you can ask an addition expert for help with addition.\",\n",
" prompt=\"You are a multiplication expert, you can ask an addition expert for help with addition.\",\n",
")\n",
"\n",
"builder = StateGraph(MessagesState)\n",
+1 -1
View File
@@ -134,7 +134,7 @@
"model = ChatOpenAI(model=\"gpt-4o\")\n",
"tools = [TavilySearchResults(max_results=1)]\n",
"web_search_agent = create_react_agent(\n",
" model, tools, state_modifier=\"You are an agent specializing in web search\"\n",
" model, tools, prompt=\"You are an agent specializing in web search\"\n",
")"
]
},
@@ -39,7 +39,7 @@
"\n",
"This tutorial will show how to add a custom system prompt to the [prebuilt ReAct agent](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.chat_agent_executor.create_react_agent). Please see [this tutorial](../create-react-agent) for how to get started with the prebuilt ReAct agent\n",
"\n",
"You can add a custom system prompt by passing a string to the `state_modifier` param.\n"
"You can add a custom system prompt by passing a string to the `prompt` param.\n"
]
},
{
@@ -144,7 +144,7 @@
"\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"graph = create_react_agent(model, tools=tools, state_modifier=prompt)"
"graph = create_react_agent(model, tools=tools, prompt=prompt)"
]
},
{
@@ -0,0 +1,362 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "d2eecb96-cf0e-47ed-8116-88a7eaa4236d",
"metadata": {},
"source": [
"# How to add cross-thread persistence (functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
"\n",
" This guide assumes familiarity with the following:\n",
" \n",
" - [Functional API](../../concepts/functional_api/)\n",
" - [Persistence](../../concepts/persistence/)\n",
" - [Memory](../../concepts/memory/)\n",
" - [Chat Models](https://python.langchain.com/docs/concepts/chat_models/)\n",
"\n",
"LangGraph allows you to persist data across **different [threads](../../concepts/persistence/#threads)**. For instance, you can store information about users (their names or preferences) in a shared (cross-thread) memory and reuse them in the new threads (e.g., new conversations).\n",
"\n",
"When using the [functional API](../../concepts/functional_api/), you can set it up to store and retrieve memories by using the [Store](https://langchain-ai.github.io/langgraph/reference/store/#langgraph.store.base.BaseStore) interface:\n",
"\n",
"1. Create an instance of a `Store`\n",
"\n",
" ```python\n",
" from langgraph.store.memory import InMemoryStore, BaseStore\n",
" \n",
" store = InMemoryStore()\n",
" ```\n",
"\n",
"2. Pass the `store` instance to the `entrypoint()` decorator and expose `store` parameter in the function signature:\n",
"\n",
" ```python\n",
" from langgraph.func import entrypoint\n",
"\n",
" @entrypoint(store=store)\n",
" def workflow(inputs: dict, store: BaseStore):\n",
" my_task(inputs).result()\n",
" ...\n",
" ```\n",
" \n",
"In this guide, we will show how to construct and use a workflow that has a shared memory implemented using the [Store](https://langchain-ai.github.io/langgraph/reference/store/#langgraph.store.base.BaseStore) interface.\n",
"\n",
"!!! note Note\n",
"\n",
" Support for the [`Store`](https://langchain-ai.github.io/langgraph/reference/store/#langgraph.store.base.BaseStore) API that is used in this guide was added in LangGraph `v0.2.32`.\n",
"\n",
" Support for __index__ and __query__ arguments of the [`Store`](https://langchain-ai.github.io/langgraph/reference/store/#langgraph.store.base.BaseStore) API that is used in this guide was added in LangGraph `v0.2.54`.\n",
"\n",
"!!! tip \"Note\"\n",
"\n",
" If you need to add cross-thread persistence to a `StateGraph`, check out this [how-to guide](../cross-thread-persistence).\n",
"\n",
"## Setup\n",
"\n",
"First, let's install the required packages and set our API keys"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3457aadf",
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langchain_anthropic langchain_openai langgraph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa2c64a7",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"ANTHROPIC_API_KEY\")\n",
"_set_env(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"id": "51b6817d",
"metadata": {},
"source": [
"!!! tip \"Set up [LangSmith](https://smith.langchain.com) for LangGraph development\"\n",
"\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started [here](https://docs.smith.langchain.com)"
]
},
{
"cell_type": "markdown",
"id": "6b5b3d42-3d2c-455e-ac10-e2ae74dc1cf1",
"metadata": {},
"source": [
"## Example: simple chatbot with long-term memory"
]
},
{
"cell_type": "markdown",
"id": "c4c550b5-1954-496b-8b9d-800361af17dc",
"metadata": {},
"source": [
"### Define store\n",
"\n",
"In this example we will create a workflow that will be able to retrieve information about a user's preferences. We will do so by defining an `InMemoryStore` - an object that can store data in memory and query that data.\n",
"\n",
"When storing objects using the `Store` interface you define two things:\n",
"\n",
"* the namespace for the object, a tuple (similar to directories)\n",
"* the object key (similar to filenames)\n",
"\n",
"In our example, we'll be using `(\"memories\", <user_id>)` as namespace and random UUID as key for each new memory.\n",
"\n",
"Importantly, to determine the user, we will be passing `user_id` via the config keyword argument of the node function.\n",
"\n",
"Let's first define our store!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a7f303d6-612e-4e34-bf36-29d4ed25d802",
"metadata": {},
"outputs": [],
"source": [
"from langgraph.store.memory import InMemoryStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"in_memory_store = InMemoryStore(\n",
" index={\n",
" \"embed\": OpenAIEmbeddings(model=\"text-embedding-3-small\"),\n",
" \"dims\": 1536,\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "3389c9f4-226d-40c7-8bfc-ee8aac24f79d",
"metadata": {},
"source": [
"### Create workflow"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2a30a362-528c-45ee-9df6-630d2d843588",
"metadata": {},
"outputs": [],
"source": [
"import uuid\n",
"\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langchain_core.runnables import RunnableConfig\n",
"from langchain_core.messages import BaseMessage\n",
"from langgraph.func import entrypoint, task\n",
"from langgraph.graph import add_messages\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.store.base import BaseStore\n",
"\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\")\n",
"\n",
"\n",
"@task\n",
"def call_model(messages: list[BaseMessage], memory_store: BaseStore, user_id: str):\n",
" namespace = (\"memories\", user_id)\n",
" last_message = messages[-1]\n",
" memories = memory_store.search(namespace, query=str(last_message.content))\n",
" info = \"\\n\".join([d.value[\"data\"] for d in memories])\n",
" system_msg = f\"You are a helpful assistant talking to the user. User info: {info}\"\n",
"\n",
" # Store new memories if the user asks the model to remember\n",
" if \"remember\" in last_message.content.lower():\n",
" memory = \"User name is Bob\"\n",
" memory_store.put(namespace, str(uuid.uuid4()), {\"data\": memory})\n",
"\n",
" response = model.invoke([{\"role\": \"system\", \"content\": system_msg}] + messages)\n",
" return response\n",
"\n",
"\n",
"# NOTE: we're passing the store object here when creating a workflow via entrypoint()\n",
"@entrypoint(checkpointer=MemorySaver(), store=in_memory_store)\n",
"def workflow(\n",
" inputs: list[BaseMessage],\n",
" *,\n",
" previous: list[BaseMessage],\n",
" config: RunnableConfig,\n",
" store: BaseStore,\n",
"):\n",
" user_id = config[\"configurable\"][\"user_id\"]\n",
" previous = previous or []\n",
" inputs = add_messages(previous, inputs)\n",
" response = call_model(inputs, store, user_id).result()\n",
" return entrypoint.final(value=response, save=add_messages(inputs, response))"
]
},
{
"cell_type": "markdown",
"id": "f22a4a18-67e4-4f0b-b655-a29bbe202e1c",
"metadata": {},
"source": [
"!!! note Note\n",
"\n",
" If you're using LangGraph Cloud or LangGraph Studio, you __don't need__ to pass store to the entrypoint decorator, since it's done automatically."
]
},
{
"cell_type": "markdown",
"id": "552d4e33-556d-4fa5-8094-2a076bc21529",
"metadata": {},
"source": [
"### Run the workflow!"
]
},
{
"cell_type": "markdown",
"id": "1842c626-6cd9-4f58-b549-58978e478098",
"metadata": {},
"source": [
"Now let's specify a user ID in the config and tell the model our name:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c871a073-a466-46ad-aafe-2b870831057e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Hello Bob! Nice to meet you. I'll remember that your name is Bob. How can I help you today?\n"
]
}
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\", \"user_id\": \"1\"}}\n",
"input_message = {\"role\": \"user\", \"content\": \"Hi! Remember: my name is Bob\"}\n",
"for chunk in workflow.stream([input_message], config, stream_mode=\"values\"):\n",
" chunk.pretty_print()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d862be40-1f8a-4057-81c4-b7bf073dc4c1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Your name is Bob.\n"
]
}
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"2\", \"user_id\": \"1\"}}\n",
"input_message = {\"role\": \"user\", \"content\": \"what is my name?\"}\n",
"for chunk in workflow.stream([input_message], config, stream_mode=\"values\"):\n",
" chunk.pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "80fd01ec-f135-4811-8743-daff8daea422",
"metadata": {},
"source": [
"We can now inspect our in-memory store and verify that we have in fact saved the memories for the user:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "76cde493-89cf-4709-a339-207d2b7e9ea7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': 'User name is Bob'}\n"
]
}
],
"source": [
"for memory in in_memory_store.search((\"memories\", \"1\")):\n",
" print(memory.value)"
]
},
{
"cell_type": "markdown",
"id": "23f5d7eb-af23-4131-b8fd-2a69e74e6e55",
"metadata": {},
"source": [
"Let's now run the workflow for another user to verify that the memories about the first user are self contained:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d362350b-d730-48bd-9652-983812fd7811",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"I don't have any information about your name. I can only see our current conversation without any prior context or personal details about you. If you'd like me to know your name, feel free to tell me!\n"
]
}
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"3\", \"user_id\": \"2\"}}\n",
"input_message = {\"role\": \"user\", \"content\": \"what is my name?\"}\n",
"for chunk in workflow.stream([input_message], config, stream_mode=\"values\"):\n",
" chunk.pretty_print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -64,18 +64,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "aa2c64a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ANTHROPIC_API_KEY: ········\n"
]
}
],
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
@@ -86,7 +78,8 @@
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"ANTHROPIC_API_KEY\")"
"_set_env(\"ANTHROPIC_API_KEY\")\n",
"_set_env(\"OPENAI_API_KEY\")"
]
},
{
@@ -183,7 +176,7 @@
" store.put(namespace, str(uuid.uuid4()), {\"data\": memory})\n",
"\n",
" response = model.invoke(\n",
" [{\"type\": \"system\", \"content\": system_msg}] + state[\"messages\"]\n",
" [{\"role\": \"system\", \"content\": system_msg}] + state[\"messages\"]\n",
" )\n",
" return {\"messages\": response}\n",
"\n",
@@ -247,7 +240,7 @@
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\", \"user_id\": \"1\"}}\n",
"input_message = {\"type\": \"user\", \"content\": \"Hi! Remember: my name is Bob\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"Hi! Remember: my name is Bob\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -273,7 +266,7 @@
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"2\", \"user_id\": \"1\"}}\n",
"input_message = {\"type\": \"user\", \"content\": \"what is my name?\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"what is my name?\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -334,7 +327,7 @@
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"3\", \"user_id\": \"2\"}}\n",
"input_message = {\"type\": \"user\", \"content\": \"what is my name?\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"what is my name?\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -356,7 +349,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
}
},
"nbformat": 4,
+14 -2
View File
@@ -25,8 +25,20 @@ If you would like to deploy LangGraph Cloud on Kubernetes, you can use this [Hel
You will eventually need to pass in the following environment variables to the LangGraph Deploy server:
- `REDIS_URI`: Connection details to a Redis instance. Redis will be used as a pub-sub broker to enable streaming real time output from background runs.
- `DATABASE_URI`: Postgres connection details. Postgres will be used to store assistants, threads, runs, persist thread state and long term memory, and to manage the state of the background task queue with 'exactly once' semantics.
- `REDIS_URI`: Connection details to a Redis instance. Redis will be used as a pub-sub broker to enable streaming real time output from background runs. The value of `REDIS_URI` must be a valid [Redis connection URI](https://redis-py.readthedocs.io/en/stable/connections.html#redis.Redis.from_url).
!!! Note "Shared Redis Instance"
Multiple self-hosted deployments can share the same Redis instance. For example, for `Deployment A`, `REDIS_URI` can be set to `redis://<hostname_1>:<port>/1` and for `Deployment B`, `REDIS_URI` can be set to `redis://<hostname_1>:<port>/2`.
`1` and `2` are different database numbers within the same instance, but `<hostname_1>` is shared. **The same database number cannot be used for separate deployments**.
- `DATABASE_URI`: Postgres connection details. Postgres will be used to store assistants, threads, runs, persist thread state and long term memory, and to manage the state of the background task queue with 'exactly once' semantics. The value of `DATABASE_URI` must be a valid [Postgres connection URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS).
!!! Note "Shared Postgres Instance"
Multiple self-hosted deployments can share the same Postgres instance. For example, for `Deployment A`, `DATABASE_URI` can be set to `postgres://<user>:<password>@/<database_name_1>?host=<hostname_1>` and for `Deployment B`, `DATABASE_URI` can be set to `postgres://<user>:<password>@/<database_name_2>?host=<hostname_1>`.
`<database_name_1>` and `database_name_2` are different databases within the same instance, but `<hostname_1>` is shared. **The same database cannot be used for separate deployments**.
- `LANGSMITH_API_KEY`: (If using [Self-Hosted Lite](../concepts/deployment_options.md#self-hosted-lite)) LangSmith API key. This will be used to authenticate ONCE at server start up.
- `LANGGRAPH_CLOUD_LICENSE_KEY`: (If using [Self-Hosted Enterprise](../concepts/deployment_options.md#self-hosted-enterprise)) LangGraph Platform license key. This will be used to authenticate ONCE at server start up.
- `LANGCHAIN_ENDPOINT`: To send traces to a [self-hosted LangSmith](https://docs.smith.langchain.com/self_hosting) instance, set `LANGCHAIN_ENDPOINT` to the hostname of the self-hosted LangSmith instance.
+23
View File
@@ -31,6 +31,12 @@ These how-to guides show how to achieve that controllability.
- [How to use MongoDB checkpointer for persistence](persistence_mongodb.ipynb)
- [How to create a custom checkpointer using Redis](persistence_redis.ipynb)
See the below guides for how-to add persistence to your workflow using the (beta)
[Functional API](../concepts/functional_api.md):
- [How to add thread-level persistence (functional API)](persistence-functional.ipynb)
- [How to add cross-thread persistence (functional API)](cross-thread-persistence-functional.ipynb)
### Memory
LangGraph makes it easy to manage conversation [memory](../concepts/memory.md) in your graph. These how-to guides show how to implement different strategies for that.
@@ -59,6 +65,12 @@ Other methods:
- [How to edit graph state](human_in_the_loop/edit-graph-state.ipynb): Edit graph state using `graph.update_state` method. Use this if implementing a **human-in-the-loop** workflow via **static breakpoints**.
- [How to add dynamic breakpoints with `NodeInterrupt`](human_in_the_loop/dynamic_breakpoints.ipynb): **Not recommended**: Use the [`interrupt` function](../concepts/human_in_the_loop.md) instead.
See the below guides for how-to implement human-in-the-loop workflows with the (beta)
[Functional API](../concepts/functional_api.md):
- [How to wait for user input (Functional API)](wait-user-input-functional.ipynb)
- [How to review tool calls (Functional API)](review-tool-calls-functional.ipynb)
### Time Travel
[Time travel](../concepts/time-travel.md) allows you to replay past actions in your LangGraph application to explore alternative paths and debug issues. These how-to guides show how to use time travel in your graph.
@@ -115,6 +127,12 @@ These how-to guides show common patterns for tool calling with LangGraph:
See the [multi-agent tutorials](../tutorials/index.md#multi-agent-systems) for implementations of other multi-agent architectures.
See the below guides for how-to implement multi-agent workflows with the (beta)
[Functional API](../concepts/functional_api.md):
- [How to build a multi-agent network (functional API)](multi-agent-network-functional.ipynb)
- [How to add multi-turn conversation in a multi-agent application (functional API)](multi-agent-multi-turn-convo-functional.ipynb)
### State Management
- [How to use Pydantic model as graph state](state-model.ipynb)
@@ -152,6 +170,11 @@ overview of its underlying implementation to help you customize for your own nee
- [How to create prebuilt ReAct agent from scratch](react-agent-from-scratch.ipynb)
See the below guide for how-to build ReAct agents with the (beta)
[Functional API](../concepts/functional_api.md):
- [How to create a ReAct agent from scratch (Functional API)](react-agent-from-scratch-functional.ipynb)
## LangGraph Platform
This section includes how-to guides for LangGraph Platform.
@@ -195,7 +195,7 @@
"source": [
"## Using in `create_react_agent`\n",
"\n",
"Add semantic search to your tool calling agent by injecting the store in the `state_modifier`. You can also use the store in a tool to let your agent manually store or search for memories."
"Add semantic search to your tool calling agent by injecting the store in the `prompt` function. You can also use the store in a tool to let your agent manually store or search for memories."
]
},
{
@@ -248,9 +248,9 @@
"agent = create_react_agent(\n",
" init_chat_model(\"openai:gpt-4o-mini\"),\n",
" tools=[upsert_memory],\n",
" # The state_modifier is run to prepare the messages for the LLM. It is called\n",
" # The 'prompt' function is run to prepare the messages for the LLM. It is called\n",
" # right before each LLM call\n",
" state_modifier=prepare_messages,\n",
" prompt=prepare_messages,\n",
" store=store,\n",
")"
]
@@ -524,7 +524,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
"version": "3.12.3"
}
},
"nbformat": 4,
@@ -0,0 +1,462 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "a2b182eb-1e31-43c8-85b1-706508dfa370",
"metadata": {},
"source": [
"# How to add multi-turn conversation in a multi-agent application (functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
" This guide assumes familiarity with the following:\n",
"\n",
" - [Multi-agent systems](../../concepts/multi_agent)\n",
" - [Human-in-the-loop](../../concepts/human_in_the_loop)\n",
" - [Functional API](../../concepts/functional_api)\n",
" - [Command](../../concepts/low_level/#command)\n",
" - [LangGraph Glossary](../../concepts/low_level/)\n",
"\n",
"\n",
"In this how-to guide, well build an application that allows an end-user to engage in a *multi-turn conversation* with one or more agents. We'll create a node that uses an [`interrupt`](../../reference/types/#langgraph.types.interrupt) to collect user input and routes back to the **active** agent.\n",
"\n",
"The agents will be implemented as tasks in a workflow that executes agent steps and determines the next action:\n",
"\n",
"1. **Wait for user input** to continue the conversation, or\n",
"2. **Route to another agent** (or back to itself, such as in a loop) via a [**handoff**](../../concepts/multi_agent/#handoffs).\n",
"\n",
"```python\n",
"from langgraph.func import entrypoint, task\n",
"from langgraph.prebuilt import create_react_agent\n",
"from langchain_core.tools import tool\n",
"from langgraph.types import interrupt\n",
"\n",
"\n",
"# Define a tool to signal intent to hand off to a different agent\n",
"# Note: this is not using Command(goto) syntax for navigating to different agents:\n",
"# `workflow()` below handles the handoffs explicitly\n",
"@tool(return_direct=True)\n",
"def transfer_to_hotel_advisor():\n",
" \"\"\"Ask hotel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to hotel advisor\"\n",
"\n",
"\n",
"# define an agent\n",
"travel_advisor_tools = [transfer_to_hotel_advisor, ...]\n",
"travel_advisor = create_react_agent(model, travel_advisor_tools)\n",
"\n",
"\n",
"# define a task that calls an agent\n",
"@task\n",
"def call_travel_advisor(messages):\n",
" response = travel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"# define the multi-agent network workflow\n",
"@entrypoint(checkpointer)\n",
"def workflow(messages):\n",
" call_active_agent = call_travel_advisor\n",
" while True:\n",
" agent_messages = call_active_agent(messages).result()\n",
" ai_msg = get_last_ai_msg(agent_messages)\n",
" if not ai_msg.tool_calls:\n",
" user_input = interrupt(value=\"Ready for user input.\")\n",
" messages = messages + [{\"role\": \"user\", \"content\": user_input}]\n",
" continue\n",
"\n",
" messages = messages + agent_messages\n",
" call_active_agent = get_next_agent(messages)\n",
" return entrypoint.final(value=agent_messages[-1], save=messages)\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "faaa4444-cd06-4813-b9ca-c9700fe12cb7",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"First, let's install the required packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "05038da0-31df-4066-a1a4-c4ccb5db4d3a",
"metadata": {},
"outputs": [],
"source": [
"# %%capture --no-stderr\n",
"# %pip install -U langgraph langchain-anthropic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0bcff5d4-130e-426d-9285-40d0f72c7cd3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"ANTHROPIC_API_KEY: ········\n"
]
}
],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"ANTHROPIC_API_KEY\")"
]
},
{
"cell_type": "markdown",
"id": "c3ec6e48-85dc-4905-ba50-985e5d4788e6",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for LangGraph development</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started <a href=\"https://docs.smith.langchain.com\">here</a>. \n",
" </p>\n",
"</div>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c217c3fe-ca50-45a1-be91-912bc83ed8b3",
"metadata": {},
"source": [
"In this example we will build a team of travel assistant agents that can communicate with each other.\n",
"\n",
"We will create 2 agents:\n",
"\n",
"* `travel_advisor`: can help with travel destination recommendations. Can ask `hotel_advisor` for help.\n",
"* `hotel_advisor`: can help with hotel recommendations. Can ask `travel_advisor` for help.\n",
"\n",
"This is a fully-connected network - every agent can talk to any other agent. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "eb51463a-4425-44ad-91d5-f21fd5b4e3b3",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"from typing_extensions import Literal\n",
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def get_travel_recommendations():\n",
" \"\"\"Get recommendation for travel destinations\"\"\"\n",
" return random.choice([\"aruba\", \"turks and caicos\"])\n",
"\n",
"\n",
"@tool\n",
"def get_hotel_recommendations(location: Literal[\"aruba\", \"turks and caicos\"]):\n",
" \"\"\"Get hotel recommendations for a given destination.\"\"\"\n",
" return {\n",
" \"aruba\": [\n",
" \"The Ritz-Carlton, Aruba (Palm Beach)\"\n",
" \"Bucuti & Tara Beach Resort (Eagle Beach)\"\n",
" ],\n",
" \"turks and caicos\": [\"Grace Bay Club\", \"COMO Parrot Cay\"],\n",
" }[location]\n",
"\n",
"\n",
"@tool(return_direct=True)\n",
"def transfer_to_hotel_advisor():\n",
" \"\"\"Ask hotel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to hotel advisor\"\n",
"\n",
"\n",
"@tool(return_direct=True)\n",
"def transfer_to_travel_advisor():\n",
" \"\"\"Ask travel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to travel advisor\""
]
},
{
"cell_type": "markdown",
"id": "7f5b2a7f",
"metadata": {},
"source": [
"!!! note \"Transfer tools\"\n",
"\n",
" You might have noticed that we're using `@tool(return_direct=True)` in the transfer tools. This is done so that individual agents (e.g., `travel_advisor`) can exit the ReAct loop early once these tools are called. This is the desired behavior, as we want to detect when the agent calls this tool and hand control off _immediately_ to a different agent. \n",
" \n",
" **NOTE**: This is meant to work with the prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] -- if you are building a custom agent, make sure to manually add logic for handling early exit for tools that are marked with `return_direct`."
]
},
{
"cell_type": "markdown",
"id": "213d661e-6ba4-42b9-bc7f-6c8c423e3419",
"metadata": {},
"source": [
"Let's now create our agents using the the prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] and our multi-agent workflow. Note that will be calling [`interrupt`][langgraph.types.interrupt] every time after we get the final response from each of the agents."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "aa4bdbff-9461-46cc-aee9-8a22d3c3d9ec",
"metadata": {},
"outputs": [],
"source": [
"import uuid\n",
"\n",
"from langchain_core.messages import AIMessage\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langgraph.prebuilt import create_react_agent\n",
"from langgraph.graph import add_messages\n",
"from langgraph.func import entrypoint, task\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.types import interrupt, Command\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\")\n",
"\n",
"# Define travel advisor ReAct agent\n",
"travel_advisor_tools = [\n",
" get_travel_recommendations,\n",
" transfer_to_hotel_advisor,\n",
"]\n",
"travel_advisor = create_react_agent(\n",
" model,\n",
" travel_advisor_tools,\n",
" state_modifier=(\n",
" \"You are a general travel expert that can recommend travel destinations (e.g. countries, cities, etc). \"\n",
" \"If you need hotel recommendations, ask 'hotel_advisor' for help. \"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
" ),\n",
")\n",
"\n",
"\n",
"@task\n",
"def call_travel_advisor(messages):\n",
" # You can also add additional logic like changing the input to the agent / output from the agent, etc.\n",
" # NOTE: we're invoking the ReAct agent with the full history of messages in the state\n",
" response = travel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"# Define hotel advisor ReAct agent\n",
"hotel_advisor_tools = [get_hotel_recommendations, transfer_to_travel_advisor]\n",
"hotel_advisor = create_react_agent(\n",
" model,\n",
" hotel_advisor_tools,\n",
" state_modifier=(\n",
" \"You are a hotel expert that can provide hotel recommendations for a given destination. \"\n",
" \"If you need help picking travel destinations, ask 'travel_advisor' for help.\"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
" ),\n",
")\n",
"\n",
"\n",
"@task\n",
"def call_hotel_advisor(messages):\n",
" response = hotel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"def string_to_uuid(input_string):\n",
" return str(uuid.uuid5(uuid.NAMESPACE_URL, input_string))\n",
"\n",
"\n",
"@entrypoint(checkpointer=checkpointer)\n",
"def multi_turn_graph(messages, previous):\n",
" previous = previous or []\n",
" messages = add_messages(previous, messages)\n",
" call_active_agent = call_travel_advisor\n",
" while True:\n",
" agent_messages = call_active_agent(messages).result()\n",
" messages = add_messages(messages, agent_messages)\n",
" # Find the last AI message\n",
" # If one of the handoff tools is called, the last message returned\n",
" # by the agent will be a ToolMessage because we set them to have\n",
" # \"return_direct=True\". This means that the last AIMessage will\n",
" # have tool calls.\n",
" # Otherwise, the last returned message will be an AIMessage with\n",
" # no tool calls, which means we are ready for new input.\n",
" ai_msg = next(m for m in reversed(agent_messages) if isinstance(m, AIMessage))\n",
" if not ai_msg.tool_calls:\n",
" user_input = interrupt(value=\"Ready for user input.\")\n",
" # Add user input as a human message\n",
" # NOTE: we generate unique ID for the human message based on its content\n",
" # it's important, since on subsequent invocations previous user input (interrupt) values\n",
" # will be looked up again and we will attempt to add them again here\n",
" # `add_messages` deduplicates messages based on the ID, ensuring correct message history\n",
" human_message = {\n",
" \"role\": \"user\",\n",
" \"content\": user_input,\n",
" \"id\": string_to_uuid(user_input),\n",
" }\n",
" messages = add_messages(messages, [human_message])\n",
" continue\n",
"\n",
" tool_call = ai_msg.tool_calls[-1]\n",
" if tool_call[\"name\"] == \"transfer_to_hotel_advisor\":\n",
" call_active_agent = call_hotel_advisor\n",
" elif tool_call[\"name\"] == \"transfer_to_travel_advisor\":\n",
" call_active_agent = call_travel_advisor\n",
" else:\n",
" raise ValueError(f\"Expected transfer tool, got '{tool_call['name']}'\")\n",
"\n",
" return entrypoint.final(value=agent_messages[-1], save=messages)"
]
},
{
"cell_type": "markdown",
"id": "af856e1b-41fc-4041-8cbf-3818a60088e0",
"metadata": {},
"source": [
"## Test multi-turn conversation\n",
"\n",
"Let's test a multi turn conversation with this application."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2b6fde57-86e3-440e-a7bf-f1e9b5ed9ff2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Conversation Turn 1 ---\n",
"\n",
"User: {'role': 'user', 'content': 'i wanna go somewhere warm in the caribbean', 'id': 'f48d82a7-7efa-43f5-ad4c-541758c95f61'}\n",
"\n",
"call_travel_advisor: Based on the recommendations, Aruba would be an excellent choice for your Caribbean getaway! Known as \"One Happy Island,\" Aruba offers:\n",
"- Year-round warm weather with consistent temperatures around 82°F (28°C)\n",
"- Beautiful white sand beaches like Eagle Beach and Palm Beach\n",
"- Crystal clear waters perfect for swimming and snorkeling\n",
"- Minimal rainfall and location outside the hurricane belt\n",
"- Rich culture blending Dutch and Caribbean influences\n",
"- Various activities from water sports to desert-like landscape exploration\n",
"- Excellent dining and shopping options\n",
"\n",
"Would you like me to help you find suitable accommodations in Aruba? I can transfer you to our hotel advisor who can recommend specific hotels based on your preferences.\n",
"\n",
"--- Conversation Turn 2 ---\n",
"\n",
"User: Command(resume='could you recommend a nice hotel in one of the areas and tell me which area it is.')\n",
"\n",
"call_hotel_advisor: I can recommend two excellent options in different areas:\n",
"\n",
"1. The Ritz-Carlton, Aruba - Located in Palm Beach\n",
"- Luxury beachfront resort\n",
"- Located in the vibrant Palm Beach area, known for its lively atmosphere\n",
"- Close to restaurants, shopping, and nightlife\n",
"- Perfect for those who want a more active vacation with plenty of amenities nearby\n",
"\n",
"2. Bucuti & Tara Beach Resort - Located in Eagle Beach\n",
"- Adults-only boutique resort\n",
"- Situated on the quieter Eagle Beach\n",
"- Known for its romantic atmosphere and excellent service\n",
"- Ideal for couples seeking a more peaceful, intimate setting\n",
"\n",
"Would you like more specific information about either of these properties or their locations?\n",
"\n",
"--- Conversation Turn 3 ---\n",
"\n",
"User: Command(resume='i like the first one. could you recommend something to do near the hotel?')\n",
"\n",
"call_travel_advisor: Near The Ritz-Carlton in Palm Beach, here are some popular activities you can enjoy:\n",
"\n",
"1. Palm Beach Strip - Take a walk along this bustling strip filled with restaurants, shops, and bars\n",
"2. Visit the Bubali Bird Sanctuary - Just a short distance away\n",
"3. Try your luck at the Stellaris Casino - Located right in The Ritz-Carlton\n",
"4. Water Sports at Palm Beach - Right in front of the hotel you can:\n",
" - Go parasailing\n",
" - Try jet skiing\n",
" - Take a sunset sailing cruise\n",
"5. Visit the Palm Beach Plaza Mall - High-end shopping just a short walk away\n",
"6. Enjoy dinner at Madame Janette's - One of Aruba's most famous restaurants nearby\n",
"\n",
"Would you like more specific information about any of these activities or other suggestions in the area?\n"
]
}
],
"source": [
"thread_config = {\"configurable\": {\"thread_id\": uuid.uuid4()}}\n",
"\n",
"inputs = [\n",
" # 1st round of conversation,\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"i wanna go somewhere warm in the caribbean\",\n",
" \"id\": str(uuid.uuid4()),\n",
" },\n",
" # Since we're using `interrupt`, we'll need to resume using the Command primitive.\n",
" # 2nd round of conversation,\n",
" Command(\n",
" resume=\"could you recommend a nice hotel in one of the areas and tell me which area it is.\"\n",
" ),\n",
" # 3rd round of conversation,\n",
" Command(\n",
" resume=\"i like the first one. could you recommend something to do near the hotel?\"\n",
" ),\n",
"]\n",
"\n",
"for idx, user_input in enumerate(inputs):\n",
" print()\n",
" print(f\"--- Conversation Turn {idx + 1} ---\")\n",
" print()\n",
" print(f\"User: {user_input}\")\n",
" print()\n",
" for update in multi_turn_graph.stream(\n",
" user_input,\n",
" config=thread_config,\n",
" stream_mode=\"updates\",\n",
" ):\n",
" for node_id, value in update.items():\n",
" if isinstance(value, list) and value:\n",
" last_message = value[-1]\n",
" if isinstance(last_message, dict) or last_message.type != \"ai\":\n",
" continue\n",
" print(f\"{node_id}: {last_message.content}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -229,7 +229,7 @@
"travel_advisor = create_react_agent(\n",
" model,\n",
" travel_advisor_tools,\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You are a general travel expert that can recommend travel destinations (e.g. countries, cities, etc). \"\n",
" \"If you need hotel recommendations, ask 'hotel_advisor' for help. \"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
@@ -254,7 +254,7 @@
"hotel_advisor = create_react_agent(\n",
" model,\n",
" hotel_advisor_tools,\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You are a hotel expert that can provide hotel recommendations for a given destination. \"\n",
" \"If you need help picking travel destinations, ask 'travel_advisor' for help.\"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
@@ -0,0 +1,500 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "87684b48-150e-4e15-b0a5-a9dd7851f8fb",
"metadata": {},
"source": [
"# How to build a multi-agent network (functional API)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "2c65639c-9705-49f1-840a-370718852e98",
"metadata": {},
"source": [
"!!! info \"Prerequisites\" \n",
" This guide assumes familiarity with the following:\n",
"\n",
" - [Multi-agent systems](../../concepts/multi_agent)\n",
" - [Functional API](../../concepts/functional_api)\n",
" - [Command](../../concepts/low_level/#command)\n",
" - [LangGraph Glossary](../../concepts/low_level/)\n",
"\n",
"In this how-to guide we will demonstrate how to implement a [multi-agent network](../../concepts/multi_agent#network) architecture where each agent can communicate with every other agent (many-to-many connections) and can decide which agent to call next. We will be using [functional API](../../concepts/functional_api) — individual agents will be defined as tasks and the agent handoffs will be defined in the main [entrypoint()][langgraph.func.entrypoint]:\n",
"\n",
"```python\n",
"from langgraph.func import entrypoint\n",
"from langgraph.prebuilt import create_react_agent\n",
"from langchain_core.tools import tool\n",
"\n",
"\n",
"# Define a tool to signal intent to hand off to a different agent\n",
"@tool(return_direct=True)\n",
"def transfer_to_hotel_advisor():\n",
" \"\"\"Ask hotel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to hotel advisor\"\n",
"\n",
"\n",
"# define an agent\n",
"travel_advisor_tools = [transfer_to_hotel_advisor, ...]\n",
"travel_advisor = create_react_agent(model, travel_advisor_tools)\n",
"\n",
"\n",
"# define a task that calls an agent\n",
"@task\n",
"def call_travel_advisor(messages):\n",
" response = travel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"# define the multi-agent network workflow\n",
"@entrypoint()\n",
"def workflow(messages):\n",
" call_active_agent = call_travel_advisor\n",
" while True:\n",
" agent_messages = call_active_agent(messages).result()\n",
" messages = messages + agent_messages\n",
" call_active_agent = get_next_agent(messages)\n",
" return messages\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "faaa4444-cd06-4813-b9ca-c9700fe12cb7",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"First, let's install the required packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "05038da0-31df-4066-a1a4-c4ccb5db4d3a",
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain-anthropic"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0bcff5d4-130e-426d-9285-40d0f72c7cd3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"ANTHROPIC_API_KEY: ········\n"
]
}
],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"ANTHROPIC_API_KEY\")"
]
},
{
"cell_type": "markdown",
"id": "c3ec6e48-85dc-4905-ba50-985e5d4788e6",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for LangGraph development</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started <a href=\"https://docs.smith.langchain.com\">here</a>. \n",
" </p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"id": "4a53f304-3709-4df7-8714-1ca61e615743",
"metadata": {},
"source": [
"## Travel agent example"
]
},
{
"cell_type": "markdown",
"id": "34cd131b-f0c2-4b69-887f-2cbd5afb14a7",
"metadata": {},
"source": [
"In this example we will build a team of travel assistant agents that can communicate with each other.\n",
"\n",
"We will create 2 agents:\n",
"\n",
"* `travel_advisor`: can help with travel destination recommendations. Can ask `hotel_advisor` for help.\n",
"* `hotel_advisor`: can help with hotel recommendations. Can ask `travel_advisor` for help.\n",
"\n",
"This is a fully-connected network - every agent can talk to any other agent. "
]
},
{
"cell_type": "markdown",
"id": "fedc9ed0-e90c-4ee1-a7c6-f5af3c634a7b",
"metadata": {},
"source": [
"First, let's create some of the tools that the agents will be using:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7e31f258-ec28-4020-b86d-c91dfa9a3bfc",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"from typing_extensions import Literal\n",
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def get_travel_recommendations():\n",
" \"\"\"Get recommendation for travel destinations\"\"\"\n",
" return random.choice([\"aruba\", \"turks and caicos\"])\n",
"\n",
"\n",
"@tool\n",
"def get_hotel_recommendations(location: Literal[\"aruba\", \"turks and caicos\"]):\n",
" \"\"\"Get hotel recommendations for a given destination.\"\"\"\n",
" return {\n",
" \"aruba\": [\n",
" \"The Ritz-Carlton, Aruba (Palm Beach)\"\n",
" \"Bucuti & Tara Beach Resort (Eagle Beach)\"\n",
" ],\n",
" \"turks and caicos\": [\"Grace Bay Club\", \"COMO Parrot Cay\"],\n",
" }[location]\n",
"\n",
"\n",
"@tool(return_direct=True)\n",
"def transfer_to_hotel_advisor():\n",
" \"\"\"Ask hotel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to hotel advisor\"\n",
"\n",
"\n",
"@tool(return_direct=True)\n",
"def transfer_to_travel_advisor():\n",
" \"\"\"Ask travel advisor agent for help.\"\"\"\n",
" return \"Successfully transferred to travel advisor\""
]
},
{
"cell_type": "markdown",
"id": "d8519a32-d23b-48b0-bd18-74f8c0dacf58",
"metadata": {},
"source": [
"!!! note \"Transfer tools\"\n",
"\n",
" You might have noticed that we're using `@tool(return_direct=True)` in the transfer tools. This is done so that individual agents (e.g., `travel_advisor`) can exit the ReAct loop early once these tools are called. This is the desired behavior, as we want to detect when the agent calls this tool and hand control off _immediately_ to a different agent. \n",
" \n",
" **NOTE**: This is meant to work with the prebuilt [`create_react_agent`][langgraph.prebuilt.chat_agent_executor.create_react_agent] -- if you are building a custom agent, make sure to manually add logic for handling early exit for tools that are marked with `return_direct`."
]
},
{
"cell_type": "markdown",
"id": "93dbc3bd-27b9-4d79-b5dd-be592bc50f74",
"metadata": {},
"source": [
"Now let's define our agent tasks and combine them into a single multi-agent network workflow:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b638d6c4-3de6-4921-980c-2df1bd1cc9c7",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import AIMessage\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langgraph.prebuilt import create_react_agent\n",
"from langgraph.graph import add_messages\n",
"from langgraph.func import entrypoint, task\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\")\n",
"\n",
"# Define travel advisor ReAct agent\n",
"travel_advisor_tools = [\n",
" get_travel_recommendations,\n",
" transfer_to_hotel_advisor,\n",
"]\n",
"travel_advisor = create_react_agent(\n",
" model,\n",
" travel_advisor_tools,\n",
" state_modifier=(\n",
" \"You are a general travel expert that can recommend travel destinations (e.g. countries, cities, etc). \"\n",
" \"If you need hotel recommendations, ask 'hotel_advisor' for help. \"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
" ),\n",
")\n",
"\n",
"\n",
"@task\n",
"def call_travel_advisor(messages):\n",
" # You can also add additional logic like changing the input to the agent / output from the agent, etc.\n",
" # NOTE: we're invoking the ReAct agent with the full history of messages in the state\n",
" response = travel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"# Define hotel advisor ReAct agent\n",
"hotel_advisor_tools = [get_hotel_recommendations, transfer_to_travel_advisor]\n",
"hotel_advisor = create_react_agent(\n",
" model,\n",
" hotel_advisor_tools,\n",
" state_modifier=(\n",
" \"You are a hotel expert that can provide hotel recommendations for a given destination. \"\n",
" \"If you need help picking travel destinations, ask 'travel_advisor' for help.\"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
" ),\n",
")\n",
"\n",
"\n",
"@task\n",
"def call_hotel_advisor(messages):\n",
" response = hotel_advisor.invoke({\"messages\": messages})\n",
" return response[\"messages\"]\n",
"\n",
"\n",
"@entrypoint()\n",
"def workflow(messages):\n",
" messages = add_messages([], messages)\n",
"\n",
" call_active_agent = call_travel_advisor\n",
" while True:\n",
" agent_messages = call_active_agent(messages).result()\n",
" messages = add_messages(messages, agent_messages)\n",
" ai_msg = next(m for m in reversed(agent_messages) if isinstance(m, AIMessage))\n",
" if not ai_msg.tool_calls:\n",
" break\n",
"\n",
" tool_call = ai_msg.tool_calls[-1]\n",
" if tool_call[\"name\"] == \"transfer_to_travel_advisor\":\n",
" call_active_agent = call_travel_advisor\n",
" elif tool_call[\"name\"] == \"transfer_to_hotel_advisor\":\n",
" call_active_agent = call_hotel_advisor\n",
" else:\n",
" raise ValueError(f\"Expected transfer tool, got '{tool_call['name']}'\")\n",
"\n",
" return messages"
]
},
{
"cell_type": "markdown",
"id": "9223db83-1938-434a-9d24-8666842a8eea",
"metadata": {},
"source": [
"Lastly, let's define a helper to render the agent outputs:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "058f3d96-534f-4b97-afb3-799ba81224ea",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import convert_to_messages\n",
"\n",
"\n",
"def pretty_print_messages(update):\n",
" if isinstance(update, tuple):\n",
" ns, update = update\n",
" # skip parent graph updates in the printouts\n",
" if len(ns) == 0:\n",
" return\n",
"\n",
" graph_id = ns[-1].split(\":\")[0]\n",
" print(f\"Update from subgraph {graph_id}:\")\n",
" print(\"\\n\")\n",
"\n",
" for node_name, node_update in update.items():\n",
" print(f\"Update from node {node_name}:\")\n",
" print(\"\\n\")\n",
"\n",
" for m in convert_to_messages(node_update[\"messages\"]):\n",
" m.pretty_print()\n",
" print(\"\\n\")"
]
},
{
"cell_type": "markdown",
"id": "7132e2c0-d937-4325-a30e-e715c5304fe0",
"metadata": {},
"source": [
"Let's test it out using the same input as our original multi-agent system:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "29b47c57-ad05-4f10-83bf-c3ff6ff8eb93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Update from subgraph call_travel_advisor:\n",
"\n",
"\n",
"Update from node agent:\n",
"\n",
"\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"[{'text': \"I'll help you find a warm Caribbean destination and then get some hotel recommendations for you.\\n\\nLet me first get some destination recommendations for the Caribbean region.\", 'type': 'text'}, {'id': 'toolu_015vT8PkPq1VXvjrDvSpWUwJ', 'input': {}, 'name': 'get_travel_recommendations', 'type': 'tool_use'}]\n",
"Tool Calls:\n",
" get_travel_recommendations (toolu_015vT8PkPq1VXvjrDvSpWUwJ)\n",
" Call ID: toolu_015vT8PkPq1VXvjrDvSpWUwJ\n",
" Args:\n",
"\n",
"\n",
"Update from subgraph call_travel_advisor:\n",
"\n",
"\n",
"Update from node tools:\n",
"\n",
"\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: get_travel_recommendations\n",
"\n",
"turks and caicos\n",
"\n",
"\n",
"Update from subgraph call_travel_advisor:\n",
"\n",
"\n",
"Update from node agent:\n",
"\n",
"\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"[{'text': \"Based on the recommendation, I suggest Turks and Caicos! This beautiful British Overseas Territory is known for its stunning white-sand beaches, crystal-clear turquoise waters, and year-round warm weather. Grace Bay Beach in Providenciales is consistently ranked among the world's best beaches. The islands offer excellent snorkeling, diving, and water sports opportunities, plus a relaxed Caribbean atmosphere.\\n\\nNow, let me connect you with our hotel advisor to get some specific hotel recommendations for Turks and Caicos.\", 'type': 'text'}, {'id': 'toolu_01JY7pNNWFuaWoe9ymxFYiPV', 'input': {}, 'name': 'transfer_to_hotel_advisor', 'type': 'tool_use'}]\n",
"Tool Calls:\n",
" transfer_to_hotel_advisor (toolu_01JY7pNNWFuaWoe9ymxFYiPV)\n",
" Call ID: toolu_01JY7pNNWFuaWoe9ymxFYiPV\n",
" Args:\n",
"\n",
"\n",
"Update from subgraph call_travel_advisor:\n",
"\n",
"\n",
"Update from node tools:\n",
"\n",
"\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: transfer_to_hotel_advisor\n",
"\n",
"Successfully transferred to hotel advisor\n",
"\n",
"\n",
"Update from subgraph call_hotel_advisor:\n",
"\n",
"\n",
"Update from node agent:\n",
"\n",
"\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"[{'text': 'Let me get some hotel recommendations for Turks and Caicos:', 'type': 'text'}, {'id': 'toolu_0129ELa7jFocn16bowaGNapg', 'input': {'location': 'turks and caicos'}, 'name': 'get_hotel_recommendations', 'type': 'tool_use'}]\n",
"Tool Calls:\n",
" get_hotel_recommendations (toolu_0129ELa7jFocn16bowaGNapg)\n",
" Call ID: toolu_0129ELa7jFocn16bowaGNapg\n",
" Args:\n",
" location: turks and caicos\n",
"\n",
"\n",
"Update from subgraph call_hotel_advisor:\n",
"\n",
"\n",
"Update from node tools:\n",
"\n",
"\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: get_hotel_recommendations\n",
"\n",
"[\"Grace Bay Club\", \"COMO Parrot Cay\"]\n",
"\n",
"\n",
"Update from subgraph call_hotel_advisor:\n",
"\n",
"\n",
"Update from node agent:\n",
"\n",
"\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Here are two excellent hotel options in Turks and Caicos:\n",
"\n",
"1. Grace Bay Club: This luxury resort is located on the world-famous Grace Bay Beach. It offers all-oceanfront suites, exceptional dining options, and personalized service. The resort features adult-only and family-friendly sections, making it perfect for any type of traveler.\n",
"\n",
"2. COMO Parrot Cay: This exclusive private island resort offers the ultimate luxury escape. It's known for its pristine beach, world-class spa, and holistic wellness programs. The resort provides an intimate, secluded experience with top-notch amenities and service.\n",
"\n",
"Would you like more specific information about either of these properties or would you like to explore hotels in another destination?\n",
"\n",
"\n"
]
}
],
"source": [
"for chunk in workflow.stream(\n",
" [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"i wanna go somewhere warm in the caribbean. pick one destination and give me hotel recommendations\",\n",
" }\n",
" ],\n",
" subgraphs=True,\n",
"):\n",
" pretty_print_messages(chunk)"
]
},
{
"cell_type": "markdown",
"id": "d7d89ee0-0229-4718-9b98-bdd3f59c1014",
"metadata": {},
"source": [
"Voila - `travel_advisor` picks a destination and then makes a decision to call `hotel_advisor` for more info!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+2 -2
View File
@@ -555,7 +555,7 @@
"travel_advisor = create_react_agent(\n",
" model,\n",
" travel_advisor_tools,\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You are a general travel expert that can recommend travel destinations (e.g. countries, cities, etc). \"\n",
" \"If you need hotel recommendations, ask 'hotel_advisor' for help. \"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
@@ -579,7 +579,7 @@
"hotel_advisor = create_react_agent(\n",
" model,\n",
" hotel_advisor_tools,\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You are a hotel expert that can provide hotel recommendations for a given destination. \"\n",
" \"If you need help picking travel destinations, ask 'travel_advisor' for help.\"\n",
" \"You MUST include human-readable response before transferring to another agent.\"\n",
@@ -0,0 +1,349 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53",
"metadata": {},
"source": [
"# How to add thread-level persistence (functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
"\n",
" This guide assumes familiarity with the following:\n",
" \n",
" - [Functional API](../../concepts/functional_api/)\n",
" - [Persistence](../../concepts/persistence/)\n",
" - [Memory](../../concepts/memory/)\n",
" - [Chat Models](https://python.langchain.com/docs/concepts/chat_models/)\n",
"\n",
"Many AI applications need memory to share context across multiple interactions on the same [thread](../../concepts/persistence#threads) (e.g., multiple turns of a conversation). In LangGraph functional API, this kind of memory can be added to any [entrypoint()][langgraph.func.entrypoint] workflow using [thread-level persistence](https://langchain-ai.github.io/langgraph/concepts/persistence).\n",
"\n",
"When creating a LangGraph workflow, you can set it up to persist its results by using a [checkpointer](https://langchain-ai.github.io/langgraph/reference/checkpoints/#basecheckpointsaver):\n",
"\n",
"\n",
"1. Create an instance of a checkpointer:\n",
"\n",
" ```python\n",
" from langgraph.checkpoint.memory import MemorySaver\n",
" \n",
" checkpointer = MemorySaver() \n",
" ```\n",
"\n",
"2. Pass `checkpointer` instance to the `entrypoint()` decorator:\n",
"\n",
" ```python\n",
" from langgraph.func import entrypoint\n",
" \n",
" @entrypoint(checkpointer=checkpointer)\n",
" def workflow(inputs)\n",
" ...\n",
" ```\n",
"\n",
"3. Optionally expose `previous` parameter in the workflow function signature:\n",
"\n",
" ```python\n",
" @entrypoint(checkpointer=checkpointer)\n",
" def workflow(\n",
" inputs,\n",
" *,\n",
" # you can optionally specify `previous` in the workflow function signature\n",
" # to access the return value from the workflow as of the last execution\n",
" previous\n",
" ):\n",
" previous = previous or []\n",
" combined_inputs = previous + inputs\n",
" result = do_something(combined_inputs)\n",
" ...\n",
" ```\n",
"\n",
"4. Optionally choose which values will be returned from the workflow and which will be saved by the checkpointer as `previous`:\n",
"\n",
" ```python\n",
" @entrypoint(checkpointer=checkpointer)\n",
" def workflow(inputs, *, previous):\n",
" ...\n",
" result = do_something(...)\n",
" return entrypoint.final(value=result, save=combine(inputs, result))\n",
" ```\n",
"\n",
"This guide shows how you can add thread-level persistence to your workflow.\n",
"\n",
"!!! tip \"Note\"\n",
"\n",
" If you need memory that is __shared__ across multiple conversations or users (cross-thread persistence), check out this [how-to guide](../cross-thread-persistence-functional).\n",
"\n",
"!!! tip \"Note\"\n",
"\n",
" If you need to add thread-level persistence to a `StateGraph`, check out this [how-to guide](../persistence)."
]
},
{
"cell_type": "markdown",
"id": "7cbd446a-808f-4394-be92-d45ab818953c",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"First we need to install the packages required"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833",
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install --quiet -U langgraph langchain_anthropic"
]
},
{
"cell_type": "markdown",
"id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d",
"metadata": {},
"source": [
"Next, we need to set API key for Anthropic (the LLM we will use)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"ANTHROPIC_API_KEY\")"
]
},
{
"cell_type": "markdown",
"id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for LangGraph development</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started <a href=\"https://docs.smith.langchain.com\">here</a>. \n",
" </p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"id": "4cf509bc",
"metadata": {},
"source": [
"## Example: simple chatbot with short-term memory\n",
"\n",
"We will be using a workflow with a single task that calls a [chat model](https://python.langchain.com/docs/concepts/chat_models/).\n",
"\n",
"Let's first define the model we'll be using:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "892b54b9-75f0-4804-9ed0-88b5e5532989",
"metadata": {},
"outputs": [],
"source": [
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-5-sonnet-latest\")"
]
},
{
"cell_type": "markdown",
"id": "7b7a2792-982b-4e47-83eb-0c594725d1c1",
"metadata": {},
"source": [
"Now we can define our task and workflow. To add in persistence, we need to pass in a [Checkpointer](https://langchain-ai.github.io/langgraph/reference/checkpoints/#langgraph.checkpoint.base.BaseCheckpointSaver) to the [entrypoint()][langgraph.func.entrypoint] decorator."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "87326ea6-34c5-46da-a41f-dda26ef9bd74",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import BaseMessage\n",
"from langgraph.graph import add_messages\n",
"from langgraph.func import entrypoint, task\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"\n",
"\n",
"@task\n",
"def call_model(messages: list[BaseMessage]):\n",
" response = model.invoke(messages)\n",
" return response\n",
"\n",
"\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"@entrypoint(checkpointer=checkpointer)\n",
"def workflow(inputs: list[BaseMessage], *, previous: list[BaseMessage]):\n",
" if previous:\n",
" inputs = add_messages(previous, inputs)\n",
"\n",
" response = call_model(inputs).result()\n",
" return entrypoint.final(value=response, save=add_messages(inputs, response))"
]
},
{
"cell_type": "markdown",
"id": "250d8fd9-2e7a-4892-9adc-19762a1e3cce",
"metadata": {},
"source": [
"If we try to use this workflow, the context of the conversation will be persisted across interactions:"
]
},
{
"cell_type": "markdown",
"id": "7654ebcc-2179-41b4-92d1-6666f6f8634f",
"metadata": {},
"source": [
"!!! note Note\n",
"\n",
" If you're using LangGraph Cloud or LangGraph Studio, you __don't need__ to pass checkpointer to the entrypoint decorator, since it's done automatically."
]
},
{
"cell_type": "markdown",
"id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159",
"metadata": {},
"source": [
"We can now interact with the agent and see that it remembers previous messages!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cfd140f0-a5a6-4697-8115-322242f197b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Hi Bob! I'm Claude. Nice to meet you! How are you today?\n"
]
}
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
"input_message = {\"role\": \"user\", \"content\": \"hi! I'm bob\"}\n",
"for chunk in workflow.stream([input_message], config, stream_mode=\"values\"):\n",
" chunk.pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "1bb07bf8-68b7-4049-a0f1-eb67a4879a3a",
"metadata": {},
"source": [
"You can always resume previous threads:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "08ae8246-11d5-40e1-8567-361e5bef8917",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Your name is Bob.\n"
]
}
],
"source": [
"input_message = {\"role\": \"user\", \"content\": \"what's my name?\"}\n",
"for chunk in workflow.stream([input_message], config, stream_mode=\"values\"):\n",
" chunk.pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "3f47bbfc-d9ef-4288-ba4a-ebbc0136fa9d",
"metadata": {},
"source": [
"If we want to start a new conversation, we can pass in a different `thread_id`. Poof! All the memories are gone!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "273d56a8-f40f-4a51-a27f-7c6bb2bda0ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"I don't know your name unless you tell me. Each conversation I have starts fresh, so I don't have access to any previous interactions or personal information unless you share it with me.\n"
]
}
],
"source": [
"input_message = {\"role\": \"user\", \"content\": \"what's my name?\"}\n",
"for chunk in workflow.stream(\n",
" [input_message],\n",
" {\"configurable\": {\"thread_id\": \"2\"}},\n",
" stream_mode=\"values\",\n",
"):\n",
" chunk.pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "ac7926a8-4c88-4b16-973c-53d6da3f4a08",
"metadata": {},
"source": [
"!!! tip \"Streaming tokens\"\n",
"\n",
" If you would like to stream LLM tokens from your chatbot, you can use `stream_mode=\"messages\"`. Check out this [how-to guide](../streaming-tokens) to learn more."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+7 -7
View File
@@ -78,7 +78,7 @@
"id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d",
"metadata": {},
"source": [
"Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)"
"Next, we need to set API key for Anthropic (the LLM we will use)."
]
},
{
@@ -211,11 +211,11 @@
}
],
"source": [
"input_message = {\"type\": \"user\", \"content\": \"hi! I'm bob\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"hi! I'm bob\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()\n",
"\n",
"input_message = {\"type\": \"user\", \"content\": \"what's my name?\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"what's my name?\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -286,7 +286,7 @@
],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
"input_message = {\"type\": \"user\", \"content\": \"hi! I'm bob\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"hi! I'm bob\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -319,7 +319,7 @@
}
],
"source": [
"input_message = {\"type\": \"user\", \"content\": \"what's my name?\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"what's my name?\"}\n",
"for chunk in graph.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
@@ -352,7 +352,7 @@
}
],
"source": [
"input_message = {\"type\": \"user\", \"content\": \"what's my name?\"}\n",
"input_message = {\"role\": \"user\", \"content\": \"what's my name?\"}\n",
"for chunk in graph.stream(\n",
" {\"messages\": [input_message]},\n",
" {\"configurable\": {\"thread_id\": \"2\"}},\n",
@@ -378,7 +378,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
}
},
"nbformat": 4,
@@ -0,0 +1,463 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to create a ReAct agent from scratch (Functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
" This guide assumes familiarity with the following:\n",
" \n",
" - [Chat Models](https://python.langchain.com/docs/concepts/chat_models)\n",
" - [Messages](https://python.langchain.com/docs/concepts/messages)\n",
" - [Tool Calling](https://python.langchain.com/docs/concepts/tool_calling/)\n",
" - [Entrypoints](../../concepts/functional_api/#entrypoint) and [Tasks](../../concepts/functional_api/#task)\n",
"\n",
"This guide demonstrates how to implement a ReAct agent using the LangGraph [Functional API](../../concepts/functional_api).\n",
"\n",
"The ReAct agent is a [tool-calling agent](../../concepts/agentic_concepts/#tool-calling-agent) that operates as follows:\n",
"\n",
"1. Queries are issued to a chat model;\n",
"2. If the model generates no [tool calls](../../concepts/agentic_concepts/#tool-calling), we return the model response.\n",
"3. If the model generates tool calls, we execute the tool calls with available tools, append them as [tool messages](https://python.langchain.com/docs/concepts/messages/) to our message list, and repeat the process.\n",
"\n",
"This is a simple and versatile set-up that can be extended with memory, human-in-the-loop capabilities, and other features. See the dedicated [how-to guides](../../how-tos/#prebuilt-react-agent) for examples.\n",
"\n",
"## Setup\n",
"\n",
"First, let's install the required packages and set our API keys:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain-openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for better debugging</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM aps built with LangGraph — read more about how to get started in the <a href=\"https://docs.smith.langchain.com\">docs</a>. \n",
" </p>\n",
" </div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create ReAct agent\n",
"\n",
"Now that you have installed the required packages and set your environment variables, we can create our agent.\n",
"\n",
"### Define model and tools\n",
"\n",
"Let's first define the tools and model we will use for our example. Here we will use a single place-holder tool that gets a description of the weather for a location.\n",
"\n",
"We will use an [OpenAI](https://python.langchain.com/docs/integrations/providers/openai/) chat model for this example, but any model [supporting tool-calling](https://python.langchain.com/docs/integrations/chat/) will suffice."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.tools import tool\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"\n",
"@tool\n",
"def get_weather(location: str):\n",
" \"\"\"Call to get the weather from a specific location.\"\"\"\n",
" # This is a placeholder for the actual implementation\n",
" if any([city in location.lower() for city in [\"sf\", \"san francisco\"]]):\n",
" return \"It's sunny!\"\n",
" elif \"boston\" in location.lower():\n",
" return \"It's rainy!\"\n",
" else:\n",
" return f\"I am not sure what the weather is in {location}\"\n",
"\n",
"\n",
"tools = [get_weather]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define tasks\n",
"\n",
"We next define the [tasks](../../concepts/functional_api/#task) we will execute. Here there are two different tasks:\n",
"\n",
"1. **Call model**: We want to query our chat model with a list of messages.\n",
"2. **Call tool**: If our model generates tool calls, we want to execute them."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import ToolMessage\n",
"from langgraph.func import entrypoint, task\n",
"\n",
"tools_by_name = {tool.name: tool for tool in tools}\n",
"\n",
"\n",
"@task\n",
"def call_model(messages):\n",
" \"\"\"Call model with a sequence of messages.\"\"\"\n",
" response = model.bind_tools(tools).invoke(messages)\n",
" return response\n",
"\n",
"\n",
"@task\n",
"def call_tool(tool_call):\n",
" tool = tools_by_name[tool_call[\"name\"]]\n",
" observation = tool.invoke(tool_call[\"args\"])\n",
" return ToolMessage(content=observation, tool_call_id=tool_call[\"id\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define entrypoint\n",
"\n",
"Our [entrypoint](../../concepts/functional_api/#entrypoint) will handle the orchestration of these two tasks. As described above, when our `call_model` task generates tool calls, the `call_tool` task will generate responses for each. We append all messages to a single messages list.\n",
"\n",
"!!! tip\n",
" Note that because tasks return future-like objects, the below implementation executes tools in parallel."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.graph.message import add_messages\n",
"\n",
"\n",
"@entrypoint()\n",
"def agent(messages):\n",
" llm_response = call_model(messages).result()\n",
" while True:\n",
" if not llm_response.tool_calls:\n",
" break\n",
"\n",
" # Execute tools\n",
" tool_result_futures = [\n",
" call_tool(tool_call) for tool_call in llm_response.tool_calls\n",
" ]\n",
" tool_results = [fut.result() for fut in tool_result_futures]\n",
"\n",
" # Append to message list\n",
" messages = add_messages(messages, [llm_response, *tool_results])\n",
"\n",
" # Call model again\n",
" llm_response = call_model(messages).result()\n",
"\n",
" return llm_response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Usage\n",
"\n",
"To use our agent, we invoke it with a messages list. Based on our implementation, these can be LangChain [message](https://python.langchain.com/docs/concepts/messages/) objects or OpenAI-style dicts:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': \"What's the weather in san francisco?\"}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_tNnkrjnoz6MNfCHJpwfuEQ0v)\n",
" Call ID: call_tNnkrjnoz6MNfCHJpwfuEQ0v\n",
" Args:\n",
" location: san francisco\n",
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's sunny!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather in San Francisco is sunny!\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"What's the weather in san francisco?\"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message]):\n",
" for task_name, message in step.items():\n",
" if task_name == \"agent\":\n",
" continue # Just print task updates\n",
" print(f\"\\n{task_name}:\")\n",
" message.pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perfect! The graph correctly calls the `get_weather` tool and responds to the user after receiving the information from the tool. Check out the LangSmith trace [here](https://smith.langchain.com/public/d5a0d5ea-bdaa-4032-911e-7db177c8141b/r)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add thread-level persistence\n",
"\n",
"Adding [thread-level persistence](../../concepts/persistence#threads) lets us support conversational experiences with our agent: subsequent invocations will append to the prior messages list, retaining the full conversational context.\n",
"\n",
"To add thread-level persistence to our agent:\n",
"\n",
"1. Select a [checkpointer](../../concepts/persistence#checkpointer-libraries): here we will use [MemorySaver](../../reference/checkpoints/#langgraph.checkpoint.memory.MemorySaver), a simple in-memory checkpointer.\n",
"2. Update our entrypoint to accept the previous messages state as a second argument. Here, we simply append the message updates to the previous sequence of messages.\n",
"3. Choose which values will be returned from the workflow and which will be saved by the checkpointer as `previous` using `entrypoint.final` (optional)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.checkpoint.memory import MemorySaver\n",
"\n",
"# highlight-next-line\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"# highlight-next-line\n",
"@entrypoint(checkpointer=checkpointer)\n",
"# highlight-next-line\n",
"def agent(messages, previous):\n",
" # highlight-next-line\n",
" if previous is not None:\n",
" # highlight-next-line\n",
" messages = add_messages(previous, messages)\n",
"\n",
" llm_response = call_model(messages).result()\n",
" while True:\n",
" if not llm_response.tool_calls:\n",
" break\n",
"\n",
" # Execute tools\n",
" tool_result_futures = [\n",
" call_tool(tool_call) for tool_call in llm_response.tool_calls\n",
" ]\n",
" tool_results = [fut.result() for fut in tool_result_futures]\n",
"\n",
" # Append to message list\n",
" messages = add_messages(messages, [llm_response, *tool_results])\n",
"\n",
" # Call model again\n",
" llm_response = call_model(messages).result()\n",
"\n",
" # Generate final response\n",
" messages = add_messages(messages, llm_response)\n",
" # highlight-next-line\n",
" return entrypoint.final(value=llm_response, save=messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now need to pass in a config when running our application. The config will specify an identifier for the conversational thread.\n",
"\n",
"!!! tip\n",
"\n",
" Read more about thread-level persistence in our [concepts page](../../concepts/persistence/) and [how-to guides](../../how-tos/#persistence)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start a thread the same way as before, this time passing in the config:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': \"What's the weather in san francisco?\"}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_lubbUSdDofmOhFunPEZLBz3g)\n",
" Call ID: call_lubbUSdDofmOhFunPEZLBz3g\n",
" Args:\n",
" location: San Francisco\n",
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's sunny!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather in San Francisco is sunny!\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"What's the weather in san francisco?\"}\n",
"print(user_message)\n",
"\n",
"# highlight-next-line\n",
"for step in agent.stream([user_message], config):\n",
" for task_name, message in step.items():\n",
" if task_name == \"agent\":\n",
" continue # Just print task updates\n",
" print(f\"\\n{task_name}:\")\n",
" message.pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we ask a follow-up conversation, the model uses the prior context to infer that we are asking about the weather:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': 'How does it compare to Boston, MA?'}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_8sTKYAhSIHOdjLD5d6gaswuV)\n",
" Call ID: call_8sTKYAhSIHOdjLD5d6gaswuV\n",
" Args:\n",
" location: Boston, MA\n",
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's rainy!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Compared to San Francisco, which is sunny, Boston, MA is experiencing rainy weather.\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"How does it compare to Boston, MA?\"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message], config):\n",
" for task_name, message in step.items():\n",
" if task_name == \"agent\":\n",
" continue # Just print task updates\n",
" print(f\"\\n{task_name}:\")\n",
" message.pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the [LangSmith trace](https://smith.langchain.com/public/20a1116b-bb3b-44c1-8765-7a28663439d9/r), we can see that the full conversational context is retained in each model call."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@@ -6,37 +6,15 @@
"source": [
"# How to create a ReAct agent from scratch\n",
"\n",
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Prerequisites</p>\n",
" <p>\n",
" This guide assumes familiarity with the following:\n",
" <ul>\n",
" <li>\n",
" <a href=\"https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#tool-calling-agent\">\n",
" Tool calling agent\n",
" </a>\n",
" </li> \n",
" <li>\n",
" <a href=\"https://python.langchain.com/docs/concepts/chat_models\">\n",
" Chat Models\n",
" </a>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://python.langchain.com/docs/concepts/messages\">\n",
" Messages\n",
" </a>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://langchain-ai.github.io/langgraph/concepts/low_level/\">\n",
" LangGraph Glossary\n",
" </a>\n",
" </li>\n",
" </ul>\n",
" </p>\n",
"</div> \n",
"!!! info \"Prerequisites\"\n",
" This guide assumes familiarity with the following:\n",
" \n",
" - [Tool calling agent](../../concepts/agentic_concepts/#tool-calling-agent)\n",
" - [Chat Models](https://python.langchain.com/docs/concepts/chat_models/)\n",
" - [Messages](https://python.langchain.com/docs/concepts/messages/)\n",
" - [LangGraph Glossary](../../concepts/low_level/)\n",
"\n",
"\n",
"Using the prebuilt ReAct agent ([create_react_agent](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.chat_agent_executor.create_react_agent)) is a great way to get started, but sometimes you might want more control and customization. In those cases, you can create a custom ReAct agent. This guide shows how to implement ReAct agent from scratch using LangGraph.\n",
"Using the prebuilt ReAct agent [create_react_agent][langgraph.prebuilt.chat_agent_executor.create_react_agent] is a great way to get started, but sometimes you might want more control and customization. In those cases, you can create a custom ReAct agent. This guide shows how to implement ReAct agent from scratch using LangGraph.\n",
"\n",
"## Setup\n",
"\n",
@@ -202,7 +180,7 @@
" state: AgentState,\n",
" config: RunnableConfig,\n",
"):\n",
" # this is similar to customizing the create_react_agent with state_modifier, but is a lot more flexible\n",
" # this is similar to customizing the create_react_agent with 'prompt' parameter, but is more flexible\n",
" system_prompt = SystemMessage(\n",
" \"You are a helpful AI assistant, please respond to the users query to the best of your ability!\"\n",
" )\n",
@@ -0,0 +1,627 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to review tool calls (Functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
" This guide assumes familiarity with the following:\n",
"\n",
" - Implementing [human-in-the-loop](../../concepts/human_in_the_loop) workflows with [interrupt](../../concepts/human_in_the_loop/#interrupt)\n",
" - [How to create a ReAct agent using the Functional API](../../how-tos/react-agent-from-scratch-functional)\n",
"\n",
"This guide demonstrates how to implement human-in-the-loop workflows in a ReAct agent using the LangGraph [Functional API](../../concepts/functional_api).\n",
"\n",
"We will build off of the agent created in the [How to create a ReAct agent using the Functional API](../../how-tos/react-agent-from-scratch-functional) guide.\n",
"\n",
"Specifically, we will demonstrate how to review [tool calls](https://python.langchain.com/docs/concepts/tool_calling/) generated by a [chat model](https://python.langchain.com/docs/concepts/chat_models/) prior to their execution. This can be accomplished through use of the [interrupt](../../concepts/human_in_the_loop/#interrupt) function at key points in our application.\n",
"\n",
"**Preview**:\n",
"\n",
"We will implement a simple function that reviews tool calls generated from our chat model and call it from inside our application's [entrypoint](../../concepts/functional_api/#entrypoint):\n",
"\n",
"```python\n",
"def review_tool_call(tool_call: ToolCall) -> Union[ToolCall, ToolMessage]:\n",
" \"\"\"Review a tool call, returning a validated version.\"\"\"\n",
" human_review = interrupt(\n",
" {\n",
" \"question\": \"Is this correct?\",\n",
" \"tool_call\": tool_call,\n",
" }\n",
" )\n",
" review_action = human_review[\"action\"]\n",
" review_data = human_review.get(\"data\")\n",
" if review_action == \"continue\":\n",
" return tool_call\n",
" elif review_action == \"update\":\n",
" updated_tool_call = {**tool_call, **{\"args\": review_data}}\n",
" return updated_tool_call\n",
" elif review_action == \"feedback\":\n",
" return ToolMessage(\n",
" content=review_data, name=tool_call[\"name\"], tool_call_id=tool_call[\"id\"]\n",
" )\n",
"```\n",
"\n",
"## Setup\n",
"\n",
"First, let's install the required packages and set our API keys:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain-openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for better debugging</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM aps built with LangGraph — read more about how to get started in the <a href=\"https://docs.smith.langchain.com\">docs</a>. \n",
" </p>\n",
" </div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define model and tools\n",
"\n",
"Let's first define the tools and model we will use for our example. As in the [ReAct agent guide](../../how-tos/react-agent-from-scratch-functional), we will use a single place-holder tool that gets a description of the weather for a location.\n",
"\n",
"We will use an [OpenAI](https://python.langchain.com/docs/integrations/providers/openai/) chat model for this example, but any model [supporting tool-calling](https://python.langchain.com/docs/integrations/chat/) will suffice."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.tools import tool\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"\n",
"@tool\n",
"def get_weather(location: str):\n",
" \"\"\"Call to get the weather from a specific location.\"\"\"\n",
" # This is a placeholder for the actual implementation\n",
" if any([city in location.lower() for city in [\"sf\", \"san francisco\"]]):\n",
" return \"It's sunny!\"\n",
" elif \"boston\" in location.lower():\n",
" return \"It's rainy!\"\n",
" else:\n",
" return f\"I am not sure what the weather is in {location}\"\n",
"\n",
"\n",
"tools = [get_weather]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define tasks\n",
"\n",
"Our [tasks](../../concepts/functional_api/#task) are unchanged from the [ReAct agent guide](../../how-tos/react-agent-from-scratch-functional):\n",
"\n",
"1. **Call model**: We want to query our chat model with a list of messages.\n",
"2. **Call tool**: If our model generates tool calls, we want to execute them."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import ToolCall, ToolMessage\n",
"from langgraph.func import entrypoint, task\n",
"\n",
"\n",
"tools_by_name = {tool.name: tool for tool in tools}\n",
"\n",
"\n",
"@task\n",
"def call_model(messages):\n",
" \"\"\"Call model with a sequence of messages.\"\"\"\n",
" response = model.bind_tools(tools).invoke(messages)\n",
" return response\n",
"\n",
"\n",
"@task\n",
"def call_tool(tool_call):\n",
" tool = tools_by_name[tool_call[\"name\"]]\n",
" observation = tool.invoke(tool_call[\"args\"])\n",
" return ToolMessage(content=observation, tool_call_id=tool_call[\"id\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define entrypoint\n",
"\n",
"To review tool calls before execution, we add a `review_tool_call` function that calls [interrupt](../../concepts/human_in_the_loop/#interrupt). When this function is called, execution will be paused until we issue a command to resume it.\n",
"\n",
"Given a tool call, our function will `interrupt` for human review. At that point we can either:\n",
"\n",
"- Accept the tool call;\n",
"- Revise the tool call and continue;\n",
"- Generate a custom tool message (e.g., instructing the model to re-format its tool call).\n",
"\n",
"We will demonstrate these three cases in the [usage examples](#usage) below."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from typing import Union\n",
"\n",
"\n",
"def review_tool_call(tool_call: ToolCall) -> Union[ToolCall, ToolMessage]:\n",
" \"\"\"Review a tool call, returning a validated version.\"\"\"\n",
" human_review = interrupt(\n",
" {\n",
" \"question\": \"Is this correct?\",\n",
" \"tool_call\": tool_call,\n",
" }\n",
" )\n",
" review_action = human_review[\"action\"]\n",
" review_data = human_review.get(\"data\")\n",
" if review_action == \"continue\":\n",
" return tool_call\n",
" elif review_action == \"update\":\n",
" updated_tool_call = {**tool_call, **{\"args\": review_data}}\n",
" return updated_tool_call\n",
" elif review_action == \"feedback\":\n",
" return ToolMessage(\n",
" content=review_data, name=tool_call[\"name\"], tool_call_id=tool_call[\"id\"]\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now update our [entrypoint](../../concepts/functional_api/#entrypoint) to review the generated tool calls. If a tool call is accepted or revised, we execute in the same way as before. Otherwise, we just append the `ToolMessage` supplied by the human.\n",
"\n",
"!!! tip\n",
"\n",
" The results of prior tasks — in this case the initial model call — are persisted, so that they are not run again following the `interrupt`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.graph.message import add_messages\n",
"from langgraph.types import Command, interrupt\n",
"\n",
"\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"@entrypoint(checkpointer=checkpointer)\n",
"def agent(messages, previous):\n",
" if previous is not None:\n",
" messages = add_messages(previous, messages)\n",
"\n",
" llm_response = call_model(messages).result()\n",
" while True:\n",
" if not llm_response.tool_calls:\n",
" break\n",
"\n",
" # Review tool calls\n",
" tool_results = []\n",
" tool_calls = []\n",
" for i, tool_call in enumerate(llm_response.tool_calls):\n",
" review = review_tool_call(tool_call)\n",
" if isinstance(review, ToolMessage):\n",
" tool_results.append(review)\n",
" else: # is a validated tool call\n",
" tool_calls.append(review)\n",
" if review != tool_call:\n",
" llm_response.tool_calls[i] = review # update message\n",
"\n",
" # Execute remaining tool calls\n",
" tool_result_futures = [call_tool(tool_call) for tool_call in tool_calls]\n",
" remaining_tool_results = [fut.result() for fut in tool_result_futures]\n",
"\n",
" # Append to message list\n",
" messages = add_messages(\n",
" messages,\n",
" [llm_response, *tool_results, *remaining_tool_results],\n",
" )\n",
"\n",
" # Call model again\n",
" llm_response = call_model(messages).result()\n",
"\n",
" # Generate final response\n",
" messages = add_messages(messages, llm_response)\n",
" return entrypoint.final(value=llm_response, save=messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Usage\n",
"\n",
"Let's demonstrate some scenarios."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def _print_step(step: dict) -> None:\n",
" for task_name, result in step.items():\n",
" if task_name == \"agent\":\n",
" continue # just stream from tasks\n",
" print(f\"\\n{task_name}:\")\n",
" if task_name in (\"__interrupt__\", \"review_tool_call\"):\n",
" print(result)\n",
" else:\n",
" result.pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accept a tool call\n",
"\n",
"To accept a tool call, we just indicate in the data we provide in the `Command` that the tool call should pass through."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': \"What's the weather in san francisco?\"}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_Bh5cSwMqCpCxTjx7AjdrQTPd)\n",
" Call ID: call_Bh5cSwMqCpCxTjx7AjdrQTPd\n",
" Args:\n",
" location: San Francisco\n",
"\n",
"__interrupt__:\n",
"(Interrupt(value={'question': 'Is this correct?', 'tool_call': {'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'call_Bh5cSwMqCpCxTjx7AjdrQTPd', 'type': 'tool_call'}}, resumable=True, ns=['agent:22fcc9cd-3573-b39b-eea7-272a025903e2'], when='during'),)\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"What's the weather in san francisco?\"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message], config):\n",
" _print_step(step)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's sunny!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather in San Francisco is sunny!\n"
]
}
],
"source": [
"# highlight-next-line\n",
"human_input = Command(resume={\"action\": \"continue\"})\n",
"\n",
"for step in agent.stream(human_input, config):\n",
" _print_step(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Revise a tool call\n",
"\n",
"To revise a tool call, we can supply updated arguments."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"2\"}}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': \"What's the weather in san francisco?\"}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_b9h8e18FqH0IQm3NMoeYKz6N)\n",
" Call ID: call_b9h8e18FqH0IQm3NMoeYKz6N\n",
" Args:\n",
" location: san francisco\n",
"\n",
"__interrupt__:\n",
"(Interrupt(value={'question': 'Is this correct?', 'tool_call': {'name': 'get_weather', 'args': {'location': 'san francisco'}, 'id': 'call_b9h8e18FqH0IQm3NMoeYKz6N', 'type': 'tool_call'}}, resumable=True, ns=['agent:9559a81d-5720-dc19-a457-457bac7bdd83'], when='during'),)\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"What's the weather in san francisco?\"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message], config):\n",
" _print_step(step)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's sunny!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather in San Francisco is sunny!\n"
]
}
],
"source": [
"# highlight-next-line\n",
"human_input = Command(resume={\"action\": \"update\", \"data\": {\"location\": \"SF, CA\"}})\n",
"\n",
"for step in agent.stream(human_input, config):\n",
" _print_step(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The LangSmith traces for this run are particularly informative:\n",
"\n",
"- In the trace [before the interrupt](https://smith.langchain.com/public/c8b07579-5cf4-4adb-a849-282163bc9d99/r/b5b128d6-e715-480b-b58d-59e64f724275), we generate a tool call for location `\"San Francisco\"`.\n",
"- In the trace [after resuming](https://smith.langchain.com/public/b28b92e5-a555-482d-aa4d-c675a19f0eb5/r), we see that the tool call in the message has been updated to `\"SF, CA\"`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generate a custom ToolMessage\n",
"\n",
"To Generate a custom `ToolMessage`, we supply the content of the message. In this case we will ask the model to reformat its tool call."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"3\"}}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': \"What's the weather in san francisco?\"}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_VqGjKE7uu8HdWs9XuY1kMV18)\n",
" Call ID: call_VqGjKE7uu8HdWs9XuY1kMV18\n",
" Args:\n",
" location: San Francisco\n",
"\n",
"__interrupt__:\n",
"(Interrupt(value={'question': 'Is this correct?', 'tool_call': {'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'call_VqGjKE7uu8HdWs9XuY1kMV18', 'type': 'tool_call'}}, resumable=True, ns=['agent:4b3b372b-9da3-70be-5c68-3d9317346070'], when='during'),)\n"
]
}
],
"source": [
"user_message = {\"role\": \"user\", \"content\": \"What's the weather in san francisco?\"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message], config):\n",
" _print_step(step)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_weather (call_xoXkK8Cz0zIpvWs78qnXpvYp)\n",
" Call ID: call_xoXkK8Cz0zIpvWs78qnXpvYp\n",
" Args:\n",
" location: San Francisco, CA\n",
"\n",
"__interrupt__:\n",
"(Interrupt(value={'question': 'Is this correct?', 'tool_call': {'name': 'get_weather', 'args': {'location': 'San Francisco, CA'}, 'id': 'call_xoXkK8Cz0zIpvWs78qnXpvYp', 'type': 'tool_call'}}, resumable=True, ns=['agent:4b3b372b-9da3-70be-5c68-3d9317346070'], when='during'),)\n"
]
}
],
"source": [
"# highlight-next-line\n",
"human_input = Command(\n",
" # highlight-next-line\n",
" resume={\n",
" # highlight-next-line\n",
" \"action\": \"feedback\",\n",
" # highlight-next-line\n",
" \"data\": \"Please format as <City>, <State>.\",\n",
" # highlight-next-line\n",
" },\n",
" # highlight-next-line\n",
")\n",
"\n",
"for step in agent.stream(human_input, config):\n",
" _print_step(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once it is re-formatted, we can accept it:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"It's sunny!\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather in San Francisco, CA is sunny!\n"
]
}
],
"source": [
"# highlight-next-line\n",
"human_input = Command(resume={\"action\": \"continue\"})\n",
"\n",
"for step in agent.stream(human_input, config):\n",
" _print_step(step)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
+1 -1
View File
@@ -217,7 +217,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll be using a small chat model from Anthropic in our example. To use chat models with tool calling, we need to first ensure that the model is aware of the available tools. We do this by calling `.bind_tools` method on `ChatAnthropic` moodel"
"We'll be using a small chat model from Anthropic in our example. To use chat models with tool calling, we need to first ensure that the model is aware of the available tools. We do this by calling `.bind_tools` method on `ChatAnthropic` model"
]
},
{
@@ -220,7 +220,7 @@
"metadata": {},
"outputs": [],
"source": [
"def state_modifier(state: State):\n",
"def prompt(state: State):\n",
" user_info = state.get(\"user_info\")\n",
" if user_info is None:\n",
" return state[\"messages\"]\n",
@@ -265,7 +265,7 @@
" [lookup_user_info],\n",
" state_schema=State,\n",
" # pass dynamic prompt function\n",
" state_modifier=state_modifier,\n",
" prompt=prompt,\n",
")"
]
},
@@ -0,0 +1,561 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to wait for user input (Functional API)\n",
"\n",
"!!! info \"Prerequisites\"\n",
" This guide assumes familiarity with the following:\n",
"\n",
" - Implementing [human-in-the-loop](../../concepts/human_in_the_loop) workflows with [interrupt](../../concepts/human_in_the_loop/#interrupt)\n",
" - [How to create a ReAct agent using the Functional API](../../how-tos/react-agent-from-scratch-functional)\n",
"\n",
"**Human-in-the-loop (HIL)** interactions are crucial for [agentic systems](../../concepts/agentic_concepts/#human-in-the-loop). Waiting for human input is a common HIL interaction pattern, allowing the agent to ask the user clarifying questions and await input before proceeding. \n",
"\n",
"We can implement this in LangGraph using the [interrupt()][langgraph.types.interrupt] function. `interrupt` allows us to stop graph execution to collect input from a user and continue execution with collected input.\n",
"\n",
"This guide demonstrates how to implement human-in-the-loop workflows using LangGraph's [Functional API](../../concepts/functional_api). Specifically, we will demonstrate:\n",
"\n",
"1. [A simple usage example](#simple-usage)\n",
"2. [How to use with a ReAct agent](#agent)\n",
"\n",
"## Setup\n",
"\n",
"First, let's install the required packages and set our API keys:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"%pip install -U langgraph langchain-openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"\n",
"def _set_env(var: str):\n",
" if not os.environ.get(var):\n",
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
"\n",
"\n",
"_set_env(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Set up <a href=\"https://smith.langchain.com\">LangSmith</a> for better debugging</p>\n",
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM aps built with LangGraph — read more about how to get started in the <a href=\"https://docs.smith.langchain.com\">docs</a>. \n",
" </p>\n",
" </div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple usage\n",
"\n",
"Let's demonstrate a simple usage example. We will create three [tasks](../../concepts/functional_api/#task):\n",
"\n",
"1. Append `\"bar\"`.\n",
"2. Pause for human input. When resuming, append human input.\n",
"3. Append `\"qux\"`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.func import entrypoint, task\n",
"from langgraph.types import Command, interrupt\n",
"\n",
"\n",
"@task\n",
"def step_1(input_query):\n",
" \"\"\"Append bar.\"\"\"\n",
" return f\"{input_query} bar\"\n",
"\n",
"\n",
"@task\n",
"def human_feedback(input_query):\n",
" \"\"\"Append user input.\"\"\"\n",
" feedback = interrupt(f\"Please provide feedback: {input_query}\")\n",
" return f\"{input_query} {feedback}\"\n",
"\n",
"\n",
"@task\n",
"def step_3(input_query):\n",
" \"\"\"Append qux.\"\"\"\n",
" return f\"{input_query} qux\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now compose these tasks in a simple [entrypoint](../../concepts/functional_api/#entrypoint):"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.checkpoint.memory import MemorySaver\n",
"\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"@entrypoint(checkpointer=checkpointer)\n",
"def graph(input_query):\n",
" result_1 = step_1(input_query).result()\n",
" result_2 = human_feedback(result_1).result()\n",
" result_3 = step_3(result_2).result()\n",
"\n",
" return result_3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All we have done to enable human-in-the-loop workflows is called [interrupt()](../../concepts/human_in_the_loop/#interrupt) inside a task.\n",
"\n",
"!!! tip\n",
"\n",
" The results of prior tasks-- in this case `step_1`-- are persisted, so that they are not run again following the `interrupt`.\n",
"\n",
"\n",
"Let's send in a query string:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'step_1': 'foo bar'}\n",
"\n",
"\n",
"{'__interrupt__': (Interrupt(value='Please provide feedback: foo bar', resumable=True, ns=['graph:d66b2e35-0ee3-d8d6-1a22-aec9d58f13b9', 'human_feedback:e0cd4ee2-b874-e1d2-8bc4-3f7ddc06bcc2'], when='during'),)}\n",
"\n",
"\n"
]
}
],
"source": [
"for event in graph.stream(\"foo\", config):\n",
" print(event)\n",
" print(\"\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we've paused with an `interrupt` after `step_1`. The interrupt provides instructions to resume the run. To resume, we issue a [Command](../../concepts/human_in_the_loop/#the-command-primitive) containing the data expected by the `human_feedback` task."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'human_feedback': 'foo bar baz'}\n",
"\n",
"\n",
"{'step_3': 'foo bar baz qux'}\n",
"\n",
"\n",
"{'graph': 'foo bar baz qux'}\n",
"\n",
"\n"
]
}
],
"source": [
"# Continue execution\n",
"for event in graph.stream(Command(resume=\"baz\"), config):\n",
" print(event)\n",
" print(\"\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After resuming, the run proceeds through the remaining step and terminates as expected."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Agent\n",
"\n",
"We will build off of the agent created in the [How to create a ReAct agent using the Functional API](../../how-tos/react-agent-from-scratch-functional) guide.\n",
"\n",
"Here we will extend the agent by allowing it to reach out to a human for assistance when needed.\n",
"\n",
"### Define model and tools\n",
"\n",
"Let's first define the tools and model we will use for our example. As in the [ReAct agent guide](../../how-tos/react-agent-from-scratch-functional), we will use a single place-holder tool that gets a description of the weather for a location.\n",
"\n",
"We will use an [OpenAI](https://python.langchain.com/docs/integrations/providers/openai/) chat model for this example, but any model [supporting tool-calling](https://python.langchain.com/docs/integrations/chat/) will suffice."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.tools import tool\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"\n",
"@tool\n",
"def get_weather(location: str):\n",
" \"\"\"Call to get the weather from a specific location.\"\"\"\n",
" # This is a placeholder for the actual implementation\n",
" if any([city in location.lower() for city in [\"sf\", \"san francisco\"]]):\n",
" return \"It's sunny!\"\n",
" elif \"boston\" in location.lower():\n",
" return \"It's rainy!\"\n",
" else:\n",
" return f\"I am not sure what the weather is in {location}\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To reach out to a human for assistance, we can simply add a tool that calls [interrupt](../../concepts/human_in_the_loop/#interrupt):"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.types import Command, interrupt\n",
"\n",
"\n",
"@tool\n",
"def human_assistance(query: str) -> str:\n",
" \"\"\"Request assistance from a human.\"\"\"\n",
" human_response = interrupt({\"query\": query})\n",
" return human_response[\"data\"]\n",
"\n",
"\n",
"tools = [get_weather, human_assistance]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define tasks\n",
"\n",
"Our tasks are otherwise unchanged from the [ReAct agent guide](../../how-tos/react-agent-from-scratch-functional):\n",
"\n",
"1. **Call model**: We want to query our chat model with a list of messages.\n",
"2. **Call tool**: If our model generates tool calls, we want to execute them.\n",
"\n",
"We just have one more tool accessible to the model."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import ToolMessage\n",
"from langgraph.func import entrypoint, task\n",
"\n",
"tools_by_name = {tool.name: tool for tool in tools}\n",
"\n",
"\n",
"@task\n",
"def call_model(messages):\n",
" \"\"\"Call model with a sequence of messages.\"\"\"\n",
" response = model.bind_tools(tools).invoke(messages)\n",
" return response\n",
"\n",
"\n",
"@task\n",
"def call_tool(tool_call):\n",
" tool = tools_by_name[tool_call[\"name\"]]\n",
" observation = tool.invoke(tool_call)\n",
" return ToolMessage(content=observation, tool_call_id=tool_call[\"id\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define entrypoint\n",
"\n",
"Our [entrypoint](../../concepts/functional_api/#entrypoint) is also unchanged from the [ReAct agent guide](../../how-tos/react-agent-from-scratch-functional):"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.graph.message import add_messages\n",
"\n",
"checkpointer = MemorySaver()\n",
"\n",
"\n",
"@entrypoint(checkpointer=checkpointer)\n",
"def agent(messages, previous):\n",
" if previous is not None:\n",
" messages = add_messages(previous, messages)\n",
"\n",
" llm_response = call_model(messages).result()\n",
" while True:\n",
" if not llm_response.tool_calls:\n",
" break\n",
"\n",
" # Execute tools\n",
" tool_result_futures = [\n",
" call_tool(tool_call) for tool_call in llm_response.tool_calls\n",
" ]\n",
" tool_results = [fut.result() for fut in tool_result_futures]\n",
"\n",
" # Append to message list\n",
" messages = add_messages(messages, [llm_response, *tool_results])\n",
"\n",
" # Call model again\n",
" llm_response = call_model(messages).result()\n",
"\n",
" # Generate final response\n",
" messages = add_messages(messages, llm_response)\n",
" return entrypoint.final(value=llm_response, save=messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Usage\n",
"\n",
"Let's invoke our model with a question that requires human assistance. Our question will also require an invocation of the `get_weather` tool:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def _print_step(step: dict) -> None:\n",
" for task_name, result in step.items():\n",
" if task_name == \"agent\":\n",
" continue # just stream from tasks\n",
" print(f\"\\n{task_name}:\")\n",
" if task_name == \"__interrupt__\":\n",
" print(result)\n",
" else:\n",
" result.pretty_print()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"config = {\"configurable\": {\"thread_id\": \"1\"}}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'user', 'content': 'Can you reach out for human assistance: what should I feed my cat? Separately, can you check the weather in San Francisco?'}\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" human_assistance (call_joAEBVX7Abfm7TsZ0k95ZkVx)\n",
" Call ID: call_joAEBVX7Abfm7TsZ0k95ZkVx\n",
" Args:\n",
" query: What should I feed my cat?\n",
" get_weather (call_ut7zfHFCcms63BOZLrRHszGH)\n",
" Call ID: call_ut7zfHFCcms63BOZLrRHszGH\n",
" Args:\n",
" location: San Francisco\n",
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"content=\"It's sunny!\" name='get_weather' tool_call_id='call_ut7zfHFCcms63BOZLrRHszGH'\n",
"\n",
"__interrupt__:\n",
"(Interrupt(value={'query': 'What should I feed my cat?'}, resumable=True, ns=['agent:aa676ccc-b038-25e3-9c8a-18e81d4e1372', 'call_tool:059d53d2-3344-13bc-e170-48b632c2dd97'], when='during'),)\n"
]
}
],
"source": [
"user_message = {\n",
" \"role\": \"user\",\n",
" \"content\": (\n",
" \"Can you reach out for human assistance: what should I feed my cat? \"\n",
" \"Separately, can you check the weather in San Francisco?\"\n",
" ),\n",
"}\n",
"print(user_message)\n",
"\n",
"for step in agent.stream([user_message], config):\n",
" _print_step(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we generate two tool calls, and although our run is interrupted, we did not block the execution of the `get_weather` tool.\n",
"\n",
"Let's inspect where we're interrupted:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'__interrupt__': (Interrupt(value={'query': 'What should I feed my cat?'}, resumable=True, ns=['agent:aa676ccc-b038-25e3-9c8a-18e81d4e1372', 'call_tool:059d53d2-3344-13bc-e170-48b632c2dd97'], when='during'),)}\n"
]
}
],
"source": [
"print(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can resume execution by issuing a [Command](../../concepts/human_in_the_loop/#the-command-primitive). Note that the data we supply in the `Command` can be customized to your needs based on the implementation of `human_assistance`."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"call_tool:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"\n",
"content='You should feed your cat a fish.' name='human_assistance' tool_call_id='call_joAEBVX7Abfm7TsZ0k95ZkVx'\n",
"\n",
"call_model:\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"For human assistance, you should feed your cat fish. \n",
"\n",
"Regarding the weather in San Francisco, it's sunny!\n"
]
}
],
"source": [
"human_response = \"You should feed your cat a fish.\"\n",
"human_command = Command(resume={\"data\": human_response})\n",
"\n",
"for step in agent.stream(human_command, config):\n",
" _print_step(step)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above, when we resume we provide the final tool message, allowing the model to generate its response. Check out the LangSmith traces to see a full breakdown of the runs:\n",
"\n",
"1. [Trace from initial query](https://smith.langchain.com/public/c3d8879d-4d01-41be-807e-6d9eed15df99/r)\n",
"2. [Trace after resuming](https://smith.langchain.com/public/97c05ef9-8b4c-428e-8826-3fd417c8c75f/r)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
+5
View File
@@ -0,0 +1,5 @@
::: langgraph.config
options:
members:
- get_store
- get_stream_writer
+9
View File
@@ -0,0 +1,9 @@
::: langgraph.pregel.Pregel
options:
members:
- stream
- astream
- invoke
- ainvoke
- update_state
- aupdate_state
+1
View File
@@ -9,6 +9,7 @@ New to LangGraph or LLM app development? Read this material to get up and runnin
## Get Started 🚀 {#quick-start}
- [LangGraph Quickstart](introduction.ipynb): Build a chatbot that can use tools and keep track of conversation history. Add human-in-the-loop capabilities and explore how time-travel works.
- [Common Workflows](workflows/index.md): Overview of the most common workflows using LLMs implemented with LangGraph.
- [LangGraph Server Quickstart](langgraph-platform/local-server.md): Launch a LangGraph server locally and interact with it using REST API and LangGraph Studio Web UI.
- [LangGraph Template Quickstart](../concepts/template_applications.md): Start building with LangGraph Platform using a template application.
- [Deploy with LangGraph Cloud Quickstart](../cloud/quick_start.md): Deploy a LangGraph app using LangGraph Cloud.
@@ -201,7 +201,7 @@
"\n",
"\n",
"research_agent = create_react_agent(\n",
" llm, tools=[tavily_tool], state_modifier=\"You are a researcher. DO NOT do any math.\"\n",
" llm, tools=[tavily_tool], prompt=\"You are a researcher. DO NOT do any math.\"\n",
")\n",
"\n",
"\n",
@@ -525,7 +525,7 @@
"doc_writer_agent = create_react_agent(\n",
" llm,\n",
" tools=[write_document, edit_document, read_document],\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You can read, write and edit documents based on note-taker's outlines. \"\n",
" \"Don't ask follow-up questions.\"\n",
" ),\n",
@@ -548,7 +548,7 @@
"note_taking_agent = create_react_agent(\n",
" llm,\n",
" tools=[create_outline, read_document],\n",
" state_modifier=(\n",
" prompt=(\n",
" \"You can read documents and create outlines for the document writer. \"\n",
" \"Don't ask follow-up questions.\"\n",
" ),\n",
@@ -14,7 +14,7 @@
"\n",
"A single agent can usually operate effectively using a handful of tools within a single domain, but even using powerful models like `gpt-4`, it can be less effective at using many tools. \n",
"\n",
"One way to approach complicated tasks is through a \"divide-and-conquer\" approach: create an specialized agent for each task or domain and route tasks to the correct \"expert\". This is an example of a [multi-agent network](https://langchain-ai.github.io/langgraph/concepts/multi_agent/#network) architecture.\n",
"One way to approach complicated tasks is through a \"divide-and-conquer\" approach: create a specialized agent for each task or domain and route tasks to the correct \"expert\". This is an example of a [multi-agent network](https://langchain-ai.github.io/langgraph/concepts/multi_agent/#network) architecture.\n",
"\n",
"This notebook (inspired by the paper [AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation](https://arxiv.org/abs/2308.08155), by Wu, et. al.) shows one way to do this using LangGraph.\n",
"\n",
@@ -190,7 +190,7 @@
"research_agent = create_react_agent(\n",
" llm,\n",
" tools=[tavily_tool],\n",
" state_modifier=make_system_prompt(\n",
" prompt=make_system_prompt(\n",
" \"You can only do research. You are working with a chart generator colleague.\"\n",
" ),\n",
")\n",
@@ -220,7 +220,7 @@
"chart_agent = create_react_agent(\n",
" llm,\n",
" [python_repl_tool],\n",
" state_modifier=make_system_prompt(\n",
" prompt=make_system_prompt(\n",
" \"You can only generate charts. You are working with a researcher colleague.\"\n",
" ),\n",
")\n",
@@ -130,37 +130,20 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "72d233ca-1dbf-4b43-b680-b3bf39e3691f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m System Message \u001b[0m================================\n",
"\n",
"You are a helpful assistant.\n",
"\n",
"=============================\u001b[1m Messages Placeholder \u001b[0m=============================\n",
"\n",
"\u001b[33;1m\u001b[1;3m{messages}\u001b[0m\n"
]
}
],
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Get the prompt to use - you can modify this!\n",
"prompt = hub.pull(\"ih/ih-react-agent-executor\")\n",
"prompt.pretty_print()\n",
"\n",
"# Choose the LLM that will drive the agent\n",
"llm = ChatOpenAI(model=\"gpt-4-turbo-preview\")\n",
"agent_executor = create_react_agent(llm, tools, state_modifier=prompt)"
"prompt = \"You are a helpful assistant.\"\n",
"agent_executor = create_react_agent(llm, tools, prompt=prompt)"
]
},
{
@@ -546,7 +529,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
}
},
"nbformat": 4,
Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

File diff suppressed because it is too large Load Diff
+11
View File
@@ -118,6 +118,8 @@ nav:
- how-tos/persistence_postgres.ipynb
- how-tos/persistence_mongodb.ipynb
- how-tos/persistence_redis.ipynb
- how-tos/persistence-functional.ipynb
- how-tos/cross-thread-persistence-functional.ipynb
- Memory:
- Memory: how-tos#memory
- how-tos/memory/manage-conversation-history.ipynb
@@ -132,6 +134,8 @@ nav:
- how-tos/human_in_the_loop/wait-user-input.ipynb
- how-tos/human_in_the_loop/time-travel.ipynb
- how-tos/human_in_the_loop/review-tool-calls.ipynb
- how-tos/wait-user-input-functional.ipynb
- how-tos/review-tool-calls-functional.ipynb
- Streaming:
- Streaming: how-tos#streaming
- how-tos/stream-values.ipynb
@@ -163,6 +167,8 @@ nav:
- how-tos/agent-handoffs.ipynb
- how-tos/multi-agent-network.ipynb
- how-tos/multi-agent-multi-turn-convo.ipynb
- how-tos/multi-agent-network-functional.ipynb
- how-tos/multi-agent-multi-turn-convo-functional.ipynb
- State Management:
- State Management: how-tos#state-management
- how-tos/state-model.ipynb
@@ -185,6 +191,7 @@ nav:
- how-tos/create-react-agent-hitl.ipynb
- how-tos/create-react-agent-structured-output.ipynb
- how-tos/react-agent-from-scratch.ipynb
- how-tos/react-agent-from-scratch-functional.ipynb
- LangGraph Platform:
- LangGraph Platform: how-tos#langgraph-platform
- Application Structure:
@@ -265,6 +272,7 @@ nav:
- concepts/persistence.md
- concepts/memory.md
- concepts/streaming.md
- concepts/functional_api.md
- LangGraph Platform:
- LangGraph Platform: concepts#langgraph-platform
- High Level:
@@ -296,6 +304,7 @@ nav:
- Quick Start:
- Quick Start: tutorials#quick-start
- tutorials/introduction.ipynb
- tutorials/workflows/index.md
- tutorials/langgraph-platform/local-server.md
- cloud/quick_start.md
- Chatbots:
@@ -370,6 +379,8 @@ nav:
- Errors: reference/errors.md
- Types: reference/types.md
- Constants: reference/constants.md
- Pregel: reference/pregel.md
- Config: reference/config.md
- Functional API: reference/func.md
- LangGraph Platform:
- Server API: "cloud/reference/api/api_ref.md"
+53 -2
View File
@@ -183,6 +183,16 @@ def validate_config(config: Config) -> Config:
"No graphs found in config. "
"Add at least one graph to 'graphs' dictionary."
)
# Validate auth config
if auth_conf := config.get("auth"):
if "path" in auth_conf:
if ":" not in auth_conf["path"]:
raise ValueError(
f"Invalid auth.path format: '{auth_conf['path']}'. "
"Must be in format './path/to/file.py:attribute_name'"
)
return config
@@ -330,7 +340,7 @@ def _assemble_local_deps(config_path: pathlib.Path, config: Config) -> LocalDeps
rfile = resolved / "requirements.txt"
pip_reqs.append(
(
rfile.relative_to(config_path.parent),
rfile.relative_to(config_path.parent).as_posix(),
f"{container_path}/requirements.txt",
)
)
@@ -373,6 +383,47 @@ def _update_graph_paths(
config["graphs"][graph_id] = f"{module_str}:{attr_str}"
def _update_auth_path(
config_path: pathlib.Path, config: Config, local_deps: LocalDeps
) -> None:
"""Update auth.path to use Docker container paths."""
auth_conf = config.get("auth")
if not auth_conf or not (path_str := auth_conf.get("path")):
return
module_str, sep, attr_str = path_str.partition(":")
if not sep or not module_str.startswith("."):
return # Already validated or absolute path
resolved = config_path.parent / module_str
if not resolved.exists():
raise FileNotFoundError(f"Auth file not found: {resolved} (from {path_str})")
if not resolved.is_file():
raise IsADirectoryError(f"Auth path must be a file: {resolved}")
# Check faux packages first (higher priority)
for faux_path, (_, destpath) in local_deps.faux_pkgs.items():
if resolved.is_relative_to(faux_path):
new_path = f"{destpath}/{resolved.relative_to(faux_path)}:{attr_str}"
auth_conf["path"] = new_path
return
# Check real packages
for real_path in local_deps.real_pkgs:
if resolved.is_relative_to(real_path):
new_path = (
f"/deps/{real_path.name}/{resolved.relative_to(real_path)}:{attr_str}"
)
auth_conf["path"] = new_path
return
raise ValueError(
f"Auth file '{resolved}' not covered by dependencies.\n"
"Add its parent directory to the 'dependencies' array in your config.\n"
f"Current dependencies: {config['dependencies']}"
)
def python_config_to_docker(config_path: pathlib.Path, config: Config, base_image: str):
# configure pip
pip_install = (
@@ -389,9 +440,9 @@ def python_config_to_docker(config_path: pathlib.Path, config: Config, base_imag
# collect dependencies
pypi_deps = [dep for dep in config["dependencies"] if not dep.startswith(".")]
local_deps = _assemble_local_deps(config_path, config)
# rewrite graph paths
_update_graph_paths(config_path, config, local_deps)
_update_auth_path(config_path, config, local_deps)
pip_pkgs_str = f"RUN {pip_install} {' '.join(pypi_deps)}" if pypi_deps else ""
if local_deps.pip_reqs:
+2 -1
View File
@@ -170,13 +170,14 @@ def compose_as_dict(
# Add Postgres service before langgraph-api if it is needed
if include_db:
services["langgraph-postgres"] = {
"image": "postgres:16",
"image": "pgvector/pgvector:pg16",
"ports": ['"5433:5432"'],
"environment": {
"POSTGRES_DB": "postgres",
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "postgres",
},
"command": ["postgres", "-c", "shared_preload_libraries=vector"],
"volumes": ["langgraph-data:/var/lib/postgresql/data"],
"healthcheck": {
"test": "pg_isready -U postgres",
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langgraph-cli"
version = "0.1.68"
version = "0.1.70"
description = "CLI for interacting with LangGraph API"
authors = []
license = "MIT"
+5 -1
View File
@@ -79,13 +79,17 @@ services:
timeout: 1s
retries: 5
langgraph-postgres:
image: postgres:16
image: pgvector/pgvector:pg16
ports:
- "5433:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
command:
- postgres
- -c
- shared_preload_libraries=vector
volumes:
- langgraph-data:/var/lib/postgresql/data
healthcheck:
+5 -1
View File
@@ -115,13 +115,17 @@ services:
timeout: 1s
retries: 5
langgraph-postgres:
image: postgres:16
image: pgvector/pgvector:pg16
ports:
- "5433:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
command:
- postgres
- -c
- shared_preload_libraries=vector
volumes:
- langgraph-data:/var/lib/postgresql/data
healthcheck:
+7 -6
View File
@@ -21,10 +21,7 @@ LangGraph is inspired by [Pregel](https://research.google/pubs/pub37252/) and [A
### Why use LangGraph?
LangGraph provides fine-grained control over both the flow and state of your
agent applications. It implements a central
[persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/),
enabling features that are common to most agent architectures:
LangGraph powers [production-grade agents](https://www.langchain.com/built-with-langgraph), trusted by Linkedin, Uber, Klarna, GitLab, and many more. LangGraph provides fine-grained control over both the flow and state of your agent applications. It implements a central [persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/), enabling features that are common to most agent architectures:
- **Memory**: LangGraph persists arbitrary aspects of your application's state,
supporting memory of conversations and other updates within and across user
@@ -245,7 +242,7 @@ final_state["messages"][-1].content
We use <code>ChatAnthropic</code> as our LLM. <strong>NOTE:</strong> we need to make sure the model knows that it has these tools available to call. We can do this by converting the LangChain tools into the format for OpenAI tool calling using the <code>.bind_tools()</code> method.
</li>
<li>
We define the tools we want to use - a search tool in our case. It is really easy to create your own tools - see documentation here on how to do that <a href="https://python.langchain.com/docs/modules/agents/tools/custom_tools">here</a>.
We define the tools we want to use - a search tool in our case. It is really easy to create your own tools - see documentation here on how to do that <a href="https://python.langchain.com/docs/how_to/custom_tools/">here</a>.
</li>
</ul>
</details>
@@ -294,7 +291,7 @@ Then we define one normal and one conditional edge. Conditional edge means that
<ul>
<li>
When we compile the graph, we turn it into a LangChain
<a href="https://python.langchain.com/v0.2/docs/concepts/#runnable-interface">Runnable</a>,
<a href="https://python.langchain.com/docs/concepts/runnables/">Runnable</a>,
which automatically enables calling <code>.invoke()</code>, <code>.stream()</code> and <code>.batch()</code>
with your inputs
</li>
@@ -333,6 +330,10 @@ Then we define one normal and one conditional edge. Conditional edge means that
* [API Reference](https://langchain-ai.github.io/langgraph/reference/graphs/): Review important classes and methods, simple examples of how to use the graph and checkpointing APIs, higher-level prebuilt components and more.
* [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/#langgraph-platform): LangGraph Platform is a commercial solution for deploying agentic applications in production, built on the open-source LangGraph framework.
## Resources
* [Built with LangGraph](https://www.langchain.com/built-with-langgraph): Hear how industry leaders use LangGraph to ship powerful, production-ready AI applications.
## Contributing
For more information on how to contribute, see [here](https://github.com/langchain-ai/langgraph/blob/main/CONTRIBUTING.md).
+180
View File
@@ -0,0 +1,180 @@
import asyncio
import sys
from langchain_core.runnables import RunnableConfig
from langchain_core.runnables.config import var_child_runnable_config
from langgraph.constants import CONF, CONFIG_KEY_STORE, CONFIG_KEY_STREAM_WRITER
from langgraph.store.base import BaseStore
from langgraph.types import StreamWriter
def get_config() -> RunnableConfig:
if sys.version_info < (3, 11):
try:
if asyncio.current_task():
raise RuntimeError(
"Python 3.11 or later required to use this in an async context"
)
except RuntimeError:
pass
if var_config := var_child_runnable_config.get():
return var_config
else:
raise RuntimeError("Called get_config outside of a runnable context")
def get_store() -> BaseStore:
"""Access LangGraph store from inside a graph node or entrypoint task at runtime.
Can be called from inside any [StateGraph][langgraph.graph.StateGraph] node or
functional API [task][langgraph.func.task], as long as the StateGraph or the [entrypoint][langgraph.func.entrypoint]
was initialized with a store, e.g.:
```python
# with StateGraph
graph = (
StateGraph(...)
...
.compile(store=store)
)
# or with entrypoint
@entrypoint(store=store)
def workflow(inputs):
...
```
!!! warning "Async with Python < 3.11"
If you are using Python < 3.11 and are running LangGraph asynchronously,
`get_store()` won't work since it uses [contextvar](https://docs.python.org/3/library/contextvars.html) propagation (only available in [Python >= 3.11](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task)).
Example: Using with StateGraph
```python
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.store.memory import InMemoryStore
from langgraph.config import get_store
store = InMemoryStore()
store.put(("values",), "foo", {"bar": 2})
class State(TypedDict):
foo: int
def my_node(state: State):
my_store = get_store()
stored_value = my_store.get(("values",), "foo").value["bar"]
return {"foo": stored_value + 1}
graph = (
StateGraph(State)
.add_node(my_node)
.add_edge(START, "my_node")
.compile(store=store)
)
graph.invoke({"foo": 1})
```
```pycon
{'foo': 3}
```
Example: Using with functional API
```python
from langgraph.func import entrypoint, task
from langgraph.store.memory import InMemoryStore
from langgraph.config import get_store
store = InMemoryStore()
store.put(("values",), "foo", {"bar": 2})
@task
def my_task(value: int):
my_store = get_store()
stored_value = my_store.get(("values",), "foo").value["bar"]
return stored_value + 1
@entrypoint(store=store)
def workflow(value: int):
return my_task(value).result()
workflow.invoke(1)
```
```pycon
3
```
"""
config = get_config()
return config[CONF][CONFIG_KEY_STORE]
def get_stream_writer() -> StreamWriter:
"""Access LangGraph [StreamWriter][langgraph.types.StreamWriter] from inside a graph node or entrypoint task at runtime.
Can be called from inside any [StateGraph][langgraph.graph.StateGraph] node or
functional API [task][langgraph.func.task].
!!! warning "Async with Python < 3.11"
If you are using Python < 3.11 and are running LangGraph asynchronously,
`get_stream_writer()` won't work since it uses [contextvar](https://docs.python.org/3/library/contextvars.html) propagation (only available in [Python >= 3.11](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task)).
Example: Using with StateGraph
```python
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.config import get_stream_writer
class State(TypedDict):
foo: int
def my_node(state: State):
my_stream_writer = get_stream_writer()
my_stream_writer({"custom_data": "Hello!"})
return {"foo": state["foo"] + 1}
graph = (
StateGraph(State)
.add_node(my_node)
.add_edge(START, "my_node")
.compile(store=store)
)
for chunk in graph.stream({"foo": 1}, stream_mode="custom"):
print(chunk)
```
```pycon
{'custom_data': 'Hello!'}
```
Example: Using with functional API
```python
from langgraph.func import entrypoint, task
from langgraph.config import get_stream_writer
@task
def my_task(value: int):
my_stream_writer = get_stream_writer()
my_stream_writer({"custom_data": "Hello!"})
return value + 1
@entrypoint(store=store)
def workflow(value: int):
return my_task(value).result()
for chunk in workflow.stream(1, stream_mode="custom"):
print(chunk)
```
```pycon
{'custom_data': 'Hello!'}
```
"""
config = get_config()
return config[CONF][CONFIG_KEY_STREAM_WRITER]
+2 -1
View File
@@ -23,6 +23,7 @@ END = sys.intern("__end__")
"""The last (maybe virtual) node in graph-style Pregel."""
SELF = sys.intern("__self__")
"""The implicit branch that handles each node's Control values."""
PREVIOUS = sys.intern("__previous__")
# --- Reserved write keys ---
INPUT = sys.intern("__input__")
@@ -78,7 +79,7 @@ CONFIG_KEY_NODE_FINISHED = sys.intern("__pregel_node_finished")
# holds a callback to be called when a node is finished
CONFIG_KEY_SCRATCHPAD = sys.intern("__pregel_scratchpad")
# holds a mutable dict for temporary storage scoped to the current task
CONFIG_KEY_END = sys.intern("__pregel_previous")
CONFIG_KEY_PREVIOUS = sys.intern("__pregel_previous")
# holds the previous return value from a stateful Pregel graph.
# --- Other constants ---
+202 -254
View File
@@ -2,73 +2,63 @@ import asyncio
import concurrent.futures
import functools
import inspect
import types
from collections.abc import Iterator
from dataclasses import dataclass
from typing import (
Any,
Awaitable,
Callable,
Generic,
Optional,
TypeVar,
Union,
get_args,
get_origin,
overload,
)
from langchain_core.runnables.base import Runnable
from langchain_core.runnables.config import RunnableConfig
from langchain_core.runnables.graph import Graph, Node
from langgraph.channels.ephemeral_value import EphemeralValue
from langgraph.channels.last_value import LastValue
from langgraph.checkpoint.base import BaseCheckpointSaver
from langgraph.constants import END, START, TAG_HIDDEN
from langgraph.constants import END, PREVIOUS, START, TAG_HIDDEN
from langgraph.pregel import Pregel
from langgraph.pregel.call import P, T, call, get_runnable_for_entrypoint
from langgraph.pregel.protocol import PregelProtocol
from langgraph.pregel.call import (
P,
SyncAsyncFuture,
T,
call,
get_runnable_for_entrypoint,
)
from langgraph.pregel.read import PregelNode
from langgraph.pregel.write import ChannelWrite, ChannelWriteEntry
from langgraph.store.base import BaseStore
from langgraph.types import RetryPolicy, StreamMode, StreamWriter
from langgraph.types import _DC_KWARGS, RetryPolicy, StreamMode
@overload
def task(
*, retry: Optional[RetryPolicy] = None
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, asyncio.Future[T]]]: ...
@overload
def task( # type: ignore[overload-cannot-match]
*, retry: Optional[RetryPolicy] = None
) -> Callable[[Callable[P, T]], Callable[P, concurrent.futures.Future[T]]]: ...
*, name: Optional[str] = None, retry: Optional[RetryPolicy] = None
) -> Callable[[Callable[P, T]], Callable[P, SyncAsyncFuture[T]]]: ...
@overload
def task(
__func_or_none__: Callable[P, T],
) -> Callable[P, concurrent.futures.Future[T]]: ...
@overload
def task(
__func_or_none__: Callable[P, Awaitable[T]],
) -> Callable[P, asyncio.Future[T]]: ...
) -> Callable[P, SyncAsyncFuture[T]]: ...
def task(
__func_or_none__: Optional[Union[Callable[P, T], Callable[P, Awaitable[T]]]] = None,
*,
name: Optional[str] = None,
retry: Optional[RetryPolicy] = None,
) -> Union[
Callable[[Callable[P, Awaitable[T]]], Callable[P, asyncio.Future[T]]],
Callable[[Callable[P, T]], Callable[P, concurrent.futures.Future[T]]],
Callable[P, asyncio.Future[T]],
Callable[P, concurrent.futures.Future[T]],
Callable[[Callable[P, T]], Callable[P, SyncAsyncFuture[T]]],
Callable[P, SyncAsyncFuture[T]],
]:
"""Define a LangGraph task using the `task` decorator.
!!! warning "Experimental"
This is an experimental API that is subject to change.
Do not use for production code.
!!! warning "Beta"
The Functional API is currently in beta and is subject to change.
!!! important "Requires python 3.11 or higher for async functions"
The `task` decorator supports both sync and async functions. To use async
@@ -130,6 +120,18 @@ def task(
) -> Union[
Callable[P, concurrent.futures.Future[T]], Callable[P, asyncio.Future[T]]
]:
if name is not None:
if hasattr(func, "__func__"):
# handle class methods
# NOTE: we're modifying the instance method to avoid modifying
# the original class method in case it's shared across multiple tasks
instance_method = functools.partial(func.__func__, func.__self__) # type: ignore [union-attr]
instance_method.__name__ = name # type: ignore [attr-defined]
func = instance_method
else:
# handle regular functions / partials / callable classes, etc.
func.__name__ = name
call_func = functools.partial(call, func, retry=retry)
object.__setattr__(call_func, "_is_pregel_task", True)
return functools.update_wrapper(call_func, func)
@@ -140,35 +142,50 @@ def task(
return decorator
def entrypoint(
*,
checkpointer: Optional[BaseCheckpointSaver] = None,
store: Optional[BaseStore] = None,
config_schema: Optional[type[Any]] = None,
) -> Callable[[types.FunctionType], Pregel]:
R = TypeVar("R")
S = TypeVar("S")
# The decorator was wrapped in a class to support the `final` attribute.
# In this form, the `final` attribute should play nicely with IDE autocompletion,
# and type checking tools.
# In addition, we'll be able to surface this information in the API Reference.
class entrypoint:
"""Define a LangGraph workflow using the `entrypoint` decorator.
!!! warning "Experimental"
This is an experimental API that is subject to change.
Do not use for production code.
!!! warning "Beta"
The Functional API is currently in beta and is subject to change.
The decorated function must accept a single parameter, which serves as the input
### Function signature
The decorated function must accept a **single parameter**, which serves as the input
to the function. This input parameter can be of any type. Use a dictionary
to pass multiple parameters to the function.
to pass **multiple parameters** to the function.
The decorated function also has access to these optional parameters:
### Injectable parameters
- `writer`: A `StreamWriter` instance for writing data to a stream.
- `config`: A configuration object for accessing workflow settings.
- `previous`: The previous return value for the given thread (available only when
a checkpointer is provided).
The decorated function can request access to additional parameters
that will be injected automatically at run time. These parameters include:
The entrypoint decorator can be applied to sync functions, async functions,
generator functions, and async generator functions.
| Parameter | Description |
|------------------|----------------------------------------------------------------------------------------------------|
| **`store`** | An instance of [BaseStore][langgraph.store.base.BaseStore]. Useful for long-term memory. |
| **`writer`** | A [StreamWriter][langgraph.types.StreamWriter] instance for writing custom data to a stream. |
| **`config`** | A configuration object (aka RunnableConfig) that holds run-time configuration values. |
| **`previous`** | The previous return value for the given thread (available only when a checkpointer is provided). |
For generator functions, the `previous` parameter will represent a list of
the values previously yielded by the generator. During a run any values yielded
by the generator, will be written to the `custom` stream.
The entrypoint decorator can be applied to sync functions or async functions.
### State management
The **`previous`** parameter can be used to access the return value of the previous
invocation of the entrypoint on the same thread id. This value is only available
when a checkpointer is provided.
If you want **`previous`** to be different from the return value, you can use the
`entrypoint.final` object to return a value while saving a different value to the
checkpoint.
Args:
checkpointer: Specify a checkpointer to create a workflow that can persist
@@ -178,9 +195,6 @@ def entrypoint(
config_schema: Specifies the schema for the configuration object that will be
passed to the workflow.
Returns:
A decorator that converts a function into a Pregel graph.
Example: Using entrypoint and tasks
```python
import time
@@ -250,120 +264,116 @@ def entrypoint(
```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint, task
from langgraph.func import entrypoint
@entrypoint(checkpointer=MemorySaver())
def my_workflow(input_data: str, previous: Optional[str] = None) -> str:
return "world"
# highlight-next-line
config = {
"configurable": {
"thread_id":
"thread_id": "some_thread"
}
}
my_workflow.invoke("hello")
```
Example: Using entrypoint.final to save a value
The `entrypoint.final` object allows you to return a value while saving
a different value to the checkpoint. This value will be accessible
in the next invocation of the entrypoint via the `previous` parameter, as
long as the same thread id is used.
```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint
@entrypoint(checkpointer=MemorySaver())
def my_workflow(number: int, *, previous: Any = None) -> entrypoint.final[int, int]:
previous = previous or 0
# This will return the previous value to the caller, saving
# 2 * number to the checkpoint, which will be used in the next invocation
# for the `previous` parameter.
return entrypoint.final(value=previous, save=2 * number)
config = {
"configurable": {
"thread_id": "some_thread"
}
}
my_workflow.invoke(3, config) # 0 (previous was None)
my_workflow.invoke(1, config) # 6 (previous was 3 * 2 from the previous invocation)
```
"""
def _imp(func: types.FunctionType) -> Pregel:
def __init__(
self,
checkpointer: Optional[BaseCheckpointSaver] = None,
store: Optional[BaseStore] = None,
config_schema: Optional[type[Any]] = None,
) -> None:
"""Initialize the entrypoint decorator."""
self.checkpointer = checkpointer
self.store = store
self.config_schema = config_schema
@dataclass(**_DC_KWARGS)
class final(Generic[R, S]):
"""A primitive that can be returned from an entrypoint.
This primitive allows to save a value to the checkpointer distinct from the
return value from the entrypoint.
Example: Decoupling the return value and the save value
```python
from langgraph.checkpoint.memory import MemorySaver
from langgraph.func import entrypoint
@entrypoint(checkpointer=MemorySaver())
def my_workflow(number: int, *, previous: Any = None) -> entrypoint.final[int, int]:
previous = previous or 0
# This will return the previous value to the caller, saving
# 2 * number to the checkpoint, which will be used in the next invocation
# for the `previous` parameter.
return entrypoint.final(value=previous, save=2 * number)
config = {
"configurable": {
"thread_id": "1"
}
}
my_workflow.invoke(3, config) # 0 (previous was None)
my_workflow.invoke(1, config) # 6 (previous was 3 * 2 from the previous invocation)
```
"""
value: R
"""Value to return. A value will always be returned even if it is None."""
save: S
"""The value for the state for the next checkpoint.
A value will always be saved even if it is None.
"""
def __call__(self, func: Callable[..., Any]) -> Pregel:
"""Convert a function into a Pregel graph.
Args:
func: The function to convert. Support both sync and async functions, as well
as generator and async generator functions.
func: The function to convert. Support both sync and async functions.
Returns:
A Pregel graph.
"""
# wrap generators in a function that writes to StreamWriter
if inspect.isgeneratorfunction(func):
original_sig = inspect.signature(func)
# Check if original signature has a writer argument with a matching type.
# If not, we'll inject it into the decorator, but not pass it
# to the wrapped function.
if "writer" in original_sig.parameters:
if inspect.isgeneratorfunction(func) or inspect.isasyncgenfunction(func):
raise NotImplementedError(
"Generators are not supported in the Functional API."
)
@functools.wraps(func)
def gen_wrapper(*args: Any, writer: StreamWriter, **kwargs: Any) -> Any:
chunks = []
for chunk in func(*args, writer=writer, **kwargs):
writer(chunk)
chunks.append(chunk)
return chunks
else:
@functools.wraps(func)
def gen_wrapper(*args: Any, writer: StreamWriter, **kwargs: Any) -> Any:
chunks = []
# Do not pass the writer argument to the wrapped function
# as it does not have a matching parameter
for chunk in func(*args, **kwargs):
writer(chunk)
chunks.append(chunk)
return chunks
# Create a new parameter for the writer argument
extra_param = inspect.Parameter(
"writer",
inspect.Parameter.KEYWORD_ONLY,
# The extra argument is a keyword-only argument
default=lambda _: None,
)
# Update the function's signature to include the extra argument
new_params = list(original_sig.parameters.values()) + [extra_param]
new_sig = original_sig.replace(parameters=new_params)
# Update the signature of the wrapper function
gen_wrapper.__signature__ = new_sig # type: ignore
bound = get_runnable_for_entrypoint(gen_wrapper)
stream_mode: StreamMode = "custom"
elif inspect.isasyncgenfunction(func):
original_sig = inspect.signature(func)
# Check if original signature has a writer argument with a matching type.
# If not, we'll inject it into the decorator, but not pass it
# to the wrapped function.
if "writer" in original_sig.parameters:
@functools.wraps(func)
async def agen_wrapper(
*args: Any, writer: StreamWriter, **kwargs: Any
) -> Any:
chunks = []
async for chunk in func(*args, writer=writer, **kwargs):
writer(chunk)
chunks.append(chunk)
return chunks
else:
@functools.wraps(func)
async def agen_wrapper(
*args: Any, writer: StreamWriter, **kwargs: Any
) -> Any:
chunks = []
async for chunk in func(*args, **kwargs):
writer(chunk)
chunks.append(chunk)
return chunks
# Create a new parameter for the writer argument
extra_param = inspect.Parameter(
"writer",
inspect.Parameter.KEYWORD_ONLY,
# The extra argument is a keyword-only argument
default=lambda _: None,
)
# Update the function's signature to include the extra argument
new_params = list(original_sig.parameters.values()) + [extra_param]
new_sig = original_sig.replace(parameters=new_params)
# Update the signature of the wrapper function
agen_wrapper.__signature__ = new_sig # type: ignore
bound = get_runnable_for_entrypoint(agen_wrapper)
stream_mode = "custom"
else:
bound = get_runnable_for_entrypoint(func)
stream_mode = "updates"
bound = get_runnable_for_entrypoint(func)
stream_mode: StreamMode = "updates"
# get input and output types
sig = inspect.signature(func)
@@ -376,127 +386,65 @@ def entrypoint(
is not inspect.Signature.empty
else Any
)
output_type = (
sig.return_annotation
if sig.return_annotation is not inspect.Signature.empty
else Any
)
return EntrypointPregel(
def _pluck_return_value(value: Any) -> Any:
"""Extract the return_ value the entrypoint.final object or passthrough."""
return value.value if isinstance(value, entrypoint.final) else value
def _pluck_save_value(value: Any) -> Any:
"""Get save value from the entrypoint.final object or passthrough."""
return value.save if isinstance(value, entrypoint.final) else value
output_type, save_type = Any, Any
if sig.return_annotation is not inspect.Signature.empty:
# User does not parameterize entrypoint.final properly
if (
sig.return_annotation is entrypoint.final
): # Un-parameterized entrypoint.final
output_type = save_type = Any
else:
origin = get_origin(sig.return_annotation)
if origin is entrypoint.final:
type_annotations = get_args(sig.return_annotation)
if len(type_annotations) != 2:
raise TypeError(
"Please an annotation for both the return_ and "
"the save values."
"For example, `-> entrypoint.final[int, str]` would assign a "
"return_ a type of `int` and save the type `str`."
)
output_type, save_type = get_args(sig.return_annotation)
else:
output_type = save_type = sig.return_annotation
return Pregel(
nodes={
func.__name__: PregelNode(
bound=bound,
triggers=[START],
channels=[START],
writers=[ChannelWrite([ChannelWriteEntry(END)], tags=[TAG_HIDDEN])],
writers=[
ChannelWrite(
[
ChannelWriteEntry(END, mapper=_pluck_return_value),
ChannelWriteEntry(PREVIOUS, mapper=_pluck_save_value),
],
tags=[TAG_HIDDEN],
)
],
)
},
channels={
START: EphemeralValue(input_type),
END: LastValue(output_type, END),
PREVIOUS: LastValue(save_type, PREVIOUS),
},
input_channels=START,
output_channels=END,
stream_channels=END,
stream_mode=stream_mode,
stream_eager=True,
checkpointer=checkpointer,
store=store,
config_type=config_schema,
checkpointer=self.checkpointer,
store=self.store,
config_type=self.config_schema,
)
return _imp
class EntrypointPregel(Pregel):
def get_graph(
self,
config: Optional[RunnableConfig] = None,
*,
xray: Union[int, bool] = False,
) -> Graph:
name, entrypoint = next(iter(self.nodes.items()))
graph = Graph()
node = Node(f"__{name}", name, entrypoint.bound, None)
graph.nodes[node.id] = node
candidates: list[tuple[Node, Union[Callable, PregelProtocol]]] = [
*_find_children(entrypoint.bound, node)
]
seen: set[Union[Callable, PregelProtocol]] = set()
for parent, child in candidates:
if child in seen:
continue
else:
seen.add(child)
if callable(child):
node = Node(f"__{child.__name__}", child.__name__, child, None) # type: ignore[arg-type]
graph.nodes[node.id] = node
graph.add_edge(parent, node, conditional=True)
graph.add_edge(node, parent)
candidates.extend(_find_children(child, node))
elif isinstance(child, Runnable):
if xray > 0:
graph = child.get_graph(config, xray=xray - 1 if xray else 0)
graph.trim_first_node()
graph.trim_last_node()
s, e = graph.extend(graph, prefix=child.name or "")
if s is None:
raise ValueError(
f"Could not extend subgraph '{child.name}' due to missing entrypoint"
)
else:
graph.add_edge(parent, s, conditional=True)
if e is not None:
graph.add_edge(e, parent)
else:
node = graph.add_node(child, child.name)
graph.add_edge(parent, node, conditional=True)
graph.add_edge(node, parent)
return graph
def _find_children(
candidate: Union[Callable, Runnable], parent: Node
) -> Iterator[tuple[Node, Union[Callable, PregelProtocol]]]:
from langchain_core.runnables.utils import get_function_nonlocals
from langgraph.utils.runnable import (
RunnableCallable,
RunnableLambda,
RunnableSeq,
RunnableSequence,
)
candidates: list[Union[Callable, Runnable]] = []
if callable(candidate) and getattr(candidate, "_is_pregel_task", False) is True:
candidates.extend(
nl.__self__ if hasattr(nl, "__self__") else nl
for nl in get_function_nonlocals(
candidate.__wrapped__
if hasattr(candidate, "__wrapped__") and callable(candidate.__wrapped__)
else candidate
)
)
else:
candidates.append(candidate)
for c in candidates:
if callable(c) and getattr(c, "_is_pregel_task", False) is True:
yield (parent, c)
elif isinstance(c, PregelProtocol):
yield (parent, c)
elif isinstance(c, RunnableSequence) or isinstance(c, RunnableSeq):
candidates.extend(c.steps)
elif isinstance(c, RunnableLambda):
candidates.extend(c.deps)
elif isinstance(c, RunnableCallable):
if c.func is not None:
candidates.extend(
nl.__self__ if hasattr(nl, "__self__") else nl
for nl in get_function_nonlocals(c.func)
)
elif c.afunc is not None:
candidates.extend(
nl.__self__ if hasattr(nl, "__self__") else nl
for nl in get_function_nonlocals(c.afunc)
)
+21 -8
View File
@@ -379,14 +379,27 @@ class StateGraph(Graph):
if input_hint := hints.get(first_parameter_name):
if isinstance(input_hint, type) and get_type_hints(input_hint):
input = input_hint
if (
(rtn := hints.get("return"))
and get_origin(rtn) is Command
and (rargs := get_args(rtn))
and get_origin(rargs[0]) is Literal
and (vals := get_args(rargs[0]))
):
ends = vals
if rtn := hints.get("return"):
# Handle Union types
rtn_origin = get_origin(rtn)
if rtn_origin is Union:
rtn_args = get_args(rtn)
# Look for Command in the union
for arg in rtn_args:
arg_origin = get_origin(arg)
if arg_origin is Command:
rtn = arg
rtn_origin = arg_origin
break
# Check if it's a Command type
if (
rtn_origin is Command
and (rargs := get_args(rtn))
and get_origin(rargs[0]) is Literal
and (vals := get_args(rargs[0]))
):
ends = vals
except (TypeError, StopIteration):
pass
if input is not None:
@@ -1,4 +1,7 @@
import functools
import inspect
from typing import (
Any,
Callable,
Literal,
Optional,
@@ -20,7 +23,6 @@ from langchain_core.tools import BaseTool
from pydantic import BaseModel
from typing_extensions import Annotated, TypedDict
from langgraph._api.deprecation import deprecated_parameter
from langgraph.errors import ErrorCode, create_error_message
from langgraph.graph import END, StateGraph
from langgraph.graph.graph import CompiledGraph
@@ -34,6 +36,7 @@ from langgraph.utils.runnable import RunnableCallable
StructuredResponse = Union[dict, BaseModel]
StructuredResponseSchema = Union[dict, type[BaseModel]]
F = TypeVar("F", bound=Callable[..., Any])
# We create the AgentState that we will pass around
@@ -55,7 +58,7 @@ class AgentState(TypedDict):
StateSchema = TypeVar("StateSchema", bound=AgentState)
StateSchemaType = Type[StateSchema]
STATE_MODIFIER_RUNNABLE_NAME = "StateModifier"
PROMPT_RUNNABLE_NAME = "Prompt"
MessagesModifier = Union[
SystemMessage,
@@ -64,7 +67,7 @@ MessagesModifier = Union[
Runnable[Sequence[BaseMessage], Sequence[BaseMessage]],
]
StateModifier = Union[
Prompt = Union[
SystemMessage,
str,
Callable[[StateSchema], Sequence[BaseMessage]],
@@ -72,75 +75,84 @@ StateModifier = Union[
]
def _get_state_modifier_runnable(
state_modifier: Optional[StateModifier], store: Optional[BaseStore] = None
) -> Runnable:
state_modifier_runnable: Runnable
if state_modifier is None:
state_modifier_runnable = RunnableCallable(
lambda state: state["messages"], name=STATE_MODIFIER_RUNNABLE_NAME
def _get_prompt_runnable(prompt: Optional[Prompt]) -> Runnable:
prompt_runnable: Runnable
if prompt is None:
prompt_runnable = RunnableCallable(
lambda state: state["messages"], name=PROMPT_RUNNABLE_NAME
)
elif isinstance(state_modifier, str):
_system_message: BaseMessage = SystemMessage(content=state_modifier)
state_modifier_runnable = RunnableCallable(
elif isinstance(prompt, str):
_system_message: BaseMessage = SystemMessage(content=prompt)
prompt_runnable = RunnableCallable(
lambda state: [_system_message] + state["messages"],
name=STATE_MODIFIER_RUNNABLE_NAME,
name=PROMPT_RUNNABLE_NAME,
)
elif isinstance(state_modifier, SystemMessage):
state_modifier_runnable = RunnableCallable(
lambda state: [state_modifier] + state["messages"],
name=STATE_MODIFIER_RUNNABLE_NAME,
elif isinstance(prompt, SystemMessage):
prompt_runnable = RunnableCallable(
lambda state: [prompt] + state["messages"],
name=PROMPT_RUNNABLE_NAME,
)
elif callable(state_modifier):
state_modifier_runnable = RunnableCallable(
state_modifier,
name=STATE_MODIFIER_RUNNABLE_NAME,
elif inspect.iscoroutinefunction(prompt):
prompt_runnable = RunnableCallable(
None,
prompt,
name=PROMPT_RUNNABLE_NAME,
)
elif isinstance(state_modifier, Runnable):
state_modifier_runnable = state_modifier
elif callable(prompt):
prompt_runnable = RunnableCallable(
prompt,
name=PROMPT_RUNNABLE_NAME,
)
elif isinstance(prompt, Runnable):
prompt_runnable = prompt
else:
raise ValueError(
f"Got unexpected type for `state_modifier`: {type(state_modifier)}"
)
raise ValueError(f"Got unexpected type for `prompt`: {type(prompt)}")
return state_modifier_runnable
return prompt_runnable
def _convert_messages_modifier_to_state_modifier(
def _convert_messages_modifier_to_prompt(
messages_modifier: MessagesModifier,
) -> StateModifier:
state_modifier: StateModifier
) -> Prompt:
prompt: Prompt
if isinstance(messages_modifier, (str, SystemMessage)):
return messages_modifier
elif callable(messages_modifier):
def state_modifier(state: AgentState) -> Sequence[BaseMessage]:
def prompt(state: AgentState) -> Sequence[BaseMessage]:
return messages_modifier(state["messages"])
return state_modifier
return prompt
elif isinstance(messages_modifier, Runnable):
state_modifier = (lambda state: state["messages"]) | messages_modifier
return state_modifier
prompt = (lambda state: state["messages"]) | messages_modifier
return prompt
raise ValueError(
f"Got unexpected type for `messages_modifier`: {type(messages_modifier)}"
)
def _get_model_preprocessing_runnable(
state_modifier: Optional[StateModifier],
messages_modifier: Optional[MessagesModifier],
store: Optional[BaseStore],
) -> Runnable:
# Add the state or message modifier, if exists
if state_modifier is not None and messages_modifier is not None:
raise ValueError(
"Expected value for either state_modifier or messages_modifier, got values for both"
)
def _convert_modifier_to_prompt(func: F) -> F:
"""Decorator that converts state_modifier/messages_modifier kwargs to prompt kwarg."""
if state_modifier is None and messages_modifier is not None:
state_modifier = _convert_messages_modifier_to_state_modifier(messages_modifier)
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
prompt = kwargs.get("prompt")
state_modifier = kwargs.pop("state_modifier", None)
messages_modifier = kwargs.pop("messages_modifier", None)
if sum(p is not None for p in (prompt, state_modifier, messages_modifier)) > 1:
raise ValueError(
"Expected only one of prompt, state_modifier, or messages_modifier, got multiple values"
)
return _get_state_modifier_runnable(state_modifier, store)
if state_modifier is not None:
prompt = state_modifier
elif messages_modifier is not None:
prompt = _convert_messages_modifier_to_prompt(messages_modifier)
kwargs["prompt"] = prompt
return func(*args, **kwargs)
return cast(F, wrapper)
def _should_bind_tools(model: LanguageModelLike, tools: Sequence[BaseTool]) -> bool:
@@ -221,14 +233,13 @@ def _validate_chat_history(
raise ValueError(error_message)
@deprecated_parameter("messages_modifier", "0.1.9", "state_modifier", removal="0.3.0")
@_convert_modifier_to_prompt
def create_react_agent(
model: Union[str, LanguageModelLike],
tools: Union[ToolExecutor, Sequence[BaseTool], ToolNode],
*,
state_schema: Optional[StateSchemaType] = None,
messages_modifier: Optional[MessagesModifier] = None,
state_modifier: Optional[StateModifier] = None,
prompt: Optional[Prompt] = None,
response_format: Optional[
Union[StructuredResponseSchema, tuple[str, StructuredResponseSchema]]
] = None,
@@ -247,26 +258,15 @@ def create_react_agent(
state_schema: An optional state schema that defines graph state.
Must have `messages` and `is_last_step` keys.
Defaults to `AgentState` that defines those two keys.
messages_modifier: An optional
messages modifier. This applies to messages BEFORE they are passed into the LLM.
prompt: An optional prompt for the LLM. Can take a few different forms:
Can take a few different forms:
- SystemMessage: this is added to the beginning of the list of messages.
- str: This is converted to a SystemMessage and added to the beginning of the list of messages.
- Callable: This function should take in a list of messages and the output is then passed to the language model.
- Runnable: This runnable should take in a list of messages and the output is then passed to the language model.
!!! Warning
`messages_modifier` parameter is deprecated as of version 0.1.9 and will be removed in 0.2.0
state_modifier: An optional
state modifier. This takes full graph state BEFORE the LLM is called and prepares the input to LLM.
Can take a few different forms:
- SystemMessage: this is added to the beginning of the list of messages in state["messages"].
- str: This is converted to a SystemMessage and added to the beginning of the list of messages in state["messages"].
- SystemMessage: this is added to the beginning of the list of messages in state["messages"].
- Callable: This function should take in full graph state and the output is then passed to the language model.
- Runnable: This runnable should take in full graph state and the output is then passed to the language model.
!!! Note
Prior to `v0.2.68`, the prompt was set using `state_modifier` / `messages_modifier` parameters.
response_format: An optional schema for the final agent output.
If provided, output will be formatted to match the given schema and returned in the 'structured_response' state key.
@@ -382,7 +382,7 @@ def create_react_agent(
```pycon
>>> system_prompt = "You are a helpful bot named Fred."
>>> graph = create_react_agent(model, tools, state_modifier=system_prompt)
>>> graph = create_react_agent(model, tools, prompt=system_prompt)
>>> inputs = {"messages": [("user", "What's your name? And what's the weather in SF?")]}
>>> for s in graph.stream(inputs, stream_mode="values"):
... message = s["messages"][-1]
@@ -414,11 +414,8 @@ def create_react_agent(
... ("placeholder", "{messages}"),
... ("user", "Remember, always be polite!"),
... ])
>>> def format_for_model(state: AgentState):
... # You can do more complex modifications here
... return prompt.invoke({"messages": state["messages"]})
>>>
>>> graph = create_react_agent(model, tools, state_modifier=format_for_model)
>>> graph = create_react_agent(model, tools, prompt=prompt)
>>> inputs = {"messages": [("user", "What's your name? And what's the weather in SF?")]}
>>> for s in graph.stream(inputs, stream_mode="values"):
... message = s["messages"][-1]
@@ -446,7 +443,7 @@ def create_react_agent(
... messages: Annotated[list[BaseMessage], add_messages]
... is_last_step: IsLastStep
>>>
>>> graph = create_react_agent(model, tools, state_schema=CustomState, state_modifier=prompt)
>>> graph = create_react_agent(model, tools, state_schema=CustomState, prompt=prompt)
>>> inputs = {"messages": [("user", "What's today's date? And what's the weather in SF?")], "today": "July 16, 2004"}
>>> for s in graph.stream(inputs, stream_mode="values"):
... message = s["messages"][-1]
@@ -532,7 +529,7 @@ def create_react_agent(
>>> from langgraph.checkpoint.memory import MemorySaver
>>> from langgraph.store.memory import InMemoryStore
>>> store = InMemoryStore()
>>> graph = create_react_agent(model, [save_memory], state_modifier=prepare_model_inputs, store=store, checkpointer=MemorySaver())
>>> graph = create_react_agent(model, [save_memory], prompt=prepare_model_inputs, store=store, checkpointer=MemorySaver())
>>> config = {"configurable": {"thread_id": "thread-1", "user_id": "1"}}
>>> inputs = {"messages": [("user", "Hey I'm Will, how's it going?")]}
@@ -612,11 +609,7 @@ def create_react_agent(
if _should_bind_tools(model, tool_classes) and tool_calling_enabled:
model = cast(BaseChatModel, model).bind_tools(tool_classes)
# we're passing store here for validation
preprocessor = _get_model_preprocessing_runnable(
state_modifier, messages_modifier, store
)
model_runnable = preprocessor | model
model_runnable = _get_prompt_runnable(prompt) | model
# If any of the tools are configured to return_directly after running,
# our graph needs to check if these were called
+11 -22
View File
@@ -27,7 +27,6 @@ from langchain_core.runnables.config import (
get_config_list,
get_executor_for_config,
)
from langchain_core.runnables.utils import Input
from langchain_core.tools import BaseTool, InjectedToolArg
from langchain_core.tools import tool as create_tool
from langchain_core.tools.base import get_all_basemodel_annotations
@@ -210,7 +209,7 @@ class ToolNode(RunnableCallable):
],
config: RunnableConfig,
*,
store: BaseStore,
store: Optional[BaseStore],
) -> Any:
tool_calls, input_type = self._parse_input(input, store)
config_list = get_config_list(config, len(tool_calls))
@@ -220,12 +219,14 @@ class ToolNode(RunnableCallable):
*executor.map(self._run_one, tool_calls, input_types, config_list)
]
# preserve existing behavior for non-command tool outputs for backwards compatibility
# preserve existing behavior for non-command tool outputs for backwards
# compatibility
if not any(isinstance(output, Command) for output in outputs):
# TypedDict, pydantic, dataclass, etc. should all be able to load from dict
return outputs if input_type == "list" else {self.messages_key: outputs}
# LangGraph will automatically handle list of Command and non-command node updates
# LangGraph will automatically handle list of Command and non-command node
# updates
combined_outputs: list[
Command | list[ToolMessage] | dict[str, list[ToolMessage]]
] = []
@@ -238,20 +239,6 @@ class ToolNode(RunnableCallable):
)
return combined_outputs
def invoke(
self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
) -> Any:
if "store" not in kwargs:
kwargs["store"] = None
return super().invoke(input, config, **kwargs)
async def ainvoke(
self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
) -> Any:
if "store" not in kwargs:
kwargs["store"] = None
return await super().ainvoke(input, config, **kwargs)
async def _afunc(
self,
input: Union[
@@ -261,7 +248,7 @@ class ToolNode(RunnableCallable):
],
config: RunnableConfig,
*,
store: BaseStore,
store: Optional[BaseStore],
) -> Any:
tool_calls, input_type = self._parse_input(input, store)
outputs = await asyncio.gather(
@@ -404,7 +391,7 @@ class ToolNode(RunnableCallable):
dict[str, Any],
BaseModel,
],
store: BaseStore,
store: Optional[BaseStore],
) -> Tuple[list[ToolCall], Literal["list", "dict"]]:
if isinstance(input, list):
input_type = "list"
@@ -484,7 +471,9 @@ class ToolNode(RunnableCallable):
}
return tool_call
def _inject_store(self, tool_call: ToolCall, store: BaseStore) -> ToolCall:
def _inject_store(
self, tool_call: ToolCall, store: Optional[BaseStore]
) -> ToolCall:
store_arg = self.tool_to_store_arg[tool_call["name"]]
if not store_arg:
return tool_call
@@ -509,7 +498,7 @@ class ToolNode(RunnableCallable):
dict[str, Any],
BaseModel,
],
store: BaseStore,
store: Optional[BaseStore],
) -> ToolCall:
if tool_call["name"] not in self.tools_by_name:
return tool_call
+127 -19
View File
@@ -97,7 +97,7 @@ from langgraph.pregel.protocol import PregelProtocol
from langgraph.pregel.read import PregelNode
from langgraph.pregel.retry import RetryPolicy
from langgraph.pregel.runner import PregelRunner
from langgraph.pregel.utils import find_subgraph_pregel, get_new_channel_versions
from langgraph.pregel.utils import get_new_channel_versions
from langgraph.pregel.validate import validate_graph, validate_keys
from langgraph.pregel.write import ChannelWrite, ChannelWriteEntry
from langgraph.store.base import BaseStore
@@ -429,7 +429,7 @@ class Pregel(PregelProtocol):
def get_subgraphs(
self, *, namespace: Optional[str] = None, recurse: bool = False
) -> Iterator[tuple[str, Pregel]]:
) -> Iterator[tuple[str, PregelProtocol]]:
for name, node in self.nodes.items():
# filter by prefix
if namespace is not None:
@@ -437,7 +437,7 @@ class Pregel(PregelProtocol):
continue
# find the subgraph, if any
graph = cast(Optional[Pregel], find_subgraph_pregel(node.bound))
graph = node.subgraphs[0] if node.subgraphs else None
# if found, yield recursively
if graph:
@@ -446,7 +446,7 @@ class Pregel(PregelProtocol):
return # we found it, stop searching
if namespace is None:
yield name, graph
if recurse:
if recurse and isinstance(graph, Pregel):
if namespace is not None:
namespace = namespace[len(name) + 1 :]
yield from (
@@ -458,7 +458,7 @@ class Pregel(PregelProtocol):
async def aget_subgraphs(
self, *, namespace: Optional[str] = None, recurse: bool = False
) -> AsyncIterator[tuple[str, Pregel]]:
) -> AsyncIterator[tuple[str, PregelProtocol]]:
for name, node in self.get_subgraphs(namespace=namespace, recurse=recurse):
yield name, node
@@ -1490,11 +1490,15 @@ class Pregel(PregelProtocol):
input: The input to the graph.
config: The configuration to use for the run.
stream_mode: The mode to stream output, defaults to self.stream_mode.
Options are 'values', 'updates', and 'debug'.
values: Emit the current values of the state for each step.
updates: Emit only the updates to the state for each step.
Output is a dict with the node name as key and the updated values as value.
debug: Emit debug events for each step.
Options are:
- `"values"`: Emit all values in the state after each step.
When used with functional API, values are emitted once at the end of the workflow.
- `"updates"`: Emit only the node or task names and updates returned by the nodes or tasks after each step.
If multiple updates are made in the same step (e.g. multiple nodes are run) then those updates are emitted separately.
- `"custom"`: Emit custom data using from inside nodes or tasks using `StreamWriter`.
- `"messages"`: Emit LLM messages token-by-token together with metadata for any LLM invocations inside nodes or tasks.
- `"debug"`: Emit debug events with as much information as possible for each step.
output_keys: The keys to stream, defaults to all non-context channels.
interrupt_before: Nodes to interrupt before, defaults to all nodes in the graph.
interrupt_after: Nodes to interrupt after, defaults to all nodes in the graph.
@@ -1509,8 +1513,7 @@ class Pregel(PregelProtocol):
```pycon
>>> import operator
>>> from typing_extensions import Annotated, TypedDict
>>> from langgraph.graph import StateGraph
>>> from langgraph.constants import START
>>> from langgraph.graph import StateGraph, START
...
>>> class State(TypedDict):
... alist: Annotated[list, operator.add]
@@ -1550,6 +1553,57 @@ class Pregel(PregelProtocol):
{'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': ['hi']}, 'triggers': ['a']}}
{'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'result': [('alist', ['there'])]}}
```
With stream_mode="custom":
```pycon
>>> from langgraph.types import StreamWriter
...
>>> def node_a(state: State, writer: StreamWriter):
... writer({"custom_data": "foo"})
... return {"alist": ["hi"]}
...
>>> builder = StateGraph(State)
>>> builder.add_node("a", node_a)
>>> builder.add_edge(START, "a")
>>> graph = builder.compile()
...
>>> for event in graph.stream({"alist": ['Ex for stream_mode="custom"']}, stream_mode="custom"):
... print(event)
{'custom_data': 'foo'}
```
With stream_mode="messages":
```pycon
>>> from typing_extensions import Annotated, TypedDict
>>> from langgraph.graph import StateGraph, START
>>> from langchain_openai import ChatOpenAI
...
>>> llm = ChatOpenAI(model="gpt-4o-mini")
...
>>> class State(TypedDict):
... question: str
... answer: str
...
>>> def node_a(state: State):
... response = llm.invoke(state["question"])
... return {"answer": response.content}
...
>>> builder = StateGraph(State)
>>> builder.add_node("a", node_a)
>>> builder.add_edge(START, "a")
>>> graph = builder.compile()
>>> for event in graph.stream({"question": "What is the capital of France?"}, stream_mode="messages"):
... print(event)
(AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], 'langgraph_path': ('__pregel_pull', 'a'), 'langgraph_checkpoint_ns': '...', 'checkpoint_ns': '...', 'ls_provider': 'openai', 'ls_model_name': 'gpt-4o-mini', 'ls_model_type': 'chat', 'ls_temperature': 0.7})
(AIMessageChunk(content=' capital', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], ...})
(AIMessageChunk(content=' of', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' France', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' is', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' Paris', additional_kwargs={}, response_metadata={}, id='...'), {...})
```
"""
stream = SyncQueue()
@@ -1712,11 +1766,15 @@ class Pregel(PregelProtocol):
input: The input to the graph.
config: The configuration to use for the run.
stream_mode: The mode to stream output, defaults to self.stream_mode.
Options are 'values', 'updates', and 'debug'.
values: Emit the current values of the state for each step.
updates: Emit only the updates to the state for each step.
Output is a dict with the node name as key and the updated values as value.
debug: Emit debug events for each step.
Options are:
- `"values"`: Emit all values in the state after each step.
When used with functional API, values are emitted once at the end of the workflow.
- `"updates"`: Emit only the node or task names and updates returned by the nodes or tasks after each step.
If multiple updates are made in the same step (e.g. multiple nodes are run) then those updates are emitted separately.
- `"custom"`: Emit custom data using from inside nodes or tasks using `StreamWriter`.
- `"messages"`: Emit LLM messages token-by-token together with metadata for any LLM invocations inside nodes or tasks.
- `"debug"`: Emit debug events with as much information as possible for each step.
output_keys: The keys to stream, defaults to all non-context channels.
interrupt_before: Nodes to interrupt before, defaults to all nodes in the graph.
interrupt_after: Nodes to interrupt after, defaults to all nodes in the graph.
@@ -1731,8 +1789,7 @@ class Pregel(PregelProtocol):
```pycon
>>> import operator
>>> from typing_extensions import Annotated, TypedDict
>>> from langgraph.graph import StateGraph
>>> from langgraph.constants import START
>>> from langgraph.graph import StateGraph, START
...
>>> class State(TypedDict):
... alist: Annotated[list, operator.add]
@@ -1772,6 +1829,57 @@ class Pregel(PregelProtocol):
{'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': ['hi']}, 'triggers': ['a']}}
{'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'result': [('alist', ['there'])]}}
```
With stream_mode="custom":
```pycon
>>> from langgraph.types import StreamWriter
...
>>> async def node_a(state: State, writer: StreamWriter):
... writer({"custom_data": "foo"})
... return {"alist": ["hi"]}
...
>>> builder = StateGraph(State)
>>> builder.add_node("a", node_a)
>>> builder.add_edge(START, "a")
>>> graph = builder.compile()
...
>>> async for event in graph.astream({"alist": ['Ex for stream_mode="custom"']}, stream_mode="custom"):
... print(event)
{'custom_data': 'foo'}
```
With stream_mode="messages":
```pycon
>>> from typing_extensions import Annotated, TypedDict
>>> from langgraph.graph import StateGraph, START
>>> from langchain_openai import ChatOpenAI
...
>>> llm = ChatOpenAI(model="gpt-4o-mini")
...
>>> class State(TypedDict):
... question: str
... answer: str
...
>>> async def node_a(state: State):
... response = await llm.ainvoke(state["question"])
... return {"answer": response.content}
...
>>> builder = StateGraph(State)
>>> builder.add_node("a", node_a)
>>> builder.add_edge(START, "a")
>>> graph = builder.compile()
>>> for event in graph.stream({"question": "What is the capital of France?"}, stream_mode="messages"):
... print(event)
(AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], 'langgraph_path': ('__pregel_pull', 'a'), 'langgraph_checkpoint_ns': '...', 'checkpoint_ns': '...', 'ls_provider': 'openai', 'ls_model_name': 'gpt-4o-mini', 'ls_model_type': 'chat', 'ls_temperature': 0.7})
(AIMessageChunk(content=' capital', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], ...})
(AIMessageChunk(content=' of', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' France', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' is', additional_kwargs={}, response_metadata={}, id='...'), {...})
(AIMessageChunk(content=' Paris', additional_kwargs={}, response_metadata={}, id='...'), {...})
```
"""
stream = AsyncQueue()
+26 -21
View File
@@ -1,3 +1,5 @@
import functools
import itertools
import sys
from collections import defaultdict, deque
from functools import partial
@@ -37,7 +39,7 @@ from langgraph.constants import (
CONFIG_KEY_CHECKPOINT_MAP,
CONFIG_KEY_CHECKPOINT_NS,
CONFIG_KEY_CHECKPOINTER,
CONFIG_KEY_END,
CONFIG_KEY_PREVIOUS,
CONFIG_KEY_READ,
CONFIG_KEY_SCRATCHPAD,
CONFIG_KEY_SEND,
@@ -46,11 +48,11 @@ from langgraph.constants import (
EMPTY_SEQ,
ERROR,
INTERRUPT,
MISSING,
NO_WRITES,
NS_END,
NS_SEP,
NULL_TASK_ID,
PREVIOUS,
PULL,
PUSH,
RESERVED,
@@ -324,7 +326,7 @@ def apply_writes(
@overload
def prepare_next_tasks(
checkpoint: Checkpoint,
pending_writes: Sequence[PendingWrite],
pending_writes: list[PendingWrite],
processes: Mapping[str, PregelNode],
channels: Mapping[str, BaseChannel],
managed: ManagedValueMapping,
@@ -341,7 +343,7 @@ def prepare_next_tasks(
@overload
def prepare_next_tasks(
checkpoint: Checkpoint,
pending_writes: Sequence[PendingWrite],
pending_writes: list[PendingWrite],
processes: Mapping[str, PregelNode],
channels: Mapping[str, BaseChannel],
managed: ManagedValueMapping,
@@ -357,7 +359,7 @@ def prepare_next_tasks(
def prepare_next_tasks(
checkpoint: Checkpoint,
pending_writes: Sequence[PendingWrite],
pending_writes: list[PendingWrite],
processes: Mapping[str, PregelNode],
channels: Mapping[str, BaseChannel],
managed: ManagedValueMapping,
@@ -418,7 +420,7 @@ def prepare_single_task(
task_id_checksum: Optional[str],
*,
checkpoint: Checkpoint,
pending_writes: Sequence[PendingWrite],
pending_writes: list[PendingWrite],
processes: Mapping[str, PregelNode],
channels: Mapping[str, BaseChannel],
managed: ManagedValueMapping,
@@ -508,9 +510,6 @@ def prepare_single_task(
pending_writes,
task_id,
),
CONFIG_KEY_END: checkpoint["channel_values"].get(
"__end__", None
),
},
),
triggers,
@@ -620,8 +619,8 @@ def prepare_single_task(
pending_writes,
task_id,
),
CONFIG_KEY_END: checkpoint["channel_values"].get(
"__end__", None
CONFIG_KEY_PREVIOUS: checkpoint["channel_values"].get(
PREVIOUS, None
),
},
),
@@ -631,6 +630,7 @@ def prepare_single_task(
task_id,
task_path[:3],
writers=proc.flat_writers,
subgraphs=proc.subgraphs,
)
else:
return PregelTask(task_id, packet.node, task_path[:3])
@@ -744,8 +744,8 @@ def prepare_single_task(
pending_writes,
task_id,
),
CONFIG_KEY_END: checkpoint["channel_values"].get(
"__end__", None
CONFIG_KEY_PREVIOUS: checkpoint["channel_values"].get(
PREVIOUS, None
),
},
),
@@ -755,29 +755,34 @@ def prepare_single_task(
task_id,
task_path[:3],
writers=proc.flat_writers,
subgraphs=proc.subgraphs,
)
else:
return PregelTask(task_id, name, task_path[:3])
def _scratchpad(
pending_writes: Sequence[PendingWrite],
pending_writes: list[PendingWrite],
task_id: str,
) -> PregelScratchpad:
null_resume_write = next(
(w for w in pending_writes if w[0] == NULL_TASK_ID and w[1] == RESUME), None
)
# using itertools.count as an atomic counter (+= 1 is not thread-safe)
return PregelScratchpad(
# call
call_counter=0,
call_counter=itertools.count(0).__next__,
# interrupt
interrupt_counter=-1,
interrupt_counter=itertools.count(0).__next__,
resume=next(
(w[2] for w in pending_writes if w[0] == task_id and w[1] == RESUME), []
),
null_resume=next(
(w[2] for w in pending_writes if w[0] == NULL_TASK_ID and w[1] == RESUME),
MISSING,
),
null_resume=null_resume_write[2] if null_resume_write is not None else None,
_consume_null_resume=functools.partial(pending_writes.remove, null_resume_write)
if null_resume_write is not None
else lambda: None,
# subgraph
subgraph_counter=0,
subgraph_counter=itertools.count(0).__next__,
)
+33 -15
View File
@@ -1,13 +1,13 @@
"""Utility to convert a user provided function into a Runnable with a ChannelWrite."""
import asyncio
import concurrent.futures
import functools
import inspect
import sys
import types
from typing import Any, Callable, Optional, TypeVar, Union
from typing import Any, Callable, Generator, Generic, Optional, TypeVar, cast
from langchain_core.runnables import Runnable
from typing_extensions import ParamSpec
from langgraph.constants import CONF, CONFIG_KEY_CALL, RETURN, TAG_HIDDEN
@@ -144,7 +144,9 @@ def get_runnable_for_entrypoint(func: Callable[..., Any]) -> RunnableSeq:
return CACHE[key]
else:
if is_async_callable(func):
run = RunnableCallable(None, func, name=func.__name__, trace=False)
run = RunnableCallable(
None, func, name=func.__name__, trace=False, recurse=False
)
else:
afunc = functools.update_wrapper(
functools.partial(run_in_executor, None, func), func
@@ -154,15 +156,11 @@ def get_runnable_for_entrypoint(func: Callable[..., Any]) -> RunnableSeq:
afunc,
name=func.__name__,
trace=False,
recurse=False,
)
seq = RunnableSeq(
run,
ChannelWrite([ChannelWriteEntry(RETURN)], tags=[TAG_HIDDEN]),
name=func.__name__,
)
if not _lookup_module_and_qualname(func):
return seq
return CACHE.setdefault(key, seq)
return run
return CACHE.setdefault(key, run)
def get_runnable_for_task(func: Callable[..., Any]) -> RunnableSeq:
@@ -170,22 +168,37 @@ def get_runnable_for_task(func: Callable[..., Any]) -> RunnableSeq:
if key in CACHE:
return CACHE[key]
else:
if hasattr(func, "__name__"):
name = func.__name__
elif hasattr(func, "func"):
name = func.func.__name__
elif hasattr(func, "__class__"):
name = func.__class__.__name__
else:
name = str(func)
if is_async_callable(func):
run = RunnableCallable(
None, func, explode_args=True, name=func.__name__, trace=False
None,
func,
explode_args=True,
name=name,
trace=False,
recurse=False,
)
else:
run = RunnableCallable(
func,
functools.wraps(func)(functools.partial(run_in_executor, None, func)),
explode_args=True,
name=func.__name__,
name=name,
trace=False,
recurse=False,
)
seq = RunnableSeq(
run,
ChannelWrite([ChannelWriteEntry(RETURN)], tags=[TAG_HIDDEN]),
name=func.__name__,
name=name,
trace_inputs=functools.partial(
_explode_args_trace_inputs, inspect.signature(func)
),
@@ -195,7 +208,7 @@ def get_runnable_for_task(func: Callable[..., Any]) -> RunnableSeq:
return CACHE.setdefault(key, seq)
CACHE: dict[tuple[Callable[..., Any], bool], RunnableSeq] = {}
CACHE: dict[tuple[Callable[..., Any], bool], Runnable] = {}
P = ParamSpec("P")
@@ -203,12 +216,17 @@ P1 = TypeVar("P1")
T = TypeVar("T")
class SyncAsyncFuture(Generic[T], concurrent.futures.Future[T]):
def __await__(self) -> Generator[T, None, T]:
yield cast(T, ...)
def call(
func: Callable[P, T],
*args: Any,
retry: Optional[RetryPolicy] = None,
**kwargs: Any,
) -> Union[concurrent.futures.Future[T], asyncio.Future[T]]:
) -> SyncAsyncFuture[T]:
config = get_config()
impl = config[CONF][CONFIG_KEY_CALL]
fut = impl(func, (args, kwargs), retry=retry, callbacks=config["callbacks"])
+1 -2
View File
@@ -30,7 +30,6 @@ from langgraph.constants import (
TAG_HIDDEN,
)
from langgraph.pregel.io import read_channels
from langgraph.pregel.utils import find_subgraph_pregel
from langgraph.types import PregelExecutableTask, PregelTask, StateSnapshot
from langgraph.utils.config import patch_checkpoint_map
@@ -157,7 +156,7 @@ def map_debug_checkpoint(
task_states: dict[str, Union[RunnableConfig, StateSnapshot]] = {}
for task in tasks:
if not find_subgraph_pregel(task.proc):
if not task.subgraphs:
continue
# assemble checkpoint_ns for this task
+2 -2
View File
@@ -74,7 +74,7 @@ def map_command(
) -> Iterator[tuple[str, str, Any]]:
"""Map input chunk to a sequence of pending writes in the form (channel, value)."""
if cmd.graph == Command.PARENT:
raise InvalidUpdateError("There is not parent graph")
raise InvalidUpdateError("There is no parent graph")
if cmd.goto:
if isinstance(cmd.goto, (tuple, list)):
sends = cmd.goto
@@ -89,7 +89,7 @@ def map_command(
raise TypeError(
f"In Command.goto, expected Send/str, got {type(send).__name__}"
)
if cmd.resume:
if cmd.resume is not None:
if isinstance(cmd.resume, dict) and all(is_task_id(k) for k in cmd.resume):
for tid, resume in cmd.resume.items():
existing: list[Any] = next(
+28 -9
View File
@@ -2,6 +2,7 @@ import asyncio
import concurrent.futures
from collections import defaultdict, deque
from contextlib import AsyncExitStack, ExitStack
from dataclasses import replace
from inspect import signature
from types import TracebackType
from typing import (
@@ -54,7 +55,6 @@ from langgraph.constants import (
ERROR,
INPUT,
INTERRUPT,
MISSING,
NS_SEP,
NULL_TASK_ID,
PUSH,
@@ -67,6 +67,7 @@ from langgraph.errors import (
EmptyInputError,
GraphDelegate,
GraphInterrupt,
ParentCommand,
)
from langgraph.managed.base import (
ManagedValueMapping,
@@ -229,20 +230,23 @@ class PregelLoop(LoopProtocol):
if self.stream is not None and CONFIG_KEY_STREAM in config[CONF]:
self.stream = DuplexStream(self.stream, config[CONF][CONFIG_KEY_STREAM])
scratchpad: Optional[PregelScratchpad] = config[CONF].get(CONFIG_KEY_SCRATCHPAD)
if not self.config[CONF].get(CONFIG_KEY_DELEGATE) and scratchpad is not None:
if scratchpad["subgraph_counter"]:
if not self.config[CONF].get(CONFIG_KEY_DELEGATE) and isinstance(
scratchpad, PregelScratchpad
):
# if count is > 0, append to checkpoint_ns
# if count is 0, leave as is
if cnt := scratchpad.subgraph_counter():
self.config = patch_configurable(
self.config,
{
CONFIG_KEY_CHECKPOINT_NS: NS_SEP.join(
(
config[CONF][CONFIG_KEY_CHECKPOINT_NS],
str(scratchpad["subgraph_counter"]),
str(cnt),
)
)
},
)
scratchpad["subgraph_counter"] += 1
if not self.is_nested and config[CONF].get(CONFIG_KEY_CHECKPOINT_NS):
self.config = patch_configurable(
self.config,
@@ -563,9 +567,14 @@ class PregelLoop(LoopProtocol):
)
# take resume value from parent
if scratchpad := configurable.get(CONFIG_KEY_SCRATCHPAD):
if scratchpad["null_resume"] is not MISSING:
self.put_writes(NULL_TASK_ID, [(RESUME, scratchpad["null_resume"])])
if scratchpad := cast(
Optional[PregelScratchpad], configurable.get(CONFIG_KEY_SCRATCHPAD)
):
if (
isinstance(scratchpad, PregelScratchpad)
and scratchpad.null_resume is not None
):
self.put_writes(NULL_TASK_ID, [(RESUME, scratchpad.null_resume)])
# map command to writes
if isinstance(self.input, Command):
if self.input.resume is not None and not self.checkpointer:
@@ -729,6 +738,16 @@ class PregelLoop(LoopProtocol):
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
# add current state to parent command
if isinstance(exc_value, ParentCommand):
cmd = exc_value.args[0]
state = (
[(self.output_keys, read_channels(self.channels, self.output_keys))]
if isinstance(self.output_keys, str)
else list(read_channels(self.channels, self.output_keys).items())
)
exc_value.args = (replace(cmd, update=[*state, *cmd._update_as_tuples()]),)
# suppress interrupt
suppress = isinstance(exc_value, GraphInterrupt) and not self.is_nested
if suppress:
# emit one last "values" event, with pending writes applied
@@ -1084,6 +1103,6 @@ class AsyncPregelLoop(PregelLoop, AsyncContextManager):
return await exit_task
except asyncio.CancelledError as e:
# Bubble up the exit task upon cancellation to permit the API
# consumer to await it before e.g., re-using the DB connection.
# consumer to await it before e.g., reusing the DB connection.
e.args = (*e.args, exit_task)
raise
@@ -76,11 +76,15 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
chunk: Optional[ChatGenerationChunk] = None,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
**kwargs: Any,
) -> Any:
if not isinstance(chunk, ChatGenerationChunk):
return
if meta := self.metadata.get(run_id):
filtered_tags = [t for t in (tags or []) if not t.startswith("seq:step")]
if filtered_tags:
meta[1]["tags"] = filtered_tags
self._emit(meta, chunk.message)
def on_llm_end(
+17
View File
@@ -22,7 +22,9 @@ from langchain_core.runnables.base import Input, Other, coerce_to_runnable
from langchain_core.runnables.utils import ConfigurableFieldSpec
from langgraph.constants import CONF, CONFIG_KEY_READ
from langgraph.pregel.protocol import PregelProtocol
from langgraph.pregel.retry import RetryPolicy
from langgraph.pregel.utils import find_subgraph_pregel
from langgraph.pregel.write import ChannelWrite
from langgraph.utils.config import merge_configs
from langgraph.utils.runnable import RunnableCallable, RunnableSeq
@@ -145,6 +147,9 @@ class PregelNode(Runnable):
metadata: Optional[Mapping[str, Any]]
"""Metadata to attach to the node for tracing."""
subgraphs: Sequence[PregelProtocol]
"""Subgraphs used by the node."""
def __init__(
self,
*,
@@ -165,9 +170,21 @@ class PregelNode(Runnable):
self.retry_policy = retry_policy
self.tags = tags
self.metadata = metadata
if self.bound is not DEFAULT_BOUND:
try:
subgraph = find_subgraph_pregel(self.bound)
except Exception:
subgraph = None
if subgraph:
self.subgraphs = [subgraph]
else:
self.subgraphs = []
else:
self.subgraphs = []
def copy(self, update: dict[str, Any]) -> PregelNode:
attrs = {**self.__dict__, **update}
attrs.pop("subgraphs")
return PregelNode(**attrs)
@cached_property
+9 -11
View File
@@ -39,7 +39,7 @@ from langgraph.errors import GraphBubbleUp, GraphInterrupt
from langgraph.pregel.algo import Call
from langgraph.pregel.executor import Submit
from langgraph.pregel.retry import arun_with_retry, run_with_retry
from langgraph.types import PregelExecutableTask, RetryPolicy
from langgraph.types import PregelExecutableTask, PregelScratchpad, RetryPolicy
from langgraph.utils.future import chain_future
F = TypeVar("F", concurrent.futures.Future, asyncio.Future)
@@ -135,8 +135,7 @@ class PregelRunner:
return task.config[CONF][CONFIG_KEY_SEND](writes)
# schedule PUSH tasks, collect futures
scratchpad = task.config[CONF][CONFIG_KEY_SCRATCHPAD]
scratchpad.setdefault("call_counter", 0)
scratchpad: PregelScratchpad = task.config[CONF][CONFIG_KEY_SCRATCHPAD]
rtn: dict[int, Optional[concurrent.futures.Future]] = {}
for idx, w in enumerate(writes):
# bail if not a PUSH write
@@ -144,9 +143,9 @@ class PregelRunner:
continue
# schedule the next task, if the callback returns one
wcall = calls[idx] if calls else None
cnt = scratchpad["call_counter"]
scratchpad["call_counter"] += 1
if next_task := self.schedule_task(task, cnt, wcall):
if next_task := self.schedule_task(
task, scratchpad.call_counter(), wcall
):
if fut := next(
(
f
@@ -324,8 +323,7 @@ class PregelRunner:
return task.config[CONF][CONFIG_KEY_SEND](writes)
# schedule PUSH tasks, collect futures
scratchpad = task.config[CONF][CONFIG_KEY_SCRATCHPAD]
scratchpad.setdefault("call_counter", 0)
scratchpad: PregelScratchpad = task.config[CONF][CONFIG_KEY_SCRATCHPAD]
rtn: dict[int, Optional[asyncio.Future]] = {}
for idx, w in enumerate(writes):
# bail if not a PUSH write
@@ -333,9 +331,9 @@ class PregelRunner:
continue
# schedule the next task, if the callback returns one
wcall = calls[idx] if calls is not None else None
cnt = scratchpad["call_counter"]
scratchpad["call_counter"] += 1
if next_task := self.schedule_task(task, cnt, wcall):
if next_task := self.schedule_task(
task, scratchpad.call_counter(), wcall
):
# if the parent task was retried,
# the next task might already be running
if fut := next(
+1 -1
View File
@@ -26,7 +26,7 @@ def get_new_channel_versions(
return new_versions
def find_subgraph_pregel(candidate: Runnable) -> Optional[Runnable]:
def find_subgraph_pregel(candidate: Runnable) -> Optional[PregelProtocol]:
from langgraph.pregel import Pregel
candidates: list[Runnable] = [candidate]
+37 -24
View File
@@ -16,14 +16,16 @@ from typing import (
TypeVar,
Union,
cast,
get_type_hints,
)
from langchain_core.runnables import Runnable, RunnableConfig
from typing_extensions import Self, TypedDict
from typing_extensions import Self
from langgraph.checkpoint.base import BaseCheckpointSaver, CheckpointMetadata
if TYPE_CHECKING:
from langgraph.pregel.protocol import PregelProtocol
from langgraph.store.base import BaseStore
@@ -47,12 +49,13 @@ Checkpointer = Union[None, bool, BaseCheckpointSaver]
StreamMode = Literal["values", "updates", "debug", "messages", "custom"]
"""How the stream method should emit outputs.
- 'values': Emit all values of the state for each step.
- 'updates': Emit only the node name(s) and updates
that were returned by the node(s) **after** each step.
- 'debug': Emit debug events for each step.
- 'messages': Emit LLM messages token-by-token.
- 'custom': Emit custom output `write: StreamWriter` kwarg of each node.
- `"values"`: Emit all values in the state after each step.
When used with functional API, values are emitted once at the end of the workflow.
- `"updates"`: Emit only the node or task names and updates returned by the nodes or tasks after each step.
If multiple updates are made in the same step (e.g. multiple nodes are run) then those updates are emitted separately.
- `"custom"`: Emit custom data using from inside nodes or tasks using `StreamWriter`.
- `"messages"`: Emit LLM messages token-by-token together with metadata for any LLM invocations inside nodes or tasks.
- `"debug"`: Emit debug events with as much information as possible for each step.
"""
StreamWriter = Callable[[Any], None]
@@ -153,6 +156,7 @@ class PregelExecutableTask(NamedTuple):
path: tuple[Union[str, int, tuple], ...]
scheduled: bool = False
writers: Sequence[Runnable] = ()
subgraphs: Sequence["PregelProtocol"] = ()
class StateSnapshot(NamedTuple):
@@ -289,6 +293,8 @@ class Command(Generic[N], ToolOutputMixin):
for t in self.update
):
return self.update
elif hints := get_type_hints(type(self.update)):
return [(k, getattr(self.update, k)) for k in hints]
elif self.update is not None:
return [("__root__", self.update)]
else:
@@ -339,15 +345,25 @@ class LoopProtocol:
self.stop = stop
class PregelScratchpad(TypedDict):
@dataclasses.dataclass(**{**_DC_KWARGS, "frozen": False})
class PregelScratchpad:
# call
call_counter: int
call_counter: Callable[[], int]
# interrupt
interrupt_counter: int
interrupt_counter: Callable[[], int]
resume: list[Any]
null_resume: Any
null_resume: Optional[Any]
_consume_null_resume: Callable[[], None]
# subgraph
subgraph_counter: int
subgraph_counter: Callable[[], int]
def consume_null_resume(self) -> Any:
if self.null_resume is not None:
value = self.null_resume
self._consume_null_resume()
self.null_resume = None
return value
raise ValueError("No null resume to consume")
def interrupt(value: Any) -> Any:
@@ -449,7 +465,6 @@ def interrupt(value: Any) -> Any:
CONFIG_KEY_CHECKPOINT_NS,
CONFIG_KEY_SCRATCHPAD,
CONFIG_KEY_SEND,
MISSING,
NS_SEP,
RESUME,
)
@@ -459,19 +474,17 @@ def interrupt(value: Any) -> Any:
conf = get_config()["configurable"]
# track interrupt index
scratchpad: PregelScratchpad = conf[CONFIG_KEY_SCRATCHPAD]
scratchpad["interrupt_counter"] += 1
idx = scratchpad["interrupt_counter"]
idx = scratchpad.interrupt_counter()
# find previous resume values
if scratchpad["resume"]:
if idx < len(scratchpad["resume"]):
return scratchpad["resume"][idx]
if scratchpad.resume:
if idx < len(scratchpad.resume):
return scratchpad.resume[idx]
# find current resume value
if scratchpad["null_resume"] is not MISSING:
assert len(scratchpad["resume"]) == idx, (scratchpad["resume"], idx)
v = scratchpad["null_resume"]
scratchpad["null_resume"] = MISSING
scratchpad["resume"].append(v)
conf[CONFIG_KEY_SEND]([(RESUME, scratchpad["resume"])])
if scratchpad.null_resume is not None:
assert len(scratchpad.resume) == idx, (scratchpad.resume, idx)
v = scratchpad.consume_null_resume()
scratchpad.resume.append(v)
conf[CONFIG_KEY_SEND]([(RESUME, scratchpad.resume)])
return v
# no resume value found
raise GraphInterrupt(
+1 -24
View File
@@ -1,5 +1,3 @@
import asyncio
import sys
from collections import ChainMap
from typing import Any, Optional, Sequence, cast
@@ -18,16 +16,15 @@ from langchain_core.runnables.config import (
)
from langgraph.checkpoint.base import CheckpointMetadata
from langgraph.config import get_config, get_store, get_stream_writer # noqa
from langgraph.constants import (
CONF,
CONFIG_KEY_CHECKPOINT_ID,
CONFIG_KEY_CHECKPOINT_MAP,
CONFIG_KEY_CHECKPOINT_NS,
CONFIG_KEY_STORE,
NS_END,
NS_SEP,
)
from langgraph.store.base import BaseStore
def recast_checkpoint_ns(ns: str) -> str:
@@ -320,23 +317,3 @@ def ensure_config(*configs: Optional[RunnableConfig]) -> RunnableConfig:
):
empty["metadata"][key] = value
return empty
def get_config() -> RunnableConfig:
if sys.version_info < (3, 11):
try:
if asyncio.current_task():
raise RuntimeError(
"Python 3.11 or later required to use this in an async context"
)
except RuntimeError:
pass
if var_config := var_child_runnable_config.get():
return var_config
else:
raise RuntimeError("Called get_config outside of a runnable context")
def get_store() -> BaseStore:
config = get_config()
return config[CONF][CONFIG_KEY_STORE]
+59 -26
View File
@@ -14,6 +14,7 @@ from typing import (
Iterator,
Optional,
Sequence,
Tuple,
Union,
cast,
)
@@ -36,7 +37,7 @@ from typing_extensions import TypeGuard
from langgraph.constants import (
CONF,
CONFIG_KEY_END,
CONFIG_KEY_PREVIOUS,
CONFIG_KEY_STORE,
CONFIG_KEY_STREAM_WRITER,
)
@@ -66,9 +67,10 @@ class StrEnum(str, enum.Enum):
# Special type to denote any type is accepted
ANY_TYPE = object()
ASYNCIO_ACCEPTS_CONTEXT = sys.version_info >= (3, 11)
# List of keyword arguments that can be injected at runtime from the config object.
# A named argument may appear multiple times if it appears with distinct types.
KWARGS_CONFIG_KEYS: tuple[tuple[str, tuple[Any, ...], str, Any], ...] = (
(
sys.intern("writer"),
@@ -77,15 +79,35 @@ KWARGS_CONFIG_KEYS: tuple[tuple[str, tuple[Any, ...], str, Any], ...] = (
lambda _: None,
),
(
# Covers store that is not optional (will raise an error if a store
# cannot be injected).
sys.intern("store"),
(BaseStore, "BaseStore", inspect.Parameter.empty),
(
BaseStore,
"BaseStore",
inspect.Parameter.empty,
),
CONFIG_KEY_STORE,
inspect.Parameter.empty,
),
(
# Covers store that is optional. Will set to None if not found in config.
sys.intern("store"),
(
Optional[BaseStore],
# Best effort to catch some forward references.
# This will not work for cases like `"Union[None, BaseStore]"`,
# we'll need to re-write logic to use get_type_hints()
# to resolve forward references.
"Optional[BaseStore]",
),
CONFIG_KEY_STORE,
None,
),
(
sys.intern("previous"),
(ANY_TYPE,),
CONFIG_KEY_END,
CONFIG_KEY_PREVIOUS,
inspect.Parameter.empty,
),
)
@@ -149,15 +171,24 @@ class RunnableCallable(Runnable):
params = inspect.signature(cast(Callable, func or afunc)).parameters
self.func_accepts_config = "config" in params
self.func_accepts: dict[str, bool] = {}
for kw, typ, _, _ in KWARGS_CONFIG_KEYS:
# Mapping from kwarg name to (config key, default value) to be used.
# The default value is used if the config key is not found in the config.
self.func_accepts: dict[str, Tuple[str, Any]] = {}
for kw, typ, config_key, default in KWARGS_CONFIG_KEYS:
p = params.get(kw)
if typ == (ANY_TYPE,):
self.func_accepts[kw] = p is not None and p.kind in VALID_KINDS
else:
self.func_accepts[kw] = (
p is not None and p.annotation in typ and p.kind in VALID_KINDS
)
if p is None or p.kind not in VALID_KINDS:
# If parameter is not found or is not a valid kind, skip
continue
if typ != (ANY_TYPE,) and p.annotation not in typ:
# A specific type is required, but the function annotation does
# not match the expected type.
continue
# If the kwarg is accepted by the function, store the default value
self.func_accepts[kw] = (config_key, default)
def __repr__(self) -> str:
repr_args = {
@@ -187,20 +218,22 @@ class RunnableCallable(Runnable):
if self.func_accepts_config:
kwargs["config"] = config
_conf = config[CONF]
for kw, _, config_key, default_value in KWARGS_CONFIG_KEYS:
if not self.func_accepts[kw]:
for kw, (config_key, default_value) in self.func_accepts.items():
# If the kwarg is already set, use the set value
if kw in kwargs:
continue
if (
default_value is inspect.Parameter.empty
and kw not in kwargs
and config_key not in _conf
# If the kwarg is requested, but isn't in the config AND has no
# default value, raise an error
config_key not in _conf and default_value is inspect.Parameter.empty
):
raise ValueError(
f"Missing required config key '{config_key}' for '{self.name}'."
)
elif kwargs.get(kw) is None:
kwargs[kw] = _conf.get(config_key, default_value)
kwargs[kw] = _conf.get(config_key, default_value)
context = copy_context()
if self.trace:
@@ -244,20 +277,20 @@ class RunnableCallable(Runnable):
if self.func_accepts_config:
kwargs["config"] = config
_conf = config[CONF]
for kw, _, config_key, default_value in KWARGS_CONFIG_KEYS:
if not self.func_accepts[kw]:
for kw, (config_key, default_value) in self.func_accepts.items():
# If the kwarg has already been set, use the set value
if kw in kwargs:
continue
if (
default_value is inspect.Parameter.empty
and kw not in kwargs
and config_key not in _conf
# If the kwarg is requested, but isn't in the config AND has no
# default value, raise an error
config_key not in _conf and default_value is inspect.Parameter.empty
):
raise ValueError(
f"Missing required config key '{config_key}' for '{self.name}'."
)
elif kwargs.get(kw) is None:
kwargs[kw] = _conf.get(config_key, default_value)
kwargs[kw] = _conf.get(config_key, default_value)
context = copy_context()
if self.trace:
callback_manager = get_async_callback_manager_for_config(config, self.tags)
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langgraph"
version = "0.2.66"
version = "0.2.69"
description = "Building stateful, multi-actor applications with LLMs"
authors = []
license = "MIT"
File diff suppressed because one or more lines are too long
@@ -1,334 +1,4 @@
# serializer version: 1
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class[memory]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class[postgres_aio]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class[postgres_aio_pipe]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class[postgres_aio_pool]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class[sqlite_aio]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class_pydantic2.1
dict({
'$defs': dict({
'InnerObject': dict({
'properties': dict({
'yo': dict({
'title': 'Yo',
'type': 'integer',
}),
}),
'required': list([
'yo',
]),
'title': 'InnerObject',
'type': 'object',
}),
}),
'properties': dict({
'answer': dict({
'anyOf': list([
dict({
'type': 'string',
}),
dict({
'type': 'null',
}),
]),
'default': None,
'title': 'Answer',
}),
'docs': dict({
'items': dict({
'type': 'string',
}),
'title': 'Docs',
'type': 'array',
}),
'inner': dict({
'$ref': '#/$defs/InnerObject',
}),
'query': dict({
'title': 'Query',
'type': 'string',
}),
}),
'required': list([
'query',
'inner',
'docs',
]),
'title': 'State',
'type': 'object',
})
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class_pydantic2.2
dict({
'$defs': dict({
'InnerObject': dict({
'properties': dict({
'yo': dict({
'title': 'Yo',
'type': 'integer',
}),
}),
'required': list([
'yo',
]),
'title': 'InnerObject',
'type': 'object',
}),
}),
'properties': dict({
'answer': dict({
'anyOf': list([
dict({
'type': 'string',
}),
dict({
'type': 'null',
}),
]),
'default': None,
'title': 'Answer',
}),
'docs': dict({
'items': dict({
'type': 'string',
}),
'title': 'Docs',
'type': 'array',
}),
'inner': dict({
'$ref': '#/$defs/InnerObject',
}),
'query': dict({
'title': 'Query',
'type': 'string',
}),
}),
'required': list([
'query',
'inner',
'docs',
]),
'title': 'State',
'type': 'object',
})
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_custom_state_class_pydantic2[memory]
'''
graph TD;
@@ -1055,253 +725,6 @@
'type': 'object',
})
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch[memory]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch[postgres_aio]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch[postgres_aio_pipe]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch[postgres_aio_pool]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_in_one_fan_out_state_graph_waiting_edge_via_branch[sqlite_aio]
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+---------------+
| rewrite_query |
+---------------+
*** ...
* .
** ...
+--------------+ .
| analyzer_one | .
+--------------+ .
* .
* .
* .
+---------------+ +---------------+
| retriever_one | | retriever_two |
+---------------+ +---------------+
*** ***
* *
** **
+----+
| qa |
+----+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_nested_graph
'''
+-----------+
| __start__ |
+-----------+
*
*
*
+-------+
| inner |
+-------+
*
*
*
+------+
| side |
+------+
*
*
*
+---------+
| __end__ |
+---------+
'''
# ---
# name: test_send_react_interrupt_control[memory]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
@@ -1392,128 +815,3 @@
'''
# ---
# name: test_weather_subgraph[memory]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
router_node(router_node)
normal_llm_node(normal_llm_node)
weather_graph_model_node(model_node)
weather_graph_weather_node(weather_node<hr/><small><em>__interrupt = before</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> router_node;
normal_llm_node --> __end__;
weather_graph_weather_node --> __end__;
router_node -.-> normal_llm_node;
router_node -.-> weather_graph_model_node;
router_node -.-> __end__;
subgraph weather_graph
weather_graph_model_node --> weather_graph_weather_node;
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_weather_subgraph[postgres_aio]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
router_node(router_node)
normal_llm_node(normal_llm_node)
weather_graph_model_node(model_node)
weather_graph_weather_node(weather_node<hr/><small><em>__interrupt = before</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> router_node;
normal_llm_node --> __end__;
weather_graph_weather_node --> __end__;
router_node -.-> normal_llm_node;
router_node -.-> weather_graph_model_node;
router_node -.-> __end__;
subgraph weather_graph
weather_graph_model_node --> weather_graph_weather_node;
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_weather_subgraph[postgres_aio_pipe]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
router_node(router_node)
normal_llm_node(normal_llm_node)
weather_graph_model_node(model_node)
weather_graph_weather_node(weather_node<hr/><small><em>__interrupt = before</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> router_node;
normal_llm_node --> __end__;
weather_graph_weather_node --> __end__;
router_node -.-> normal_llm_node;
router_node -.-> weather_graph_model_node;
router_node -.-> __end__;
subgraph weather_graph
weather_graph_model_node --> weather_graph_weather_node;
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_weather_subgraph[postgres_aio_pool]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
router_node(router_node)
normal_llm_node(normal_llm_node)
weather_graph_model_node(model_node)
weather_graph_weather_node(weather_node<hr/><small><em>__interrupt = before</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> router_node;
normal_llm_node --> __end__;
weather_graph_weather_node --> __end__;
router_node -.-> normal_llm_node;
router_node -.-> weather_graph_model_node;
router_node -.-> __end__;
subgraph weather_graph
weather_graph_model_node --> weather_graph_weather_node;
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_weather_subgraph[sqlite_aio]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
router_node(router_node)
normal_llm_node(normal_llm_node)
weather_graph_model_node(model_node)
weather_graph_weather_node(weather_node<hr/><small><em>__interrupt = before</em></small>)
__end__([<p>__end__</p>]):::last
__start__ --> router_node;
normal_llm_node --> __end__;
weather_graph_weather_node --> __end__;
router_node -.-> normal_llm_node;
router_node -.-> weather_graph_model_node;
router_node -.-> __end__;
subgraph weather_graph
weather_graph_model_node --> weather_graph_weather_node;
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
+3 -3
View File
@@ -2829,9 +2829,9 @@ def test_state_graph_packets(
# Define decision-making logic
def should_continue(data: AgentState) -> str:
assert isinstance(data["session"], httpx.Client)
assert data["something_extra"] == "hi there", (
"nodes can pass extra data to their cond edges, which isn't saved in state"
)
assert (
data["something_extra"] == "hi there"
), "nodes can pass extra data to their cond edges, which isn't saved in state"
# Logic to decide whether to continue in the loop or exit
if tool_calls := data["messages"][-1].tool_calls:
return [Send("tools", tool_call) for tool_call in tool_calls]
+169 -67
View File
@@ -62,6 +62,7 @@ from langgraph.prebuilt.tool_node import (
from langgraph.store.base import BaseStore
from langgraph.store.memory import InMemoryStore
from langgraph.types import Command, Interrupt, interrupt
from langgraph.utils.config import get_stream_writer
from tests.conftest import (
ALL_CHECKPOINTERS_ASYNC,
ALL_CHECKPOINTERS_SYNC,
@@ -147,7 +148,7 @@ class FakeToolCallingModel(BaseChatModel):
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_no_modifier(request: pytest.FixtureRequest, checkpointer_name: str) -> None:
def test_no_prompt(request: pytest.FixtureRequest, checkpointer_name: str) -> None:
checkpointer: BaseCheckpointSaver = request.getfixturevalue(
"checkpointer_" + checkpointer_name
)
@@ -181,7 +182,7 @@ def test_no_modifier(request: pytest.FixtureRequest, checkpointer_name: str) ->
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_no_modifier_async(checkpointer_name: str) -> None:
async def test_no_prompt_async(checkpointer_name: str) -> None:
async with awith_checkpointer(checkpointer_name) as checkpointer:
model = FakeToolCallingModel()
@@ -217,16 +218,17 @@ def test_passing_two_modifiers():
with pytest.raises(ValueError):
create_react_agent(model, [], messages_modifier="Foo", state_modifier="Bar")
with pytest.raises(ValueError):
create_react_agent(model, [], messages_modifier="Foo", prompt="Bar")
def test_system_message_modifier():
messages_modifier = SystemMessage(content="Foo")
agent_1 = create_react_agent(
FakeToolCallingModel(), [], messages_modifier=messages_modifier
)
agent_2 = create_react_agent(
FakeToolCallingModel(), [], state_modifier=messages_modifier
)
for agent in [agent_1, agent_2]:
def test_system_message_prompt():
prompt = SystemMessage(content="Foo")
for agent in (
create_react_agent(FakeToolCallingModel(), [], prompt=prompt),
create_react_agent(FakeToolCallingModel(), [], messages_modifier=prompt),
create_react_agent(FakeToolCallingModel(), [], state_modifier=prompt),
):
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {
@@ -235,15 +237,13 @@ def test_system_message_modifier():
assert response == expected_response
def test_system_message_string_modifier():
messages_modifier = "Foo"
agent_1 = create_react_agent(
FakeToolCallingModel(), [], messages_modifier=messages_modifier
)
agent_2 = create_react_agent(
FakeToolCallingModel(), [], state_modifier=messages_modifier
)
for agent in [agent_1, agent_2]:
def test_string_prompt():
prompt = "Foo"
for agent in (
create_react_agent(FakeToolCallingModel(), [], prompt=prompt),
create_react_agent(FakeToolCallingModel(), [], messages_modifier=prompt),
create_react_agent(FakeToolCallingModel(), [], state_modifier=prompt),
):
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {
@@ -252,63 +252,71 @@ def test_system_message_string_modifier():
assert response == expected_response
def test_callable_messages_modifier():
model = FakeToolCallingModel()
def test_callable_prompt():
def messages_modifier(messages):
modified_message = f"Bar {messages[-1].content}"
return [HumanMessage(content=modified_message)]
agent = create_react_agent(model, [], messages_modifier=messages_modifier)
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {"messages": inputs + [AIMessage(content="Bar hi?", id="0")]}
assert response == expected_response
def test_callable_state_modifier():
model = FakeToolCallingModel()
def state_modifier(state):
def prompt(state):
modified_message = f"Bar {state['messages'][-1].content}"
return [HumanMessage(content=modified_message)]
agent = create_react_agent(model, [], state_modifier=state_modifier)
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {"messages": inputs + [AIMessage(content="Bar hi?", id="0")]}
assert response == expected_response
for agent in (
create_react_agent(FakeToolCallingModel(), [], prompt=prompt),
create_react_agent(FakeToolCallingModel(), [], state_modifier=prompt),
create_react_agent(
FakeToolCallingModel(), [], messages_modifier=messages_modifier
),
):
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {
"messages": inputs + [AIMessage(content="Bar hi?", id="0")]
}
assert response == expected_response
def test_runnable_messages_modifier():
model = FakeToolCallingModel()
async def test_callable_prompt_async():
async def prompt(state):
modified_message = f"Bar {state['messages'][-1].content}"
return [HumanMessage(content=modified_message)]
for agent in (
create_react_agent(FakeToolCallingModel(), [], prompt=prompt),
create_react_agent(FakeToolCallingModel(), [], state_modifier=prompt),
):
inputs = [HumanMessage("hi?")]
response = await agent.ainvoke({"messages": inputs})
expected_response = {
"messages": inputs + [AIMessage(content="Bar hi?", id="0")]
}
assert response == expected_response
def test_runnable_prompt():
messages_modifier = RunnableLambda(
lambda messages: [HumanMessage(content=f"Baz {messages[-1].content}")]
)
agent = create_react_agent(model, [], messages_modifier=messages_modifier)
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {"messages": inputs + [AIMessage(content="Baz hi?", id="0")]}
assert response == expected_response
def test_runnable_state_modifier():
model = FakeToolCallingModel()
state_modifier = RunnableLambda(
prompt = RunnableLambda(
lambda state: [HumanMessage(content=f"Baz {state['messages'][-1].content}")]
)
agent = create_react_agent(model, [], state_modifier=state_modifier)
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {"messages": inputs + [AIMessage(content="Baz hi?", id="0")]}
assert response == expected_response
for agent in (
create_react_agent(FakeToolCallingModel(), [], prompt=prompt),
create_react_agent(FakeToolCallingModel(), [], state_modifier=prompt),
create_react_agent(
FakeToolCallingModel(), [], messages_modifier=messages_modifier
),
):
inputs = [HumanMessage("hi?")]
response = agent.invoke({"messages": inputs})
expected_response = {
"messages": inputs + [AIMessage(content="Baz hi?", id="0")]
}
assert response == expected_response
def test_state_modifier_with_store():
def test_prompt_with_store():
def add(a: int, b: int):
"""Adds a and b"""
return a + b
@@ -317,20 +325,18 @@ def test_state_modifier_with_store():
in_memory_store.put(("memories", "1"), "user_name", {"data": "User name is Alice"})
in_memory_store.put(("memories", "2"), "user_name", {"data": "User name is Bob"})
def modify(state, config, *, store):
def prompt(state, config, *, store):
user_id = config["configurable"]["user_id"]
system_str = store.get(("memories", user_id), "user_name").value["data"]
return [SystemMessage(system_str)] + state["messages"]
def modify_no_store(state, config):
def prompt_no_store(state, config):
return SystemMessage("foo") + state["messages"]
model = FakeToolCallingModel()
# test state modifier that uses store works
agent = create_react_agent(
model, [add], state_modifier=modify, store=in_memory_store
)
agent = create_react_agent(model, [add], prompt=prompt, store=in_memory_store)
response = agent.invoke(
{"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}}
)
@@ -338,7 +344,7 @@ def test_state_modifier_with_store():
# test state modifier that doesn't use store works
agent = create_react_agent(
model, [add], state_modifier=modify_no_store, store=in_memory_store
model, [add], prompt=prompt_no_store, store=in_memory_store
)
response = agent.invoke(
{"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}}
@@ -346,6 +352,48 @@ def test_state_modifier_with_store():
assert response["messages"][-1].content == "foo-hi"
async def test_prompt_with_store_async():
async def add(a: int, b: int):
"""Adds a and b"""
return a + b
in_memory_store = InMemoryStore()
await in_memory_store.aput(
("memories", "1"), "user_name", {"data": "User name is Alice"}
)
await in_memory_store.aput(
("memories", "2"), "user_name", {"data": "User name is Bob"}
)
async def prompt(state, config, *, store):
user_id = config["configurable"]["user_id"]
system_str = (await store.aget(("memories", user_id), "user_name")).value[
"data"
]
return [SystemMessage(system_str)] + state["messages"]
async def prompt_no_store(state, config):
return SystemMessage("foo") + state["messages"]
model = FakeToolCallingModel()
# test state modifier that uses store works
agent = create_react_agent(model, [add], prompt=prompt, store=in_memory_store)
response = await agent.ainvoke(
{"messages": [("user", "hi")]}, {"configurable": {"user_id": "1"}}
)
assert response["messages"][-1].content == "User name is Alice-hi"
# test state modifier that doesn't use store works
agent = create_react_agent(
model, [add], prompt=prompt_no_store, store=in_memory_store
)
response = await agent.ainvoke(
{"messages": [("user", "hi")]}, {"configurable": {"user_id": "2"}}
)
assert response["messages"][-1].content == "foo-hi"
@pytest.mark.parametrize("tool_style", ["openai", "anthropic"])
def test_model_with_tools(tool_style: str):
model = FakeToolCallingModel(tool_style=tool_style)
@@ -1619,7 +1667,7 @@ def test_react_agent_update_state():
}
)
def state_modifier(state: State):
def prompt(state: State):
user_name = state.get("user_name")
if user_name is None:
return state["messages"]
@@ -1634,7 +1682,7 @@ def test_react_agent_update_state():
model,
[get_user_name],
state_schema=State,
state_modifier=state_modifier,
prompt=prompt,
checkpointer=checkpointer,
)
config = {"configurable": {"thread_id": "1"}}
@@ -2167,3 +2215,57 @@ def test_react_with_subgraph_tools() -> None:
content="What's 2 + 3 and 2 * 3?-What's 2 + 3 and 2 * 3?-5-6", id="1"
),
]
def test_tool_node_stream_writer() -> None:
@dec_tool
def streaming_tool(x: int) -> str:
"""Do something with writer."""
my_writer = get_stream_writer()
for value in ["foo", "bar", "baz"]:
my_writer({"custom_tool_value": value})
return x
tool_node = ToolNode([streaming_tool])
graph = (
StateGraph(MessagesState)
.add_node("tools", tool_node)
.add_edge(START, "tools")
.compile()
)
tool_call = {
"name": "streaming_tool",
"args": {"x": 1},
"id": "1",
"type": "tool_call",
}
inputs = {
"messages": [AIMessage("", tool_calls=[tool_call])],
}
assert list(graph.stream(inputs, stream_mode="custom")) == [
{"custom_tool_value": "foo"},
{"custom_tool_value": "bar"},
{"custom_tool_value": "baz"},
]
assert list(graph.stream(inputs, stream_mode=["custom", "updates"])) == [
("custom", {"custom_tool_value": "foo"}),
("custom", {"custom_tool_value": "bar"}),
("custom", {"custom_tool_value": "baz"}),
(
"updates",
{
"tools": {
"messages": [
_AnyIdToolMessage(
content="1",
name="streaming_tool",
tool_call_id="1",
),
],
},
},
),
]
+387 -63
View File
@@ -1,4 +1,5 @@
import enum
import functools
import json
import logging
import operator
@@ -9,6 +10,7 @@ import warnings
from collections import Counter, deque
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from dataclasses import dataclass
from random import randrange
from typing import (
Annotated,
@@ -27,6 +29,7 @@ from typing import (
import httpx
import pytest
from langchain_core.language_models import GenericFakeChatModel
from langchain_core.runnables import (
RunnableConfig,
RunnableLambda,
@@ -78,6 +81,7 @@ from tests.conftest import (
from tests.memory_assert import MemorySaverAssertCheckpointMetadata
from tests.messages import (
_AnyIdAIMessage,
_AnyIdAIMessageChunk,
_AnyIdHumanMessage,
_AnyIdToolMessage,
)
@@ -1539,12 +1543,14 @@ def test_imp_nested(
@task
def submapper(input: int) -> str:
time.sleep(input / 100)
return str(input)
@task()
def mapper(input: int) -> str:
sub = submapper(input)
time.sleep(input / 100)
return submapper(input).result() * 2
return sub.result() * 2
@entrypoint(checkpointer=checkpointer)
def graph(input: list[int]) -> list[str]:
@@ -1565,8 +1571,6 @@ def test_imp_nested(
"title": "LangGraphOutput",
}
assert graph.get_graph().draw_mermaid() == snapshot
thread1 = {"configurable": {"thread_id": "1"}}
assert [*graph.stream([0, 1], thread1)] == [
{"submapper": "0"},
@@ -1616,8 +1620,6 @@ def test_imp_stream_order(
fut_baz = baz(fut_bar.result())
return fut_baz.result()
assert graph.get_graph().draw_mermaid() == snapshot
thread1 = {"configurable": {"thread_id": "1"}}
assert [c for c in graph.stream({"a": "0"}, thread1)] == [
{
@@ -4826,6 +4828,13 @@ def test_parent_command(request: pytest.FixtureRequest, checkpointer_name: str)
"source": "loop",
"writes": {
"alice": {
"messages": [
_AnyIdHumanMessage(
content="get user name",
additional_kwargs={},
response_metadata={},
),
],
"user_name": "Meow",
}
},
@@ -5045,8 +5054,6 @@ def test_interrupt_functional(
fut_bar = bar(bar_input)
return fut_bar.result()
assert graph.get_graph().draw_mermaid() == snapshot
config = {"configurable": {"thread_id": "1"}}
# First run, interrupted at bar
graph.invoke({"a": ""}, config)
@@ -5078,15 +5085,29 @@ def test_interrupt_task_functional(
fut_bar = bar(fut_foo.result())
return fut_bar.result()
assert graph.get_graph().draw_mermaid() == snapshot
config = {"configurable": {"thread_id": "1"}}
# First run, interrupted at bar
graph.invoke({"a": ""}, config)
assert not graph.invoke({"a": ""}, config)
# Resume with an answer
res = graph.invoke(Command(resume="bar"), config)
assert res == {"a": "foobar"}
# Test that we can interrupt the same task multiple times
config = {"configurable": {"thread_id": "2"}}
@entrypoint(checkpointer=checkpointer)
def graph(inputs: dict) -> dict:
foo_result = foo(inputs).result()
bar_result = bar(foo_result).result()
baz_result = bar(bar_result).result()
return baz_result
# First run, interrupted at bar
assert not graph.invoke({"a": ""}, config)
# Provide resumes
assert not graph.invoke(Command(resume="bar"), config)
assert graph.invoke(Command(resume="baz"), config) == {"a": "foobarbaz"}
def test_root_mixed_return() -> None:
def my_node(state: list[str]):
@@ -5116,6 +5137,35 @@ def test_dict_mixed_return() -> None:
assert graph.invoke({"foo": ""}) == {"foo": "ab"}
def test_command_pydantic_dataclass() -> None:
from pydantic import BaseModel
class PydanticState(BaseModel):
foo: str
@dataclass
class DataclassState:
foo: str
for State in (PydanticState, DataclassState):
def node_a(state) -> Command[Literal["node_b"]]:
return Command(
update=State(foo="foo"),
goto="node_b",
)
def node_b(state):
return {"foo": state.foo + "bar"}
builder = StateGraph(State)
builder.add_edge(START, "node_a")
builder.add_node(node_a)
builder.add_node(node_b)
graph = builder.compile()
assert graph.invoke(State(foo="")) == {"foo": "foobar"}
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_command_with_static_breakpoints(
request: pytest.FixtureRequest, checkpointer_name: str
@@ -5524,18 +5574,16 @@ def test_falsy_return_from_task(
falsy_task().result()
interrupt("test")
assert graph.get_graph().draw_mermaid() == snapshot
configurable = {"configurable": {"thread_id": str(uuid.uuid4())}}
graph.invoke({"a": 5}, configurable)
graph.invoke(Command(resume="123"), configurable)
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_multiple_interrupts_imperative(
def test_multiple_interrupts_functional(
request: pytest.FixtureRequest, checkpointer_name: str, snapshot: SnapshotAssertion
):
"""Test multiple interrupts with an imperative API."""
"""Test multiple interrupts with functional API."""
checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")
counter = 0
@@ -5558,8 +5606,6 @@ def test_multiple_interrupts_imperative(
return {"values": values}
assert graph.get_graph().draw_mermaid() == snapshot
configurable = {"configurable": {"thread_id": str(uuid.uuid4())}}
graph.invoke({}, configurable)
graph.invoke(Command(resume="a"), configurable)
@@ -5787,50 +5833,13 @@ def test_entrypoint_from_sync_generator() -> None:
"""@entrypoint does not support sync generators."""
previous_return_values = []
@entrypoint(checkpointer=MemorySaver())
def foo(inputs, previous=None) -> Any:
previous_return_values.append(previous)
yield "a"
yield "b"
with pytest.raises(NotImplementedError):
config = {"configurable": {"thread_id": "1"}}
assert foo.invoke({"a": "1"}, config) == ["a", "b"]
assert previous_return_values == [None]
assert foo.invoke({"a": "2"}, config) == ["a", "b"]
assert previous_return_values == [None, ["a", "b"]]
def test_entrypoint_request_stream_writer() -> None:
"""Test using a stream writer with an entrypoint."""
@entrypoint(checkpointer=MemorySaver())
def foo(inputs, writer: StreamWriter) -> Any:
writer("a")
yield "b"
config = {"configurable": {"thread_id": "1"}}
# Different invocations
# Are any of these confusing or unexpected?
assert list(foo.invoke({}, config)) == ["b"]
assert list(foo.stream({}, config)) == ["a", "b"]
# Stream modes
assert list(foo.stream({}, config, stream_mode=["updates"])) == [
("updates", {"foo": ["b"]})
]
assert list(foo.stream({}, config, stream_mode=["values"])) == [("values", ["b"])]
assert list(foo.stream({}, config, stream_mode=["custom"])) == [
(
"custom",
"a",
),
(
"custom",
"b",
),
]
@entrypoint(checkpointer=MemorySaver())
def foo(inputs, previous=None) -> Any:
previous_return_values.append(previous)
yield "a"
yield "b"
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
@@ -5913,7 +5922,7 @@ def test_multiple_subgraphs_functional(
# Define addition subgraph
@entrypoint()
def add(inputs):
def add(inputs: tuple[int, int]):
a, b = inputs
return a + b
@@ -5923,7 +5932,7 @@ def test_multiple_subgraphs_functional(
return a * b
@entrypoint()
def multiply(inputs):
def multiply(inputs: tuple[int, int]):
return multiply_task(*inputs).result()
# Test calling the same subgraph multiple times
@@ -5956,9 +5965,10 @@ def test_multiple_subgraphs_functional(
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_multiple_subgraphs_mixed(
def test_multiple_subgraphs_mixed_entrypoint(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
"""Test calling multiple StateGraph subgraphs from an entrypoint."""
checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")
class State(TypedDict):
@@ -6016,7 +6026,77 @@ def test_multiple_subgraphs_mixed(
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_multiple_subgraphs_mixed_checkpointer(
def test_multiple_subgraphs_mixed_state_graph(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
"""Test calling multiple entrypoint "subgraphs" from a StateGraph."""
checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")
class State(TypedDict):
a: int
b: int
class Output(TypedDict):
result: int
# Define addition subgraph
@entrypoint()
def add(inputs: tuple[int, int]):
a, b = inputs
return a + b
# Define multiplication subgraph using tasks
@task
def multiply_task(a, b):
return a * b
@entrypoint()
def multiply(inputs: tuple[int, int]):
return multiply_task(*inputs).result()
# Test calling the same subgraph multiple times
def call_same_subgraph(state):
result = add.invoke([state["a"], state["b"]])
another_result = add.invoke([result, 10])
return {"result": another_result}
parent_call_same_subgraph = (
StateGraph(State, output=Output)
.add_node(call_same_subgraph)
.add_edge(START, "call_same_subgraph")
.compile(checkpointer=checkpointer)
)
config = {"configurable": {"thread_id": "1"}}
assert parent_call_same_subgraph.invoke({"a": 2, "b": 3}, config) == {"result": 15}
# Test calling multiple subgraphs
class Output(TypedDict):
add_result: int
multiply_result: int
def call_multiple_subgraphs(state):
add_result = add.invoke([state["a"], state["b"]])
multiply_result = multiply.invoke([state["a"], state["b"]])
return {
"add_result": add_result,
"multiply_result": multiply_result,
}
parent_call_multiple_subgraphs = (
StateGraph(State, output=Output)
.add_node(call_multiple_subgraphs)
.add_edge(START, "call_multiple_subgraphs")
.compile(checkpointer=checkpointer)
)
config = {"configurable": {"thread_id": "2"}}
assert parent_call_multiple_subgraphs.invoke({"a": 2, "b": 3}, config) == {
"add_result": 5,
"multiply_result": 6,
}
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_SYNC)
def test_multiple_subgraphs_checkpointer(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
checkpointer = request.getfixturevalue(f"checkpointer_{checkpointer_name}")
@@ -6092,3 +6172,247 @@ def test_multiple_subgraphs_mixed_checkpointer(
),
((), {"parent_node": {"parent_counter": 7}}),
]
def test_merging_updates_command_parent():
# simple reducer
def append_unique(left, right):
combined = list(left)
for item in right:
if item in combined:
continue
else:
combined.append(item)
return combined
class State(TypedDict):
foo: str
bar: Annotated[list[str], append_unique]
# Define subgraph
def subgraph_node_1(state: State):
return Command(
goto="subgraph_node_2",
update={
"foo": "foo",
"bar": ["subgraph_node_1"],
},
)
def subgraph_node_2(state: State):
return Command(
goto="node_3",
update={"bar": ["subgraph_node_2"]},
graph=Command.PARENT,
)
subgraph_builder = StateGraph(State)
subgraph_builder.add_node(subgraph_node_1)
subgraph_builder.add_node(subgraph_node_2)
subgraph_builder.add_edge(START, "subgraph_node_1")
# Define main graph
def node_1(state: State):
return Command(
goto="node_2",
update={"bar": ["node_1"]},
)
def node_3(state: State, store):
return Command(
update={"bar": ["node_3"]},
)
main_builder = StateGraph(State)
main_builder.add_node("node_1", node_1)
main_builder.add_node("node_2", subgraph_builder.compile())
main_builder.add_node("node_3", node_3)
main_builder.add_edge(START, "node_1")
main_builder.add_edge("node_2", "node_3")
main_graph = main_builder.compile()
assert main_graph.invoke({"foo": ""}) == {
"foo": "foo",
"bar": ["node_1", "subgraph_node_1", "subgraph_node_2", "node_3"],
}
assert list(
main_graph.stream({"foo": ""}, stream_mode="updates", subgraphs=True)
) == [
((), {"node_1": {"bar": ["node_1"]}}),
(
(AnyStr("node_2:"),),
{"subgraph_node_1": {"foo": "foo", "bar": ["subgraph_node_1"]}},
),
(
(),
{
"node_2": [
{"foo": "foo"},
{"bar": ["node_1", "subgraph_node_1"]},
{"bar": ["subgraph_node_2"]},
]
},
),
((), {"node_3": {"bar": ["node_3"]}}),
]
def test_entrypoint_output_schema_with_return_and_save() -> None:
"""Test output schema inference with entrypoint.final."""
# Un-parameterized entrypoint.final is interpreted as entrypoint.final[Any, Any]
@entrypoint()
def foo2(inputs, *, previous: Any) -> entrypoint.final:
return entrypoint.final(value="foo", save=1)
assert foo2.get_output_schema().model_json_schema() == {
"title": "LangGraphOutput",
}
@entrypoint()
def foo(inputs, *, previous: Any) -> entrypoint.final[str, int]:
return entrypoint.final(value="foo", save=1)
assert foo.get_output_schema().model_json_schema() == {
"title": "LangGraphOutput",
"type": "string",
}
with pytest.raises(TypeError):
# Raise an exception on an improperly parameterized entrypoint.final
# User is attempting to parameterize in this case, so we'll offer
# a bit of help if it's not done correctly.
@entrypoint()
def foo(inputs, *, previous: Any) -> entrypoint.final[int]:
return entrypoint.final(value=1, save=1) # type: ignore
def test_entrypoint_with_return_and_save() -> None:
"""Test entrypoint with return and save."""
previous_ = None
@entrypoint(checkpointer=MemorySaver())
def foo(msg: str, *, previous: Any) -> entrypoint.final[int, list[str]]:
nonlocal previous_
previous_ = previous
previous = previous or []
return entrypoint.final(value=len(previous), save=previous + [msg])
assert foo.get_output_schema().model_json_schema() == {
"title": "LangGraphOutput",
"type": "integer",
}
config = {"configurable": {"thread_id": "1"}}
assert foo.invoke("hello", config) == 0
assert previous_ is None
assert foo.invoke("goodbye", config) == 1
assert previous_ == ["hello"]
assert foo.invoke("definitely", config) == 2
assert previous_ == ["hello", "goodbye"]
def test_overriding_injectable_args_with_tasks() -> None:
"""Test overriding injectable args in tasks."""
from langgraph.store.memory import InMemoryStore
@task
def foo(store: BaseStore, writer: StreamWriter, value: Any) -> None:
assert store is value
assert writer is value
@entrypoint(store=InMemoryStore())
def main(inputs, store: BaseStore) -> str:
assert store is not None
foo(store=None, writer=None, value=None).result()
foo(store="hello", writer="hello", value="hello").result()
return "OK"
assert main.invoke({}) == "OK"
def test_named_tasks_functional() -> None:
class Foo:
def foo(self, value: str) -> dict:
return value + "foo"
f = Foo()
# class method task
foo = task(f.foo, name="custom_foo")
other_foo = task(f.foo, name="other_foo")
# regular function task
@task(name="custom_bar")
def bar(value: str) -> dict:
return value + "|bar"
def baz(update: str, value: str) -> dict:
return value + f"|{update}"
# partial function task (unnamed)
baz_task = task(functools.partial(baz, "baz"))
# partial function task (named_)
custom_baz_task = task(functools.partial(baz, "custom_baz"), name="custom_baz")
class Qux:
def __call__(self, value: str) -> dict:
return value + "|qux"
qux_task = task(Qux(), name="qux")
@entrypoint()
def workflow(inputs: dict) -> dict:
foo_result = foo(inputs).result()
other_foo(inputs).result()
fut_bar = bar(foo_result)
fut_baz = baz_task(fut_bar.result())
fut_custom_baz = custom_baz_task(fut_baz.result())
fut_qux = qux_task(fut_custom_baz.result())
return fut_qux.result()
assert list(workflow.stream("", stream_mode="updates")) == [
{"custom_foo": "foo"},
{"other_foo": "foo"},
{"custom_bar": "foo|bar"},
{"baz": "foo|bar|baz"},
{"custom_baz": "foo|bar|baz|custom_baz"},
{"qux": "foo|bar|baz|custom_baz|qux"},
{"workflow": "foo|bar|baz|custom_baz|qux"},
]
def test_tags_stream_mode_messages() -> None:
model = GenericFakeChatModel(messages=iter(["foo"]), tags=["meow"])
graph = (
StateGraph(MessagesState)
.add_node(
"call_model", lambda state: {"messages": model.invoke(state["messages"])}
)
.add_edge(START, "call_model")
.compile()
)
assert list(
graph.stream(
{
"messages": "hi",
},
stream_mode="messages",
)
) == [
(
_AnyIdAIMessageChunk(content="foo"),
{
"langgraph_step": 1,
"langgraph_node": "call_model",
"langgraph_triggers": ["start:call_model"],
"langgraph_path": ("__pregel_pull", "call_model"),
"langgraph_checkpoint_ns": AnyStr("call_model:"),
"checkpoint_ns": AnyStr("call_model:"),
"ls_provider": "genericfakechatmodel",
"ls_model_type": "chat",
"tags": ["meow"],
},
)
]
+204 -16
View File
@@ -1,4 +1,5 @@
import asyncio
import functools
import logging
import operator
import random
@@ -25,6 +26,7 @@ from uuid import UUID
import httpx
import pytest
from langchain_core.language_models import GenericFakeChatModel
from langchain_core.runnables import (
RunnableConfig,
RunnableLambda,
@@ -81,6 +83,7 @@ from tests.memory_assert import (
)
from tests.messages import (
_AnyIdAIMessage,
_AnyIdAIMessageChunk,
_AnyIdHumanMessage,
_AnyIdToolMessage,
)
@@ -6145,6 +6148,13 @@ async def test_parent_command(checkpointer_name: str) -> None:
"source": "loop",
"writes": {
"alice": {
"messages": [
_AnyIdHumanMessage(
content="get user name",
additional_kwargs={},
response_metadata={},
),
],
"user_name": "Meow",
}
},
@@ -6812,8 +6822,8 @@ async def test_falsy_return_from_task(checkpointer_name: str) -> None:
@NEEDS_CONTEXTVARS
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multiple_interrupts_imperative(checkpointer_name: str) -> None:
"""Test multiple interrupts with an imperative API."""
async def test_multiple_interrupts_functional(checkpointer_name: str) -> None:
"""Test multiple interrupts with functional API."""
from langgraph.func import entrypoint, task
counter = 0
@@ -7130,7 +7140,9 @@ async def test_multiple_subgraphs_functional(checkpointer_name: str) -> None:
@NEEDS_CONTEXTVARS
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multiple_subgraphs_mixed(checkpointer_name: str) -> None:
async def test_multiple_subgraphs_mixed_entrypoint(checkpointer_name: str) -> None:
"""Test calling multiple StateGraph subgraphs from an entrypoint."""
class State(TypedDict):
a: int
b: int
@@ -7195,7 +7207,82 @@ async def test_multiple_subgraphs_mixed(checkpointer_name: str) -> None:
@NEEDS_CONTEXTVARS
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multiple_subgraphs_mixed_checkpointer(
async def test_multiple_subgraphs_mixed_state_graph(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
"""Test calling multiple entrypoint "subgraphs" from a StateGraph."""
async with awith_checkpointer(checkpointer_name) as checkpointer:
class State(TypedDict):
a: int
b: int
class Output(TypedDict):
result: int
# Define addition subgraph
@entrypoint()
async def add(inputs):
a, b = inputs
return a + b
# Define multiplication subgraph using tasks
@task
async def multiply_task(a, b):
return a * b
@entrypoint()
async def multiply(inputs):
return await multiply_task(*inputs)
# Test calling the same subgraph multiple times
async def call_same_subgraph(state):
result = await add.ainvoke([state["a"], state["b"]])
another_result = await add.ainvoke([result, 10])
return {"result": another_result}
parent_call_same_subgraph = (
StateGraph(State, output=Output)
.add_node(call_same_subgraph)
.add_edge(START, "call_same_subgraph")
.compile(checkpointer=checkpointer)
)
config = {"configurable": {"thread_id": "1"}}
assert await parent_call_same_subgraph.ainvoke({"a": 2, "b": 3}, config) == {
"result": 15
}
# Test calling multiple subgraphs
class Output(TypedDict):
add_result: int
multiply_result: int
async def call_multiple_subgraphs(state):
add_result = await add.ainvoke([state["a"], state["b"]])
multiply_result = await multiply.ainvoke([state["a"], state["b"]])
return {
"add_result": add_result,
"multiply_result": multiply_result,
}
parent_call_multiple_subgraphs = (
StateGraph(State, output=Output)
.add_node(call_multiple_subgraphs)
.add_edge(START, "call_multiple_subgraphs")
.compile(checkpointer=checkpointer)
)
config = {"configurable": {"thread_id": "2"}}
assert await parent_call_multiple_subgraphs.ainvoke(
{"a": 2, "b": 3}, config
) == {
"add_result": 5,
"multiply_result": 6,
}
@NEEDS_CONTEXTVARS
@pytest.mark.parametrize("checkpointer_name", ALL_CHECKPOINTERS_ASYNC)
async def test_multiple_subgraphs_checkpointer(
request: pytest.FixtureRequest, checkpointer_name: str
) -> None:
async with awith_checkpointer(checkpointer_name) as checkpointer:
@@ -7308,20 +7395,121 @@ async def test_async_entrypoint_without_checkpointer() -> None:
}
@NEEDS_CONTEXTVARS
async def test_entrypoint_from_async_generator() -> None:
"""@entrypoint does not support sync generators."""
# Test invoke
previous_return_values = []
with pytest.raises(NotImplementedError):
# In this version reducers do not work
@entrypoint(checkpointer=MemorySaver())
async def foo(inputs, previous=None) -> Any:
previous_return_values.append(previous)
yield "a"
yield "b"
@entrypoint(checkpointer=MemorySaver())
async def foo(inputs) -> Any:
yield "a"
yield "b"
config = {"configurable": {"thread_id": "1"}}
assert list(await foo.ainvoke({"a": "1"}, config)) == ["a", "b"]
assert previous_return_values == [None]
@NEEDS_CONTEXTVARS
async def test_named_tasks_functional() -> None:
class Foo:
async def foo(self, value: str) -> dict:
return value + "foo"
f = Foo()
# class method task
foo = task(f.foo, name="custom_foo")
other_foo = task(f.foo, name="other_foo")
# regular function task
@task(name="custom_bar")
async def bar(value: str) -> dict:
return value + "|bar"
async def baz(update: str, value: str) -> dict:
return value + f"|{update}"
# partial function task (unnamed)
baz_task = task(functools.partial(baz, "baz"))
# partial function task (named_)
custom_baz_task = task(functools.partial(baz, "custom_baz"), name="custom_baz")
class Qux:
def __call__(self, value: str) -> dict:
return value + "|qux"
qux_task = task(Qux(), name="qux")
@entrypoint()
async def workflow(inputs: dict) -> dict:
foo_result = await foo(inputs)
await other_foo(inputs)
bar_result = await bar(foo_result)
baz_result = await baz_task(bar_result)
custom_baz_result = await custom_baz_task(baz_result)
qux_result = await qux_task(custom_baz_result)
return qux_result
assert [c async for c in workflow.astream("", stream_mode="updates")] == [
{"custom_foo": "foo"},
{"other_foo": "foo"},
{"custom_bar": "foo|bar"},
{"baz": "foo|bar|baz"},
{"custom_baz": "foo|bar|baz|custom_baz"},
{"qux": "foo|bar|baz|custom_baz|qux"},
{"workflow": "foo|bar|baz|custom_baz|qux"},
]
@NEEDS_CONTEXTVARS
async def test_overriding_injectable_args_with_async_task() -> None:
"""Test overriding injectable args in tasks."""
from langgraph.store.memory import InMemoryStore
@task
async def foo(store: BaseStore, writer: StreamWriter, value: Any) -> None:
assert store is value
assert writer is value
@entrypoint(store=InMemoryStore())
async def main(inputs, store: BaseStore) -> str:
assert store is not None
await foo(store=None, writer=None, value=None)
await foo(store="hello", writer="hello", value="hello")
return "OK"
assert await main.ainvoke({}) == "OK"
async def test_tags_stream_mode_messages() -> None:
model = GenericFakeChatModel(messages=iter(["foo"]), tags=["meow"])
async def call_model(state, config):
return {"messages": await model.ainvoke(state["messages"], config)}
graph = (
StateGraph(MessagesState)
.add_node(call_model)
.add_edge(START, "call_model")
.compile()
)
assert [
c
async for c in graph.astream(
{
"messages": "hi",
},
stream_mode="messages",
)
] == [
(
_AnyIdAIMessageChunk(content="foo"),
{
"langgraph_step": 1,
"langgraph_node": "call_model",
"langgraph_triggers": ["start:call_model"],
"langgraph_path": ("__pregel_pull", "call_model"),
"langgraph_checkpoint_ns": AnyStr("call_model:"),
"checkpoint_ns": AnyStr("call_model:"),
"ls_provider": "genericfakechatmodel",
"ls_model_type": "chat",
"tags": ["meow"],
},
)
]
+199 -3
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Any
from typing import Any, Optional
import pytest
@@ -43,8 +43,15 @@ def test_runnable_callable_func_accepts():
expected_writer = {"with_writer": True, "awith_writer": True}
for name, runnable in runnables.items():
assert runnable.func_accepts["writer"] == expected_writer.get(name, False)
assert runnable.func_accepts["store"] == expected_store.get(name, False)
if expected_writer.get(name, False):
assert "writer" in runnable.func_accepts
else:
assert "writer" not in runnable.func_accepts
if expected_store.get(name, False):
assert "store" in runnable.func_accepts
else:
assert "store" not in runnable.func_accepts
async def test_runnable_callable_basic():
@@ -63,3 +70,192 @@ async def test_runnable_callable_basic():
# Test asynchronous ainvoke
result_async = await runnable_async.ainvoke("test")
assert result_async == "test"
def test_runnable_callable_injectable_arguments() -> None:
"""Test injectable arguments for RunnableCallable.
This test verifies that injectable arguments like BaseStore work correctly.
It tests:
- Optional store injection
- Required store injection
- Store injection via config
- Store injection override behavior
- Store value injection and validation
"""
# Test Optional[BaseStore] annotation.
def func_optional_store(inputs: Any, store: Optional[BaseStore]) -> str:
"""Test function that accepts an optional store parameter."""
assert store is None
return "success"
assert RunnableCallable(func_optional_store).invoke({"x": "1"}) == "success"
# Test BaseStore annotation
def func_required_store(inputs: Any, store: BaseStore) -> str:
"""Test function that requires a store parameter."""
assert store is None
return "success"
with pytest.raises(ValueError):
# Should fail b/c store is not Optional and config is not populated with store.
assert RunnableCallable(func_required_store).invoke({}) == "success"
# Manually provide store
assert RunnableCallable(func_required_store).invoke({}, store=None) == "success"
# Specify a value for store in the config
assert (
RunnableCallable(func_required_store).invoke(
{}, config={"configurable": {"__pregel_store": None}}
)
== "success"
)
# Specify a value for store in config, but override with None
assert (
RunnableCallable(func_optional_store).invoke(
{"x": "1"},
store=None,
config={"configurable": {"__pregel_store": "foobar"}},
)
== "success"
)
# Set of tests where we verify that 'foobar' is injected as the store value.
def func_required_store_v2(inputs: Any, store: BaseStore) -> str:
"""Test function that requires a store parameter and validates its value.
The store value is expected to be 'foobar' when injected.
"""
assert store == "foobar"
return "success"
assert (
RunnableCallable(func_required_store_v2).invoke(
{}, config={"configurable": {"__pregel_store": "foobar"}}
)
== "success"
)
assert RunnableCallable(func_required_store_v2).invoke(
# And manual override takes precedence.
{},
store="foobar",
config={"configurable": {"__pregel_store": "barbar"}},
)
async def test_runnable_callable_injectable_arguments_async() -> None:
"""Test injectable arguments for async RunnableCallable.
This test verifies that injectable arguments like BaseStore work correctly
in the async context. It tests:
- Optional store injection
- Required store injection
- Store injection via config
- Store injection override behavior
"""
# Test Optional[BaseStore] annotation.
def func_optional_store(inputs: Any, store: Optional[BaseStore]) -> str:
"""Test function that accepts an optional store parameter."""
assert store is None
return "success"
async def afunc_optional_store(inputs: Any, store: Optional[BaseStore]) -> str:
"""Async version of func_optional_store."""
assert store is None
return "success"
assert (
await RunnableCallable(
func=func_optional_store, afunc=afunc_optional_store
).ainvoke({"x": "1"})
== "success"
)
# Test BaseStore annotation
def func_required_store(inputs: Any, store: BaseStore) -> str:
"""Test function that requires a store parameter."""
assert store is None
return "success"
async def afunc_required_store(inputs: Any, store: BaseStore) -> str:
"""Async version of func_required_store."""
assert store is None
return "success"
with pytest.raises(ValueError):
# Should fail b/c store is not Optional and config is not populated with store.
assert (
await RunnableCallable(
func=func_required_store, afunc=afunc_required_store
).ainvoke({})
== "success"
)
# Manually provide store
assert (
await RunnableCallable(
func=func_required_store, afunc=afunc_required_store
).ainvoke({}, store=None)
== "success"
)
# Specify a value for store in the config
assert (
await RunnableCallable(
func=func_required_store, afunc=afunc_required_store
).ainvoke({}, config={"configurable": {"__pregel_store": None}})
== "success"
)
# Specify a value for store in config, but override with None
assert (
await RunnableCallable(
func=func_optional_store, afunc=afunc_optional_store
).ainvoke(
{"x": "1"},
store=None,
config={"configurable": {"__pregel_store": "foobar"}},
)
== "success"
)
# Set of tests where we verify that 'foobar' is injected as the store value.
def func_required_store_v2(inputs: Any, store: BaseStore) -> str:
"""Test function that requires a store parameter with specific value.
The store parameter is expected to be 'foobar' when injected.
"""
assert store == "foobar"
return "success"
async def afunc_required_store_v2(inputs: Any, store: BaseStore) -> str:
"""Async version of func_required_store_v2.
The store parameter is expected to be 'foobar' when injected.
"""
assert store == "foobar"
return "success"
assert (
await RunnableCallable(
func=func_required_store_v2, afunc=afunc_required_store_v2
).ainvoke({}, config={"configurable": {"__pregel_store": "foobar"}})
== "success"
)
assert (
await RunnableCallable(
func=func_required_store_v2, afunc=afunc_required_store_v2
).ainvoke(
# And manual override takes precedence.
{},
store="foobar",
config={"configurable": {"__pregel_store": "barbar"}},
)
== "success"
)

Some files were not shown because too many files have changed in this diff Show More