Compare commits

..
Author SHA1 Message Date
Arjun Natarajan fcc068b38d i think its this 2025-06-17 15:28:31 -04:00
Arjun Natarajan 50291be532 final changes 2025-06-17 15:09:34 -04:00
5336a52925 docs: added studio evals images (#5076)
* docs: added studio evals images

* Update docs/docs/cloud/how-tos/studio/run_evals.md

Co-authored-by: lc-arjun <arjun@langchain.dev>

* Update docs/docs/cloud/how-tos/studio/run_evals.md

Co-authored-by: lc-arjun <arjun@langchain.dev>

* Update docs/docs/cloud/how-tos/studio/run_evals.md

Co-authored-by: lc-arjun <arjun@langchain.dev>

* docs: updated studio evals

* Update docs/docs/cloud/how-tos/studio/run_evals.md

Co-authored-by: lc-arjun <arjun@langchain.dev>

* docs: removed images

---------

Co-authored-by: lc-arjun <arjun@langchain.dev>
2025-06-17 11:58:26 -07:00
Arjun Natarajan ed45b88591 docs: studio evals 2025-06-11 13:58:38 -04:00
langchain-infraandGitHub 29ffaa0e0b docs: fix config section (#5066) 2025-06-11 13:23:33 -04:00
Sydney RunkleandGitHub 45cd4e1928 oss: auto apply labels to contributor issues (#5067)
auto apply labels
2025-06-11 17:19:49 +00:00
langchain-infraandGitHub 480271f753 docs: add mount prefix environment variable (#5060) 2025-06-11 11:20:19 -04:00
infra 66fdf60e47 docs: add mount prefix environment variable 2025-06-11 11:18:16 -04:00
infra 0894daf3fc docs: add mount prefix environment variable 2025-06-11 11:17:45 -04:00
Lauren Hirata SinghandGitHub 850c55d630 Revert "fix assistants overview link" (#5059) 2025-06-11 11:02:17 -04:00
Lauren Hirata SinghandGitHub c0d65ff409 Revert "fix assistants overview link (#5058)"
This reverts commit be7b60a722.
2025-06-11 10:58:52 -04:00
Lauren Hirata SinghandGitHub be7b60a722 fix assistants overview link (#5058) 2025-06-11 10:58:07 -04:00
Eugene YurtsevandGitHub d467ec6556 Remove gitmcp badge (#5055)
* Remove gitmcp badge

* xt

* x
2025-06-11 10:55:05 -04:00
b8683ab67a docs: Update subgraphs.md (#5052)
* Update subgraphs.md

The state while defining the Subgraph is updated. Also an edge connecting START to the call_model node in the subgraph was created.

* Update docs/docs/concepts/subgraphs.md

* Update docs/docs/concepts/subgraphs.md

---------

Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
2025-06-11 13:49:51 +00:00
Nuno CamposandGitHub 470b9a4b97 Clean up PregelNode attributes (#5049) 2025-06-10 17:24:03 -07:00
Nuno Campos 516175780d Clean up things for Matt! 2025-06-10 16:14:15 -07:00
William FHandGitHub 571780f74c fix: header merging (#4926) 2025-06-10 14:44:34 -07:00
Emmanuel FerdmanandGitHub d719438307 fix: throw exception on multiple injections (#5033)
Throw exception on for multiple injections

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
2025-06-10 16:54:01 -04:00
Simon FrankandGitHub 85c809a651 docs: fixed a wrong import in persistence docs (#5045) 2025-06-10 20:53:50 +00:00
Nuno CamposandGitHub 0441fd156f Add docs for checkpoint encryption (#5047)
docs: list CipherProtocol in API
2025-06-10 16:52:45 -04:00
Nuno CamposandGitHub 37b5d3886c Add library overview to AGENTS.md (#5044) 2025-06-10 10:08:55 -07:00
Nuno Campos b95267a3cc Refine dependency map 2025-06-10 10:06:08 -07:00
Nuno CamposandGitHub 2e33c520a5 Support numpy array serialization in JsonPlusSerializer (#5035)
* Handle numpy Fortran arrays

* Lint

* Lint

* Lint
2025-06-10 01:17:28 +00:00
Nuno CamposandGitHub 67b1dc602e Update ormsgpack (#5034)
* Update ormsgpack

- Now supports bytearray/memoryview passthrough

* Lint
2025-06-10 00:30:58 +00:00
Naohiro YoshidaandGitHub 1519b90414 Centralized CheckpointTuple creation into a shared function for checkpoint_postgres (#4970) 2025-06-09 18:40:17 +00:00
YkohandGitHub 0035ab9825 docs: Replace unsupported models with structured output-supported models (#3982) 2025-06-09 14:17:05 -04:00
c42cd57a32 chore: Update variable naming in postgres store (#4096)
Co-authored-by: William FH <13333726+hinthornw@users.noreply.github.com>
2025-06-09 17:54:06 +00:00
acc56e094a docs: add query params for Store semantic search (#4828)
Co-authored-by: William FH <13333726+hinthornw@users.noreply.github.com>
2025-06-09 17:47:50 +00:00
Yassin NouhandGitHub 6b30d4fd8f docs: enhance PostgresSaver connection requirements explanation (#4953)
docs: enhance PostgresSaver connection requirements explanation - Add detailed explanation of why autocommit=True and row_factory=dict_row are required - Include example of incorrect usage and resulting errors - Addresses issue #4937 about incomplete setup documentation
2025-06-09 17:12:44 +00:00
fcc37cd06b docs: update tutorial/rag/langgraph_adaptive_rag.ipynb (#2006)
- add some explanations of ipynb code in markdown cell.

Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
2025-06-09 12:54:29 -04:00
William FHandGitHub c17ee1bf5a feat: [CLI] Add support for building deps with uv (#4995) 2025-06-09 08:57:29 -07:00
William FHandGitHub 88c603b00b fix: (sdk-js) Expand ToolMessage Type (#5015) 2025-06-09 08:22:35 -07:00
Sydney RunkleandGitHub c12f7cb2b9 github: support blank issues (help with v1 planning) (#4999)
blank issues
2025-06-09 13:52:14 +00:00
🤖Esteban Dalel RandGitHub 6d7d689578 docs: highlight changed lines in 3-add-memory.md (#4930) 2025-06-08 14:08:04 +00:00
LostInCode404andGitHub f1b7eca7fc docs: Update 1-build-basic-chatbot.md to add a section about END node (#4886)
Update `1-build-basic-chatbot.md` to add a section about `END` node
2025-06-08 13:51:17 +00:00
Michael LiandGitHub 93766a6df1 docs: fix assistants url at manage_assistants.md (#4993)
* docs: fix agent supervisor doc codes

* docs: fix assistants url at manage_assistants.md
2025-06-08 13:49:45 +00:00
Dionysis GlytsosandGitHub a9d4e0da29 docs: fix typos (#4992)
Fix typos
2025-06-08 13:46:41 +00:00
Sydney RunkleandGitHub 9105e60a34 graph: improve generics on StateGraph etc + move typing utils to private file (#4982) 2025-06-06 19:51:05 -04:00
Sydney RunkleandGitHub b735452153 deprecate input and output in favor of input_schema and output_schema (#4983) 2025-06-06 19:44:56 -04:00
Sydney Runkle 5920d8aa92 using StateT as default for InputT 2025-06-06 12:58:19 -04:00
533f5b3d6f docs: fix task description example in the agent supervisor tutorial (#4938)
* docs: fix agent supervisor doc codes

---------

Co-authored-by: vbarda <vadym@langchain.dev>
2025-06-06 13:41:41 +00:00
Asamu DavidandGitHub be5889a7df docs: add docs for image_distro cli option (#4974) 2025-06-05 23:11:47 +01:00
David Asamu 0bf268feca add docs for image_distro cli option 2025-06-05 17:23:05 +01:00
Sydney RunkleandGitHub 5e7566f4a3 lint: use pep 604 union syntax and pep 585 generic syntax (#4963)
* new union syntax

* fix test

* second round of conversions by injecting future annotations

* format + add top level makefile
2025-06-04 21:50:16 -04:00
Sydney RunkleandGitHub 494c8ef0d2 docs: remove references to StateGraph(dict) (#4964)
remove StateGraph(dict)
2025-06-04 21:29:19 -04:00
lc-arjunandGitHub 45e60ff9e1 fix: camel case to snake case conversion (#4966) 2025-06-04 17:31:12 -07:00
133 changed files with 10643 additions and 9548 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
name: "\U0001F41B Bug Report"
description: Report a bug in LangGraph. To report a security issue, please instead use the security option below. For questions, please use the GitHub Discussions.
labels: ["02 Bug Report"]
labels: [pending,bug]
body:
- type: markdown
attributes:
+1 -1
View File
@@ -1,4 +1,4 @@
blank_issues_enabled: false
blank_issues_enabled: true
version: 2.1
contact_links:
- name: 🤔 Question or Problem
+1 -1
View File
@@ -1,7 +1,7 @@
name: Documentation
description: Report an issue related to the LangGraph documentation.
title: "DOC: <Please write a comprehensive title after the 'DOC: ' prefix>"
labels: [03 - Documentation]
labels: [documentation]
body:
- type: textarea
+55
View File
@@ -0,0 +1,55 @@
# AGENTS Instructions
This repository is a monorepo. Each library lives in a subdirectory under `libs/`.
When you modify code in any library, run the following commands in that library's directory before creating a pull request:
- `make format` run code formatters
- `make lint` run the linter
- `make test` execute the test suite
To run a particular test file or to pass additional pytest options you can specify the `TEST` variable:
```
TEST=path/to/test.py make test
```
Other pytest arguments can also be supplied inside the `TEST` variable.
## Libraries
The repository contains several Python and JavaScript/TypeScript libraries.
Below is a high-level overview:
- **checkpoint** base interfaces for LangGraph checkpointers.
- **checkpoint-postgres** Postgres implementation of the checkpoint saver.
- **checkpoint-sqlite** SQLite implementation of the checkpoint saver.
- **cli** official command-line interface for LangGraph.
- **langgraph** core framework for building stateful, multi-actor agents.
- **prebuilt** high-level APIs for creating and running agents and tools.
- **sdk-js** JS/TS SDK for interacting with the LangGraph REST API.
- **sdk-py** Python SDK for the LangGraph Platform API.
### Dependency map
The diagram below lists downstream libraries for each production dependency as
declared in that library's `pyproject.toml` (or `package.json`).
```text
checkpoint
├── checkpoint-postgres
├── checkpoint-sqlite
├── prebuilt
└── langgraph
prebuilt
└── langgraph
sdk-py
├── langgraph
└── cli
sdk-js (standalone)
```
Changes to a library may impact all of its dependents shown above.
+58
View File
@@ -0,0 +1,58 @@
# Define the directories containing projects
LIBS_DIRS := $(wildcard libs/*)
# Default target
.PHONY: all
all: lint format lock test
# Install dependencies for all projects
.PHONY: install
install:
@echo "Creating virtual environment..."
@uv venv
@for dir in $(LIBS_DIRS); do \
if [ -f $$dir/pyproject.toml ]; then \
echo "Installing dependencies for $$dir"; \
uv pip install -e $$dir; \
fi; \
done
# Lint all projects
.PHONY: lint
lint:
@for dir in $(LIBS_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "Running lint in $$dir"; \
$(MAKE) -C $$dir lint; \
fi; \
done
# Format all projects
.PHONY: format
format:
@for dir in $(LIBS_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "Running format in $$dir"; \
$(MAKE) -C $$dir format; \
fi; \
done
# Lock all projects
.PHONY: lock
lock:
@for dir in $(LIBS_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "Running lock in $$dir"; \
(cd $$dir && uv lock); \
fi; \
done
# Test all projects
.PHONY: test
test:
@for dir in $(LIBS_DIRS); do \
if [ -f $$dir/Makefile ]; then \
echo "Running test in $$dir"; \
$(MAKE) -C $$dir test; \
fi; \
done
-1
View File
@@ -12,7 +12,6 @@
[![Downloads](https://static.pepy.tech/badge/langgraph/month)](https://pepy.tech/project/langgraph)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langgraph)](https://github.com/langchain-ai/langgraph/issues)
[![Docs](https://img.shields.io/badge/docs-latest-blue)](https://langchain-ai.github.io/langgraph/)
[![GitMCP](https://img.shields.io/endpoint?url=https://gitmcp.io/badge/langchain-ai/langgraph)](https://gitmcp.io/langchain-ai/langgraph)
Trusted by companies shaping the future of agents including Klarna, Replit, Elastic, and more LangGraph is a low-level orchestration framework for building, managing, and deploying long-running, stateful agents.
@@ -30,18 +30,16 @@ Before deploying, review the [conceptual guide for the Self-Hosted Control Plane
1. `LangGraphPlatform CRD`: A CRD for LangGraph Platform deployments. This contains the spec for managing an instance of a LangGraph platform deployment.
1. `operator`: This operator handles changes to your LangGraph Platform CRDs.
1. `host-backend`: This is the [control plane](../../concepts/langgraph_control_plane.md).
1. Two additional images will be used by the chart.
1. Two additional images will be used by the chart. Use the images that are specified in the latest release.
hostBackendImage:
repository: "docker.io/langchain/hosted-langserve-backend"
pullPolicy: IfNotPresent
tag: "0.9.80"
operatorImage:
repository: "docker.io/langchain/langgraph-operator"
pullPolicy: IfNotPresent
tag: "aa9dff4"
1. In your `langsmith_config.yaml` file, enable the `langgraphPlatform` option. Note that you must also have a valid ingress setup:
1. In your config file for langsmith (usually `langsmith_config.yaml`, enable the `langgraphPlatform` option. Note that you must also have a valid ingress setup:
config:
langgraphPlatform:
@@ -2,7 +2,7 @@
!!! info "Prerequisites"
- [Assistants Overview](../../concepts/assistants.md)
- [Assistants Overview](../../../concepts/assistants.md)
LangGraph Studio lets you view, edit, and update your assistants, and allows you to run your graph using these assistant configurations.
@@ -0,0 +1,57 @@
# Run experiments over a dataset
LangGraph Studio supports evaluations by allowing you to run your assistant over a pre-defined LangSmith dataset. This enables you to understand how your application performs over a variety of inputs, compare the results to reference outputs, and score the results using [evaluators](../../../agents/evals.md).
This guide shows you how to run an experiment end-to-end from Studio.
---
## Prerequisites
Before running an experiment, ensure you have the following:
1. **A LangSmith dataset**: Your dataset should contain the inputs you want to test and optionally, reference outputs for comparison.
- The schema for the inputs must match the required input schema for the assistant. For more information on schemas, see [here](../../../concepts/low_level.md#schema).
- For more on creating datasets, see [How to Manage Datasets](https://docs.smith.langchain.com/evaluation/how_to_guides/manage_datasets_in_application#set-up-your-dataset).
2. **(Optional) Evaluators**: You can attach evaluators (e.g., LLM-as-a-Judge, heuristics, or custom functions) to your dataset in LangSmith. These will run automatically after the graph has processed all inputs.
- To learn more, read about [Evaluation Concepts](https://docs.smith.langchain.com/evaluation/concepts#evaluators).
3. **A running application**: The experiment can be run against:
- An application deployed on [LangGraph Platform](../../quick_start.md).
- A locally running application started via the [langgraph-cli](../../../tutorials/langgraph-platform/local-server.md).
---
## Step-by-step guide
### 1. Launch the experiment
Click the **Run experiment** button in the top right corner of the Studio page.
### 2. Select your dataset
In the modal that appears, select the dataset (or a specific dataset split) to use for the experiment and click **Start**.
### 3. Monitor the progress
All of the inputs in the dataset will now be run against the active assistant. Monitor the experiment's progress via the badge in the top right corner.
You can continue to work in Studio while the experiment runs in the background. Click the arrow icon button at any time to navigate to LangSmith and view the detailed experiment results.
---
## Troubleshooting
### "Run experiment" button is disabled
If the "Run experiment" button is disabled, check the following:
- **Deployed application**: If your application is deployed on LangGraph Platform, you may need to create a new revision to enable this feature.
- **Local development server**: If you are running your application locally, make sure you have upgraded to the latest version of the `langgraph-cli` (`pip install -U langgraph-cli`). Additionally, ensure you have tracing enabled by setting the `LANGSMITH_API_KEY` in your project's `.env` file.
### Evaluator results are missing
When you run an experiment, any attached evaluators are scheduled for execution in a queue. If you don't see results immediately, it likely means they are still pending.
@@ -3818,6 +3818,14 @@
"title": "Filter",
"description": "Optional dictionary of key-value pairs to filter results."
},
"query": {
"type": [
"string",
"null"
],
"title": "Query",
"description": "Query string for semantic/vector search."
},
"limit": {
"type": "integer",
"default": 10,
+15
View File
@@ -43,6 +43,7 @@ The LangGraph CLI requires a JSON configuration file that follows this [schema](
| <span style="white-space: nowrap;">`graphs`</span> | **Required**. Mapping from graph ID to path where the compiled graph or a function that makes a graph is defined. Example: <ul><li>`./your_package/your_file.py:variable`, where `variable` is an instance of `langgraph.graph.state.CompiledStateGraph`</li><li>`./your_package/your_file.py:make_graph`, where `make_graph` is a function that takes a config dictionary (`langchain_core.runnables.RunnableConfig`) and returns an instance of `langgraph.graph.state.StateGraph` or `langgraph.graph.state.CompiledStateGraph`. See [how to rebuild a graph at runtime](../../cloud/deployment/graph_rebuild.md) for more details.</li></ul> |
| <span style="white-space: nowrap;">`auth`</span> | _(Added in v0.0.11)_ Auth configuration containing the path to your authentication handler. Example: `./your_package/auth.py:auth`, where `auth` is an instance of `langgraph_sdk.Auth`. See [authentication guide](../../concepts/auth.md) for details. |
| <span style="white-space: nowrap;">`base_image`</span> | Optional. Base image to use for the LangGraph API server. Defaults to `langchain/langgraph-api` or `langchain/langgraphjs-api`. Use this to pin your builds to a particular version of the langgraph API, such as `"langchain/langgraph-server:0.2"`. See https://hub.docker.com/r/langchain/langgraph-server/tags for more details. (added in `langgraph-cli==0.2.8`) |
| <span style="white-space: nowrap;">`image_distro`</span> | Optional. Linux distribution for the base image. Must be either `"debian"` or `"wolfi"`. If omitted, defaults to `"debian"`. Available in `langgraph-cli>=0.2.11`.|
| <span style="white-space: nowrap;">`env`</span> | Path to `.env` file or a mapping from environment variable to its value. |
| <span style="white-space: nowrap;">`store`</span> | Configuration for adding semantic search and/or time-to-live (TTL) to the BaseStore. Contains the following fields: <ul><li>`index` (optional): Configuration for semantic search indexing with fields `embed`, `dims`, and optional `fields`.</li><li>`ttl` (optional): Configuration for item expiration. An object with optional fields: `refresh_on_read` (boolean, defaults to `true`), `default_ttl` (float, lifespan in **minutes**, defaults to no expiration), and `sweep_interval_minutes` (integer, how often to check for expired items, defaults to no sweeping).</li></ul> |
| <span style="white-space: nowrap;">`ui`</span> | Optional. Named definitions of UI components emitted by the agent, each pointing to a JS/TS file. (added in `langgraph-cli==0.1.84`) |
@@ -79,6 +80,20 @@ The LangGraph CLI requires a JSON configuration file that follows this [schema](
}
```
#### Using Wolfi Base Images
You can specify the Linux distribution for your base image using the `image_distro` field. Valid options are `debian` or `wolfi`. Wolfi is the recommended option as it provides smaller and more secure images. This is available in `langgraph-cli>=0.2.11`.
```json
{
"dependencies": ["."],
"graphs": {
"chat": "./chat/graph.py:graph"
},
"image_distro": "wolfi"
}
```
#### Adding semantic search to the store
All deployments come with a DB-backed BaseStore. Adding an "index" configuration to your `langgraph.json` will enable [semantic search](../deployment/semantic_search.md) within the BaseStore of your deployment.
+9
View File
@@ -123,3 +123,12 @@ Defaults to `''`.
Set `REDIS_CLUSTER` to `True` to enable Redis Cluster mode. When enabled, the system will connect to Redis using cluster mode. This is useful when connecting to a Redis Cluster deployment.
Defaults to `False`.
## `MOUNT_PREFIX`
!!! info "Only Allowed in Self-Hosted Deployments"
The `MOUNT_PREFIX` environment variable is only allowed in Self-Hosted Deployment models, LangGraph Platform SaaS will not allow this environment variable.
Set `MOUNT_PREFIX` to serve the LangGraph Server under a specific path prefix. This is useful for deployments where the server is behind a reverse proxy or load balancer that requires a specific path prefix.
For example, if the server is to be served under `https://example.com/langgraph`, set `MOUNT_PREFIX` to `/langgraph`.
+1 -1
View File
@@ -198,7 +198,7 @@ async def add_owner(
You can register handlers for specific resources and actions by chaining the resource and action names together with the [`@auth.on`](../cloud/reference/sdk/python_sdk_ref.md#langgraph_sdk.auth.Auth.on) decorator.
When a request is made, the most specific handler that matches that resource and action is called. Below is an example of how to register handlers for specific resources and actions. For the following setup:
1. Authenticated users are able to create threads, read thread, create runs on threads
1. Authenticated users are able to create threads, read threads, and create runs on threads
2. Only users with the "assistants:create" permission are allowed to create new assistants
3. All other endpoints (e.g., e.g., delete assistant, crons, store) are disabled for all users.
+2 -1
View File
@@ -24,6 +24,7 @@ Key features of LangGraph Studio:
- [Manage assistants](../cloud/how-tos/studio/manage_assistants.md)
- [Manage threads](../cloud/how-tos/threads_studio.md)
- [Iterate on prompts](../cloud/how-tos/iterate_graph_studio.md)
- [Run experiments over a dataset](../cloud/how-tos/studio/run_evals.md)
- Manage [long term memory](memory.md)
- Debug agent state via [time travel](time-travel.md)
@@ -41,4 +42,4 @@ Chat mode is a simpler UI for iterating on and testing chat-specific agents. It
## Learn more
- See this guide on how to [get started](../cloud/how-tos/studio/quick_start.md) with LangGraph Studio.
- See this guide on how to [get started](../cloud/how-tos/studio/quick_start.md) with LangGraph Studio.
+11 -5
View File
@@ -89,7 +89,7 @@ def node_3(state: PrivateState) -> OutputState:
# Read from PrivateState, write to OutputState
return {"graph_output": state["bar"] + " Lance"}
builder = StateGraph(OverallState,input=InputState,output=OutputState)
builder = StateGraph(OverallState,input_schema=InputState,output_schema=OutputState)
builder.add_node("node_1", node_1)
builder.add_node("node_2", node_2)
builder.add_node("node_3", node_3)
@@ -107,7 +107,7 @@ There are two subtle and important points to note here:
1. We pass `state: InputState` as the input schema to `node_1`. But, we write out to `foo`, a channel in `OverallState`. How can we write out to a state channel that is not included in the input schema? This is because a node _can write to any state channel in the graph state._ The graph state is the union of the state channels defined at initialization, which includes `OverallState` and the filters `InputState` and `OutputState`.
2. We initialize the graph with `StateGraph(OverallState,input=InputState,output=OutputState)`. So, how can we write to `PrivateState` in `node_2`? How does the graph gain access to this schema if it was not passed in the `StateGraph` initialization? We can do this because _nodes can also declare additional state channels_ as long as the state schema definition exists. In this case, the `PrivateState` schema is defined, so we can add `bar` as a new state channel in the graph and write to it.
2. We initialize the graph with `StateGraph(OverallState,input_schema=InputState,output_schema=OutputState)`. So, how can we write to `PrivateState` in `node_2`? How does the graph gain access to this schema if it was not passed in the `StateGraph` initialization? We can do this because _nodes can also declare additional state channels_ as long as the state schema definition exists. In this case, the `PrivateState` schema is defined, so we can add `bar` as a new state channel in the graph and write to it.
### Reducers
@@ -197,19 +197,25 @@ In LangGraph, nodes are typically python functions (sync or async) where the **f
Similar to `NetworkX`, you add these nodes to a graph using the [add_node][langgraph.graph.StateGraph.add_node] method:
```python
from typing_extensions import TypedDict
from langchain_core.runnables import RunnableConfig
from langgraph.graph import StateGraph
builder = StateGraph(dict)
class State(TypedDict):
input: str
results: str
builder = StateGraph(State)
def my_node(state: dict, config: RunnableConfig):
def my_node(state: State, config: RunnableConfig):
print("In node: ", config["configurable"]["user_id"])
return {"results": f"Hello, {state['input']}!"}
# The second argument is optional
def my_other_node(state: dict):
def my_other_node(state: State):
return state
+26 -1
View File
@@ -470,9 +470,34 @@ If the checkpointer is used with asynchronous graph execution (i.e. executing th
### Serializer
When checkpointers save the graph state, they need to serialize the channel values in the state. This is done using serializer objects.
When checkpointers save the graph state, they need to serialize the channel values in the state. This is done using serializer objects.
`langgraph_checkpoint` defines [protocol][langgraph.checkpoint.serde.base.SerializerProtocol] for implementing serializers provides a default implementation ([JsonPlusSerializer][langgraph.checkpoint.serde.jsonplus.JsonPlusSerializer]) that handles a wide variety of types, including LangChain and LangGraph primitives, datetimes, enums and more.
#### Encryption
Checkpointers can optionally encrypt all persisted state. To enable this, pass an instance of [`EncryptedSerializer`][langgraph.checkpoint.serde.encrypted.EncryptedSerializer] to the `serde` argument of any `BaseCheckpointSaver` implementation. The easiest way to create an encrypted serializer is via [`from_pycryptodome_aes`][langgraph.checkpoint.serde.encrypted.EncryptedSerializer.from_pycryptodome_aes], which reads the AES key from the `LANGGRAPH_AES_KEY` environment variable (or accepts a `key` argument):
```python
import sqlite3
from langgraph.checkpoint.serde.encrypted import EncryptedSerializer
from langgraph.checkpoint.sqlite import SqliteSaver
serde = EncryptedSerializer.from_pycryptodome_aes() # reads LANGGRAPH_AES_KEY
checkpointer = SqliteSaver(sqlite3.connect("checkpoint.db"), serde=serde)
```
```python
from langgraph.checkpoint.serde.encrypted import EncryptedSerializer
from langgraph.checkpoint.postgres import PostgresSaver
serde = EncryptedSerializer.from_pycryptodome_aes()
checkpointer = PostgresSaver.from_conn_string("postgresql://...", serde=serde)
checkpointer.setup()
```
When running on LangGraph Platform, encryption is automatically enabled whenever `LANGGRAPH_AES_KEY` is present, so you only need to provide the environment variable. Other encryption schemes can be used by implementing [`CipherProtocol`][langgraph.checkpoint.serde.base.CipherProtocol] and supplying it to `EncryptedSerializer`.
## Capabilities
### Human-in-the-loop
+1 -1
View File
@@ -94,7 +94,7 @@ def answer_node(state: InputState):
return {"answer": "bye", "question": state["question"]}
# Build the graph with explicit schemas
builder = StateGraph(OverallState, input=InputState, output=OutputState)
builder = StateGraph(OverallState, input_schema=InputState, output_schema=OutputState)
builder.add_node(answer_node)
builder.add_edge(START, "answer_node")
builder.add_edge("answer_node", END)
+3 -2
View File
@@ -59,8 +59,9 @@ The main question when adding subgraphs is how the parent graph and subgraph com
response = model.invoke(state["subgraph_messages"])
return {"subgraph_messages": response}
subgraph_builder = StateGraph(State)
subgraph_builder.add_node(call_model)
subgraph_builder = StateGraph(SubgraphMessagesState)
subgraph_builder.add_node("call_model_from_subgraph", call_model)
subgraph_builder.add_edge(START, "call_model_from_subgraph")
...
# highlight-next-line
subgraph = subgraph_builder.compile()
+3 -3
View File
@@ -439,7 +439,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "6ec0eb77-874e-443e-8c73-93125b515106",
"metadata": {},
"outputs": [
@@ -478,7 +478,7 @@
"\n",
"\n",
"# Build the graph with input and output schemas specified\n",
"builder = StateGraph(OverallState, input=InputState, output=OutputState)\n",
"builder = StateGraph(OverallState, input_schema=InputState, output_schema=OutputState)\n",
"builder.add_node(answer_node) # Add the answer node\n",
"builder.add_edge(START, \"answer_node\") # Define the starting edge\n",
"builder.add_edge(\"answer_node\", END) # Define the ending edge\n",
@@ -3430,7 +3430,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
"version": "3.9.6"
}
},
"nbformat": 4,
+2 -2
View File
@@ -1107,10 +1107,10 @@
"source": [
"### Use in production\n",
"\n",
"In production, you would want to use a checkpointer backed by a database:\n",
"In production, you would want to use a store backed by a database:\n",
"\n",
"```python\n",
"from langgraph.checkpoint.postgres import PostgresSaver\n",
"from langgraph.store.postgres import PostgresStore\n",
"\n",
"DB_URI = \"postgresql://postgres:postgres@localhost:5442/postgres?sslmode=disable\"\n",
"# highlight-next-line\n",
+7 -1
View File
@@ -12,12 +12,18 @@
options:
members:
- SerializerProtocol
- CipherProtocol
::: langgraph.checkpoint.serde.jsonplus
options:
members:
- JsonPlusSerializer
::: langgraph.checkpoint.serde.encrypted
options:
members:
- EncryptedSerializer
::: langgraph.checkpoint.memory
::: langgraph.checkpoint.sqlite
@@ -32,4 +38,4 @@
::: langgraph.checkpoint.postgres.aio
options:
members:
- AsyncPostgresSaver
- AsyncPostgresSaver
+1 -1
View File
@@ -22,7 +22,7 @@ Welcome to the LangGraph reference docs! These pages detail the core interfaces
## LangGraph
The core APIs for the LangGraph opens source library.
The core APIs for the LangGraph open source library.
- [Graphs](graphs.md): Main graph abstraction and usage.
- [Functional API](func.md): Functional programming interface for graphs.
@@ -580,9 +580,7 @@
" ]\n",
")\n",
"\n",
"evaluator = prompt | ChatOpenAI(model=\"gpt-4-turbo-preview\").with_structured_output(\n",
" RedTeamingResult, method=\"function_calling\"\n",
")\n",
"evaluator = prompt | ChatOpenAI(model=\"gpt-4o\").with_structured_output(RedTeamingResult)\n",
"\n",
"\n",
"def did_resist(run, example):\n",
@@ -32,7 +32,7 @@ from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
@@ -100,7 +100,16 @@ Add an `entry` point to tell the graph **where to start its work** each time it
graph_builder.add_edge(START, "chatbot")
```
## 5. Compile the graph
## 5. Add an `exit` point
Add an `exit` point to indicate **where the graph should finish execution**. This is helpful for more complex flows, but even in a simple graph like this, adding an end node improves clarity.
```python
graph_builder.add_edge("chatbot", END)
```
This tells the graph to terminate after running the chatbot node.
## 6. Compile the graph
Before running the graph, we'll need to compile it. We can do so by calling `compile()`
on the graph builder. This creates a `CompiledGraph` we can invoke on our state.
@@ -109,7 +118,7 @@ on the graph builder. This creates a `CompiledGraph` we can invoke on our state.
graph = graph_builder.compile()
```
## 6. Visualize the graph (optional)
## 7. Visualize the graph (optional)
You can visualize the graph using the `get_graph` method and one of the "draw" methods, like `draw_ascii` or `draw_png`. The `draw` methods each require additional dependencies.
@@ -126,7 +135,7 @@ except Exception:
![basic chatbot diagram](basic-chatbot.png)
## 7. Run the chatbot
## 8. Run the chatbot
Now run the chatbot!
@@ -171,7 +180,7 @@ from typing import Annotated
from langchain.chat_models import init_chat_model
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
@@ -194,6 +203,7 @@ def chatbot(state: State):
# the node is used.
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()
```
@@ -164,7 +164,7 @@ llm = init_chat_model("anthropic:claude-3-5-sonnet-latest")
```
-->
```python
```python hl_lines="36 37"
from typing import Annotated
from langchain.chat_models import init_chat_model
@@ -206,4 +206,4 @@ graph = graph_builder.compile(checkpointer=memory)
## Next steps
In the next tutorial, you will [add human-in-the-loop to the chatbot](./4-human-in-the-loop.md) to handle situations where it may need guidance or verification before proceeding.
In the next tutorial, you will [add human-in-the-loop to the chatbot](./4-human-in-the-loop.md) to handle situations where it may need guidance or verification before proceeding.
@@ -471,7 +471,7 @@
"\n",
"_get_pass(\"TAVILY_API_KEY\")\n",
"\n",
"calculate = get_math_tool(ChatOpenAI(model=\"gpt-4-turbo-preview\"))\n",
"calculate = get_math_tool(ChatOpenAI(model=\"gpt-4o\"))\n",
"search = TavilySearchResults(\n",
" max_results=1,\n",
" description='tavily_search_results_json(query=\"the search query\") - a search engine.',\n",
@@ -540,11 +540,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m System Message \u001b[0m================================\n",
"================================\u001B[1m System Message \u001B[0m================================\n",
"\n",
"Given a user query, create a plan to solve it with the utmost parallelizability. Each plan should comprise an action from the following \u001b[33;1m\u001b[1;3m{num_tools}\u001b[0m types:\n",
"\u001b[33;1m\u001b[1;3m{tool_descriptions}\u001b[0m\n",
"\u001b[33;1m\u001b[1;3m{num_tools}\u001b[0m. join(): Collects and combines results from prior actions.\n",
"Given a user query, create a plan to solve it with the utmost parallelizability. Each plan should comprise an action from the following \u001B[33;1m\u001B[1;3m{num_tools}\u001B[0m types:\n",
"\u001B[33;1m\u001B[1;3m{tool_descriptions}\u001B[0m\n",
"\u001B[33;1m\u001B[1;3m{num_tools}\u001B[0m. join(): Collects and combines results from prior actions.\n",
"\n",
" - An LLM agent is called upon invoking join() to either finalize the user query or wait until the plans are executed.\n",
" - join should always be the last action in the plan, and will be called in two scenarios:\n",
@@ -561,11 +561,11 @@
" - Only use the provided action types. If a query cannot be addressed using these, invoke the join action for the next steps.\n",
" - Never introduce new actions other than the ones provided.\n",
"\n",
"=============================\u001b[1m Messages Placeholder \u001b[0m=============================\n",
"=============================\u001B[1m Messages Placeholder \u001B[0m=============================\n",
"\n",
"\u001b[33;1m\u001b[1;3m{messages}\u001b[0m\n",
"\u001B[33;1m\u001B[1;3m{messages}\u001B[0m\n",
"\n",
"================================\u001b[1m System Message \u001b[0m================================\n",
"================================\u001B[1m System Message \u001B[0m================================\n",
"\n",
"Remember, ONLY respond with the task list in the correct format! E.g.:\n",
"idx. tool(arg_name=args)\n",
@@ -1030,7 +1030,7 @@
"joiner_prompt = hub.pull(\"wfh/llm-compiler-joiner\").partial(\n",
" examples=\"\"\n",
") # You can optionally add examples\n",
"llm = ChatOpenAI(model=\"gpt-4-turbo-preview\")\n",
"llm = ChatOpenAI(model=\"gpt-4o\")\n",
"\n",
"runnable = joiner_prompt | llm.with_structured_output(\n",
" JoinOutputs, method=\"function_calling\"\n",
File diff suppressed because one or more lines are too long
@@ -135,7 +135,6 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"from langgraph.prebuilt import create_react_agent\n",
@@ -90,7 +90,11 @@
"id": "9ac1c2cd-81fb-40eb-8ba1-e9197800cba6",
"metadata": {},
"source": [
"## Create Index"
"## Create Index\n",
"\n",
"Set up a vector database using OpenAI Embeddings and the Chroma vector database. \n",
"Input URLs of blog posts related to agents, prompt engineering, and large language models (LLMs). \n",
"Generate vector indices for use in Retrieval-Augmented Generation (RAG)."
]
},
{
@@ -159,6 +163,21 @@
"</div>"
]
},
{
"cell_type": "markdown",
"id": "6cdd5ac0-fa18-4ee9-8051-062a0c56268f",
"metadata": {},
"source": [
"### Router for Query Analysis\n",
"\n",
"Lets start with Routing. First, assign the query analysis to the LLM.\n",
"\n",
"Create a RouteQuery data model and specify it in a structured format for the LLM. The decision for routing should be embedded in the prompt. You need to clearly define which parts of the document should be directed to RAG based on the topic.\n",
"\n",
"While you could automate this process by having the LLM summarize the RAG documents again, its more cost-effective to manually manage this when dealing with large documents, as automation could become expensive.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
@@ -219,6 +238,18 @@
"print(question_router.invoke({\"question\": \"What are the types of agent memory?\"}))"
]
},
{
"cell_type": "markdown",
"id": "cb248c94-0b0c-4d86-8565-32aa8d7424e4",
"metadata": {},
"source": [
"### Retrieval Grader\n",
"\n",
"After performing retrieval, evaluate the results. Although you initially decided to use RAG based on the query, the retrieved documents might not be satisfactory. Assess whether the retrieved documents are sufficiently relevant to the query.\n",
"\n",
"For this, rely on the LLM to evaluate the relevance, providing a binary yes or no decision."
]
},
{
"cell_type": "code",
"execution_count": 5,
@@ -309,6 +340,17 @@
"print(generation)"
]
},
{
"cell_type": "markdown",
"id": "cb0ab54a-4a4f-45fa-b1c5-cea1bf4c59d5",
"metadata": {},
"source": [
"### Hallucination Grader\n",
"\n",
"Verify if the LLM produced any hallucinations by comparing its output to the retrieved facts. \n",
"Provide the LLMs evaluation in a binary yes or no format.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
@@ -357,6 +399,16 @@
"hallucination_grader.invoke({\"documents\": docs, \"generation\": generation})"
]
},
{
"cell_type": "markdown",
"id": "4f58502a-c25f-4d80-a402-5583b0cd3e41",
"metadata": {},
"source": [
"### Answer Grader\n",
"\n",
"Evaluate the answer finally."
]
},
{
"cell_type": "code",
"execution_count": 8,
@@ -405,6 +457,18 @@
"answer_grader.invoke({\"question\": question, \"generation\": generation})"
]
},
{
"cell_type": "markdown",
"id": "af77946c-2646-4039-86b0-e2fde1ab7459",
"metadata": {},
"source": [
"### Question Rewriting\n",
"\n",
"The original question from user was directly used in RAG. \n",
"However, the users question might not be in a form suitable for RAG. \n",
"To improve retrieval, rephrase the question to ensure it aligns better with vector similarity search."
]
},
{
"cell_type": "code",
"execution_count": 9,
@@ -450,7 +514,9 @@
"id": "d07c0b31-b919-4498-869f-9673125c2473",
"metadata": {},
"source": [
"## Web Search Tool"
"## Web Search Tool\n",
"\n",
"Use Tavily Search tool to get information from the web."
]
},
{
+1 -1
View File
@@ -185,7 +185,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
"\n",
"# Prompt\n",
+1 -1
View File
@@ -1758,7 +1758,7 @@
"id": "4eb67198-c84f-458b-8baf-783d7246dddc",
"metadata": {},
"source": [
"Let's let the agent try again. Call `stream` with `None` to just use the inputs loaded from the memory. We will skip our human review for the next few attempats\n",
"Let's let the agent try again. Call `stream` with `None` to just use the inputs loaded from the memory. We will skip our human review for the next few attempts\n",
"to see if it can correct itself."
]
},
+1 -19
View File
@@ -179,6 +179,7 @@ nav:
- cloud/how-tos/studio/manage_assistants.md
- cloud/how-tos/threads_studio.md
- cloud/how-tos/iterate_graph_studio.md
- cloud/how-tos/studio/run_evals.md
- cloud/how-tos/clone_traces_studio.md
- cloud/how-tos/datasets_studio.md
- LangGraph SDK: concepts/sdk.md
@@ -381,25 +382,6 @@ extra:
link: https://github.com/langchain-ai/langgraph
- icon: fontawesome/brands/twitter
link: https://twitter.com/LangChainAI
analytics:
provider: google
property: G-G8X6ELZYE0
feedback:
title: Was this page helpful?
ratings:
- icon: material/emoticon-happy-outline
name: This page was helpful
data: 1
note: >-
Thanks for your feedback!
- icon: material/emoticon-sad-outline
name: This page could be improved
data: 0
note: >-
Thanks for your feedback! Please help us improve this page by adding to the discussion below.
shared_analytics:
provider: google
property: G-47WX3HKKY2
validation:
# https://www.mkdocs.org/user-guide/configuration/
# We are still raising for omitted files because they determine the breadcrumbs for pages.
Generated
+3060 -3059
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -184,7 +184,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_router = llm.with_structured_output(RouteQuery)\n",
"\n",
"# Prompt\n",
@@ -235,7 +235,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
"\n",
"# Prompt\n",
@@ -328,7 +328,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeHallucinations)\n",
"\n",
"# Prompt\n",
@@ -376,7 +376,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeAnswer)\n",
"\n",
"# Prompt\n",
+4 -4
View File
@@ -200,11 +200,11 @@
"output_type": "stream",
"text": [
"********************Prompt[rlm/rag-prompt]********************\n",
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"================================\u001B[1m Human Message \u001B[0m=================================\n",
"\n",
"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\n",
"Question: \u001b[33;1m\u001b[1;3m{question}\u001b[0m \n",
"Context: \u001b[33;1m\u001b[1;3m{context}\u001b[0m \n",
"Question: \u001B[33;1m\u001B[1;3m{question}\u001B[0m \n",
"Context: \u001B[33;1m\u001B[1;3m{context}\u001B[0m \n",
"Answer:\n"
]
}
@@ -244,7 +244,7 @@
" binary_score: str = Field(description=\"Relevance score 'yes' or 'no'\")\n",
"\n",
" # LLM\n",
" model = ChatOpenAI(temperature=0, model=\"gpt-4-0125-preview\", streaming=True)\n",
" model = ChatOpenAI(temperature=0, model=\"gpt-4o\", streaming=True)\n",
"\n",
" # LLM with tool and validation\n",
" llm_with_tool = model.with_structured_output(grade)\n",
+1 -1
View File
@@ -171,7 +171,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
"\n",
"# Prompt\n",
+3 -3
View File
@@ -191,7 +191,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
"\n",
"# Prompt\n",
@@ -284,7 +284,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeHallucinations)\n",
"\n",
"# Prompt\n",
@@ -332,7 +332,7 @@
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeAnswer)\n",
"\n",
"# Prompt\n",
@@ -33,7 +33,9 @@
"id": "a384cc48-0425-4e8f-aafc-cfb8e56025c9",
"metadata": {},
"outputs": [],
"source": ["%pip install -qU langchain-pinecone langchain-openai langchainhub langgraph"]
"source": [
"%pip install -qU langchain-pinecone langchain-openai langchainhub langgraph"
]
},
{
"cell_type": "markdown",
@@ -51,7 +53,9 @@
"id": "ccc3dae5-1df6-48ca-af8a-50f0e6128876",
"metadata": {},
"outputs": [],
"source": ["import os\n\nos.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\nos.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\nos.environ[\"LANGCHAIN_API_KEY\"] = \"<your-api-key>\""]
"source": [
"import os\n\nos.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\nos.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\nos.environ[\"LANGCHAIN_API_KEY\"] = \"<your-api-key>\""
]
},
{
"cell_type": "code",
@@ -59,7 +63,9 @@
"id": "88637820",
"metadata": {},
"outputs": [],
"source": ["import os\n\nos.environ[\"LANGCHAIN_PROJECT\"] = \"pinecone-devconnect\""]
"source": [
"import os\n\nos.environ[\"LANGCHAIN_PROJECT\"] = \"pinecone-devconnect\""
]
},
{
"cell_type": "markdown",
@@ -77,7 +83,9 @@
"id": "565a6d44-2c9f-4fff-b1ec-eea05df9350d",
"metadata": {},
"outputs": [],
"source": ["from langchain_openai import OpenAIEmbeddings\nfrom langchain_pinecone import PineconeVectorStore\n\n# use pinecone movies database\n\n# Add to vectorDB\nvectorstore = PineconeVectorStore(\n embedding=OpenAIEmbeddings(),\n index_name=\"sample-movies\",\n text_key=\"summary\",\n)\nretriever = vectorstore.as_retriever()"]
"source": [
"from langchain_openai import OpenAIEmbeddings\nfrom langchain_pinecone import PineconeVectorStore\n\n# use pinecone movies database\n\n# Add to vectorDB\nvectorstore = PineconeVectorStore(\n embedding=OpenAIEmbeddings(),\n index_name=\"sample-movies\",\n text_key=\"summary\",\n)\nretriever = vectorstore.as_retriever()"
]
},
{
"cell_type": "code",
@@ -104,7 +112,9 @@
]
}
],
"source": ["docs = retriever.invoke(\"James Cameron\")\nfor doc in docs:\n print(\"# \" + doc.metadata[\"title\"])\n print(doc.page_content)\n print()"]
"source": [
"docs = retriever.invoke(\"James Cameron\")\nfor doc in docs:\n print(\"# \" + doc.metadata[\"title\"])\n print(doc.page_content)\n print()"
]
},
{
"cell_type": "markdown",
@@ -120,7 +130,32 @@
"id": "1fafad21-60cc-483e-92a3-6a7edb1838e3",
"metadata": {},
"outputs": [],
"source": ["### Retrieval Grader\n\nfrom langchain import hub\nfrom langchain_core.pydantic_v1 import BaseModel, Field\nfrom langchain_openai import ChatOpenAI\n\n\n# Data model\nclass GradeDocuments(BaseModel):\n \"\"\"Binary score for relevance check on retrieved documents.\"\"\"\n\n binary_score: str = Field(\n description=\"Documents are relevant to the question, 'yes' or 'no'\"\n )\n\n\n# https://smith.langchain.com/hub/efriis/self-rag-retrieval-grader\ngrade_prompt = hub.pull(\"efriis/self-rag-retrieval-grader\")\n\n# LLM with function call\nllm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\nstructured_llm_grader = llm.with_structured_output(GradeDocuments)\n\nretrieval_grader = grade_prompt | structured_llm_grader"]
"source": [
"### Retrieval Grader\n",
"\n",
"from langchain import hub\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"\n",
"# Data model\n",
"class GradeDocuments(BaseModel):\n",
" \"\"\"Binary score for relevance check on retrieved documents.\"\"\"\n",
"\n",
" binary_score: str = Field(\n",
" description=\"Documents are relevant to the question, 'yes' or 'no'\"\n",
" )\n",
"\n",
"\n",
"# https://smith.langchain.com/hub/efriis/self-rag-retrieval-grader\n",
"grade_prompt = hub.pull(\"efriis/self-rag-retrieval-grader\")\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
"\n",
"retrieval_grader = grade_prompt | structured_llm_grader"
]
},
{
"cell_type": "code",
@@ -137,7 +172,9 @@
]
}
],
"source": ["# Test the retrieval grader\nquestion = \"movies starring jason momoa\"\ndocs = retriever.invoke(question)\ndoc_txt = docs[0].page_content\nprint(doc_txt)\nprint(retrieval_grader.invoke({\"question\": question, \"document\": doc_txt}))"]
"source": [
"# Test the retrieval grader\nquestion = \"movies starring jason momoa\"\ndocs = retriever.invoke(question)\ndoc_txt = docs[0].page_content\nprint(doc_txt)\nprint(retrieval_grader.invoke({\"question\": question, \"document\": doc_txt}))"
]
},
{
"cell_type": "markdown",
@@ -163,7 +200,9 @@
]
}
],
"source": ["### Generate\n\nfrom langchain import hub\nfrom langchain_core.output_parsers import StrOutputParser\n\n# Prompt\nprompt = hub.pull(\"rlm/rag-prompt\")\n\n# LLM\nllm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n\n# Chain\nrag_chain = prompt | llm | StrOutputParser()\n\n# Run\ngeneration = rag_chain.invoke({\"context\": docs, \"question\": question})\nprint(generation)"]
"source": [
"### Generate\n\nfrom langchain import hub\nfrom langchain_core.output_parsers import StrOutputParser\n\n# Prompt\nprompt = hub.pull(\"rlm/rag-prompt\")\n\n# LLM\nllm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n\n# Chain\nrag_chain = prompt | llm | StrOutputParser()\n\n# Run\ngeneration = rag_chain.invoke({\"context\": docs, \"question\": question})\nprint(generation)"
]
},
{
"cell_type": "code",
@@ -189,7 +228,30 @@
"output_type": "execute_result"
}
],
"source": ["### Hallucination Grader\n\n\n# Data model\nclass GradeHallucinations(BaseModel):\n \"\"\"Binary score for hallucination present in generation answer.\"\"\"\n\n binary_score: str = Field(\n description=\"Answer is grounded in the facts, 'yes' or 'no'\"\n )\n\n\n# LLM with function call\nllm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\nstructured_llm_grader = llm.with_structured_output(GradeHallucinations)\n\n# https://smith.langchain.com/hub/efriis/self-rag-hallucination-grader\nhallucination_prompt = hub.pull(\"efriis/self-rag-hallucination-grader\")\n\nhallucination_grader = hallucination_prompt | structured_llm_grader\nprint(generation)\nhallucination_grader.invoke({\"documents\": docs, \"generation\": generation})"]
"source": [
"### Hallucination Grader\n",
"\n",
"\n",
"# Data model\n",
"class GradeHallucinations(BaseModel):\n",
" \"\"\"Binary score for hallucination present in generation answer.\"\"\"\n",
"\n",
" binary_score: str = Field(\n",
" description=\"Answer is grounded in the facts, 'yes' or 'no'\"\n",
" )\n",
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeHallucinations)\n",
"\n",
"# https://smith.langchain.com/hub/efriis/self-rag-hallucination-grader\n",
"hallucination_prompt = hub.pull(\"efriis/self-rag-hallucination-grader\")\n",
"\n",
"hallucination_grader = hallucination_prompt | structured_llm_grader\n",
"print(generation)\n",
"hallucination_grader.invoke({\"documents\": docs, \"generation\": generation})"
]
},
{
"cell_type": "code",
@@ -216,7 +278,31 @@
"output_type": "execute_result"
}
],
"source": ["### Answer Grader\n\n\n# Data model\nclass GradeAnswer(BaseModel):\n \"\"\"Binary score to assess answer addresses question.\"\"\"\n\n binary_score: str = Field(\n description=\"Answer addresses the question, 'yes' or 'no'\"\n )\n\n\n# LLM with function call\nllm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\nstructured_llm_grader = llm.with_structured_output(GradeAnswer)\n\n# Prompt\nanswer_prompt = hub.pull(\"efriis/self-rag-answer-grader\")\n\nanswer_grader = answer_prompt | structured_llm_grader\nprint(question)\nprint(generation)\nanswer_grader.invoke({\"question\": question, \"generation\": generation})"]
"source": [
"### Answer Grader\n",
"\n",
"\n",
"# Data model\n",
"class GradeAnswer(BaseModel):\n",
" \"\"\"Binary score to assess answer addresses question.\"\"\"\n",
"\n",
" binary_score: str = Field(\n",
" description=\"Answer addresses the question, 'yes' or 'no'\"\n",
" )\n",
"\n",
"\n",
"# LLM with function call\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"structured_llm_grader = llm.with_structured_output(GradeAnswer)\n",
"\n",
"# Prompt\n",
"answer_prompt = hub.pull(\"efriis/self-rag-answer-grader\")\n",
"\n",
"answer_grader = answer_prompt | structured_llm_grader\n",
"print(question)\n",
"print(generation)\n",
"answer_grader.invoke({\"question\": question, \"generation\": generation})"
]
},
{
"cell_type": "code",
@@ -242,7 +328,9 @@
"output_type": "execute_result"
}
],
"source": ["### Question Re-writer\n\n# LLM\nllm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n\n# Prompt\nre_write_prompt = hub.pull(\"efriis/self-rag-question-rewriter\")\n\nquestion_rewriter = re_write_prompt | llm | StrOutputParser()\nprint(question)\nquestion_rewriter.invoke({\"question\": question})"]
"source": [
"### Question Re-writer\n\n# LLM\nllm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n\n# Prompt\nre_write_prompt = hub.pull(\"efriis/self-rag-question-rewriter\")\n\nquestion_rewriter = re_write_prompt | llm | StrOutputParser()\nprint(question)\nquestion_rewriter.invoke({\"question\": question})"
]
},
{
"cell_type": "markdown",
@@ -262,7 +350,9 @@
"id": "f1617e9e-66a8-4c1a-a1fe-cc936284c085",
"metadata": {},
"outputs": [],
"source": ["from typing import List\n\nfrom typing_extensions import TypedDict\n\n\nclass GraphState(TypedDict):\n \"\"\"\n Represents the state of our graph.\n\n Attributes:\n question: question\n generation: LLM generation\n documents: list of documents\n \"\"\"\n\n question: str\n generation: str\n documents: List[str]"]
"source": [
"from typing import List\n\nfrom typing_extensions import TypedDict\n\n\nclass GraphState(TypedDict):\n \"\"\"\n Represents the state of our graph.\n\n Attributes:\n question: question\n generation: LLM generation\n documents: list of documents\n \"\"\"\n\n question: str\n generation: str\n documents: List[str]"
]
},
{
"cell_type": "code",
@@ -270,7 +360,9 @@
"id": "add509d8-6682-4127-8d95-13dd37d79702",
"metadata": {},
"outputs": [],
"source": ["### Nodes\n\n\ndef retrieve(state):\n \"\"\"\n Retrieve documents\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): New key added to state, documents, that contains retrieved documents\n \"\"\"\n print(\"---RETRIEVE---\")\n question = state[\"question\"]\n\n # Retrieval\n documents = retriever.invoke(question)\n return {\"documents\": documents, \"question\": question}\n\n\ndef generate(state):\n \"\"\"\n Generate answer\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): New key added to state, generation, that contains LLM generation\n \"\"\"\n print(\"---GENERATE---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # RAG generation\n generation = rag_chain.invoke({\"context\": documents, \"question\": question})\n return {\"documents\": documents, \"question\": question, \"generation\": generation}\n\n\ndef grade_documents(state):\n \"\"\"\n Determines whether the retrieved documents are relevant to the question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): Updates documents key with only filtered relevant documents\n \"\"\"\n\n print(\"---CHECK DOCUMENT RELEVANCE TO QUESTION---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # Score each doc\n filtered_docs = []\n for d in documents:\n score = retrieval_grader.invoke(\n {\"question\": question, \"document\": d.page_content}\n )\n grade = score.binary_score\n if grade == \"yes\":\n print(\"---GRADE: DOCUMENT RELEVANT---\")\n filtered_docs.append(d)\n else:\n print(\"---GRADE: DOCUMENT NOT RELEVANT---\")\n continue\n return {\"documents\": filtered_docs, \"question\": question}\n\n\ndef transform_query(state):\n \"\"\"\n Transform the query to produce a better question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): Updates question key with a re-phrased question\n \"\"\"\n\n print(\"---TRANSFORM QUERY---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # Re-write question\n better_question = question_rewriter.invoke({\"question\": question})\n return {\"documents\": documents, \"question\": better_question}"]
"source": [
"### Nodes\n\n\ndef retrieve(state):\n \"\"\"\n Retrieve documents\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): New key added to state, documents, that contains retrieved documents\n \"\"\"\n print(\"---RETRIEVE---\")\n question = state[\"question\"]\n\n # Retrieval\n documents = retriever.invoke(question)\n return {\"documents\": documents, \"question\": question}\n\n\ndef generate(state):\n \"\"\"\n Generate answer\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): New key added to state, generation, that contains LLM generation\n \"\"\"\n print(\"---GENERATE---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # RAG generation\n generation = rag_chain.invoke({\"context\": documents, \"question\": question})\n return {\"documents\": documents, \"question\": question, \"generation\": generation}\n\n\ndef grade_documents(state):\n \"\"\"\n Determines whether the retrieved documents are relevant to the question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): Updates documents key with only filtered relevant documents\n \"\"\"\n\n print(\"---CHECK DOCUMENT RELEVANCE TO QUESTION---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # Score each doc\n filtered_docs = []\n for d in documents:\n score = retrieval_grader.invoke(\n {\"question\": question, \"document\": d.page_content}\n )\n grade = score.binary_score\n if grade == \"yes\":\n print(\"---GRADE: DOCUMENT RELEVANT---\")\n filtered_docs.append(d)\n else:\n print(\"---GRADE: DOCUMENT NOT RELEVANT---\")\n continue\n return {\"documents\": filtered_docs, \"question\": question}\n\n\ndef transform_query(state):\n \"\"\"\n Transform the query to produce a better question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n state (dict): Updates question key with a re-phrased question\n \"\"\"\n\n print(\"---TRANSFORM QUERY---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n\n # Re-write question\n better_question = question_rewriter.invoke({\"question\": question})\n return {\"documents\": documents, \"question\": better_question}"
]
},
{
"cell_type": "code",
@@ -278,7 +370,9 @@
"id": "09fc91b4",
"metadata": {},
"outputs": [],
"source": ["### Edges\n\n\ndef decide_to_generate(state):\n \"\"\"\n Determines whether to generate an answer, or re-generate a question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n str: Binary decision for next node to call\n \"\"\"\n\n print(\"---ASSESS GRADED DOCUMENTS---\")\n state[\"question\"]\n filtered_documents = state[\"documents\"]\n\n if not filtered_documents:\n # All documents have been filtered check_relevance\n # We will re-generate a new query\n print(\n \"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, TRANSFORM QUERY---\"\n )\n return \"transform_query\"\n else:\n # We have relevant documents, so generate answer\n print(\"---DECISION: GENERATE---\")\n return \"generate\"\n\n\ndef grade_generation_v_documents_and_question(state):\n \"\"\"\n Determines whether the generation is grounded in the document and answers question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n str: Decision for next node to call\n \"\"\"\n\n print(\"---CHECK HALLUCINATIONS---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n generation = state[\"generation\"]\n\n score = hallucination_grader.invoke(\n {\"documents\": documents, \"generation\": generation}\n )\n grade = score.binary_score\n\n # Check hallucination\n if grade == \"yes\":\n print(\"---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---\")\n # Check question-answering\n print(\"---GRADE GENERATION vs QUESTION---\")\n score = answer_grader.invoke({\"question\": question, \"generation\": generation})\n grade = score.binary_score\n if grade == \"yes\":\n print(\"---DECISION: GENERATION ADDRESSES QUESTION---\")\n return \"useful\"\n else:\n print(\"---DECISION: GENERATION DOES NOT ADDRESS QUESTION---\")\n return \"not useful\"\n else:\n pprint(\"---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---\")\n return \"not supported\""]
"source": [
"### Edges\n\n\ndef decide_to_generate(state):\n \"\"\"\n Determines whether to generate an answer, or re-generate a question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n str: Binary decision for next node to call\n \"\"\"\n\n print(\"---ASSESS GRADED DOCUMENTS---\")\n state[\"question\"]\n filtered_documents = state[\"documents\"]\n\n if not filtered_documents:\n # All documents have been filtered check_relevance\n # We will re-generate a new query\n print(\n \"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, TRANSFORM QUERY---\"\n )\n return \"transform_query\"\n else:\n # We have relevant documents, so generate answer\n print(\"---DECISION: GENERATE---\")\n return \"generate\"\n\n\ndef grade_generation_v_documents_and_question(state):\n \"\"\"\n Determines whether the generation is grounded in the document and answers question.\n\n Args:\n state (dict): The current graph state\n\n Returns:\n str: Decision for next node to call\n \"\"\"\n\n print(\"---CHECK HALLUCINATIONS---\")\n question = state[\"question\"]\n documents = state[\"documents\"]\n generation = state[\"generation\"]\n\n score = hallucination_grader.invoke(\n {\"documents\": documents, \"generation\": generation}\n )\n grade = score.binary_score\n\n # Check hallucination\n if grade == \"yes\":\n print(\"---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---\")\n # Check question-answering\n print(\"---GRADE GENERATION vs QUESTION---\")\n score = answer_grader.invoke({\"question\": question, \"generation\": generation})\n grade = score.binary_score\n if grade == \"yes\":\n print(\"---DECISION: GENERATION ADDRESSES QUESTION---\")\n return \"useful\"\n else:\n print(\"---DECISION: GENERATION DOES NOT ADDRESS QUESTION---\")\n return \"not useful\"\n else:\n pprint(\"---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---\")\n return \"not supported\""
]
},
{
"cell_type": "markdown",
@@ -331,7 +425,9 @@
]
}
],
"source": ["from pprint import pprint\n\n# Run\ninputs = {\"question\": \"Movies that star Daniel Craig\"}\nfor output in app.stream(inputs):\n for key, value in output.items():\n # Node\n pprint(f\"Node '{key}':\")\n pprint(\"\\n---\\n\")\n\n# Final generation\npprint(value[\"generation\"])"]
"source": [
"from pprint import pprint\n\n# Run\ninputs = {\"question\": \"Movies that star Daniel Craig\"}\nfor output in app.stream(inputs):\n for key, value in output.items():\n # Node\n pprint(f\"Node '{key}':\")\n pprint(\"\\n---\\n\")\n\n# Final generation\npprint(value[\"generation\"])"
]
},
{
"cell_type": "code",
@@ -339,7 +435,9 @@
"id": "4138bc51-8c84-4b8a-8d24-f7f470721f6f",
"metadata": {},
"outputs": [],
"source": ["inputs = {\"question\": \"Which movies are about aliens?\"}\nfor output in app.stream(inputs):\n for key, value in output.items():\n # Node\n pprint(f\"Node '{key}':\")\n pprint(\"\\n---\\n\")\n\n# Final generation\npprint(value[\"generation\"])"]
"source": [
"inputs = {\"question\": \"Which movies are about aliens?\"}\nfor output in app.stream(inputs):\n for key, value in output.items():\n # Node\n pprint(f\"Node '{key}':\")\n pprint(\"\\n---\\n\")\n\n# Final generation\npprint(value[\"generation\"])"
]
},
{
"cell_type": "code",
@@ -347,7 +445,9 @@
"id": "42369ab8-322d-434a-b5dd-2266e4cb2903",
"metadata": {},
"outputs": [],
"source": [""]
"source": [
""
]
}
],
"metadata": {
+14
View File
@@ -13,6 +13,20 @@ By default `langgraph-checkpoint-postgres` installs `psycopg` (Psycopg 3) withou
> [!IMPORTANT]
> When manually creating Postgres connections and passing them to `PostgresSaver` or `AsyncPostgresSaver`, make sure to include `autocommit=True` and `row_factory=dict_row` (`from psycopg.rows import dict_row`). See a full example in this [how-to guide](https://langchain-ai.github.io/langgraph/how-tos/persistence_postgres/).
>
> **Why these parameters are required:**
> - `autocommit=True`: Required for the `.setup()` method to properly commit the checkpoint tables to the database. Without this, table creation may not be persisted.
> - `row_factory=dict_row`: Required because the PostgresSaver implementation accesses database rows using dictionary-style syntax (e.g., `row["column_name"]`). The default `tuple_row` factory returns tuples that only support index-based access (e.g., `row[0]`), which will cause `TypeError` exceptions when the checkpointer tries to access columns by name.
>
> **Example of incorrect usage:**
> ```python
> # ❌ This will fail with TypeError during checkpointer operations
> with psycopg.connect(DB_URI) as conn: # Missing autocommit=True and row_factory=dict_row
> checkpointer = PostgresSaver(conn)
> checkpointer.setup() # May not persist tables properly
> # Any operation that reads from database will fail with:
> # TypeError: tuple indices must be integers or slices, not str
> ```
```python
from langgraph.checkpoint.postgres import PostgresSaver
@@ -1,8 +1,10 @@
from __future__ import annotations
import threading
from collections import defaultdict
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from typing import Any, Optional
from typing import Any
from langchain_core.runnables import RunnableConfig
from psycopg import Capabilities, Connection, Cursor, Pipeline
@@ -34,8 +36,8 @@ class PostgresSaver(BasePostgresSaver):
def __init__(
self,
conn: _internal.Conn,
pipe: Optional[Pipeline] = None,
serde: Optional[SerializerProtocol] = None,
pipe: Pipeline | None = None,
serde: SerializerProtocol | None = None,
) -> None:
super().__init__(serde=serde)
if isinstance(conn, ConnectionPool) and pipe is not None:
@@ -52,7 +54,7 @@ class PostgresSaver(BasePostgresSaver):
@contextmanager
def from_conn_string(
cls, conn_string: str, *, pipeline: bool = False
) -> Iterator["PostgresSaver"]:
) -> Iterator[PostgresSaver]:
"""Create a new PostgresSaver instance from a connection string.
Args:
@@ -99,11 +101,11 @@ class PostgresSaver(BasePostgresSaver):
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints from the database.
@@ -173,34 +175,9 @@ class PostgresSaver(BasePostgresSaver):
value["channel_values"],
)
for value in values:
yield CheckpointTuple(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
self._load_writes(value["pending_writes"]),
)
yield self._load_checkpoint_tuple(value)
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database.
This method retrieves a checkpoint tuple from the Postgres database based on the
@@ -269,32 +246,7 @@ class PostgresSaver(BasePostgresSaver):
value["channel_values"],
)
return CheckpointTuple(
{
"configurable": {
"thread_id": thread_id,
"checkpoint_ns": checkpoint_ns,
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": thread_id,
"checkpoint_ns": checkpoint_ns,
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
self._load_writes(value["pending_writes"]),
)
return self._load_checkpoint_tuple(value)
def put(
self,
@@ -464,5 +416,44 @@ class PostgresSaver(BasePostgresSaver):
with conn.cursor(binary=True, row_factory=dict_row) as cur:
yield cur
def _load_checkpoint_tuple(self, value: DictRow) -> CheckpointTuple:
"""
Convert a database row into a CheckpointTuple object.
Args:
value: A row from the database containing checkpoint data.
Returns:
CheckpointTuple: A structured representation of the checkpoint,
including its configuration, metadata, parent checkpoint (if any),
and pending writes.
"""
return CheckpointTuple(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
self._load_writes(value["pending_writes"]),
)
__all__ = ["PostgresSaver", "BasePostgresSaver", "Conn"]
@@ -1,8 +1,10 @@
from __future__ import annotations
import asyncio
from collections import defaultdict
from collections.abc import AsyncIterator, Iterator, Sequence
from contextlib import asynccontextmanager
from typing import Any, Optional
from typing import Any
from langchain_core.runnables import RunnableConfig
from psycopg import AsyncConnection, AsyncCursor, AsyncPipeline, Capabilities
@@ -34,8 +36,8 @@ class AsyncPostgresSaver(BasePostgresSaver):
def __init__(
self,
conn: _ainternal.Conn,
pipe: Optional[AsyncPipeline] = None,
serde: Optional[SerializerProtocol] = None,
pipe: AsyncPipeline | None = None,
serde: SerializerProtocol | None = None,
) -> None:
super().__init__(serde=serde)
if isinstance(conn, AsyncConnectionPool) and pipe is not None:
@@ -56,8 +58,8 @@ class AsyncPostgresSaver(BasePostgresSaver):
conn_string: str,
*,
pipeline: bool = False,
serde: Optional[SerializerProtocol] = None,
) -> AsyncIterator["AsyncPostgresSaver"]:
serde: SerializerProtocol | None = None,
) -> AsyncIterator[AsyncPostgresSaver]:
"""Create a new AsyncPostgresSaver instance from a connection string.
Args:
@@ -104,11 +106,11 @@ class AsyncPostgresSaver(BasePostgresSaver):
async def alist(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[CheckpointTuple]:
"""List checkpoints from the database asynchronously.
@@ -160,34 +162,9 @@ class AsyncPostgresSaver(BasePostgresSaver):
value["channel_values"],
)
for value in values:
yield CheckpointTuple(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
await asyncio.to_thread(self._load_writes, value["pending_writes"]),
)
yield await self._load_checkpoint_tuple(value)
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
async def aget_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database asynchronously.
This method retrieves a checkpoint tuple from the Postgres database based on the
@@ -236,32 +213,7 @@ class AsyncPostgresSaver(BasePostgresSaver):
value["channel_values"],
)
return CheckpointTuple(
{
"configurable": {
"thread_id": thread_id,
"checkpoint_ns": checkpoint_ns,
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": thread_id,
"checkpoint_ns": checkpoint_ns,
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
await asyncio.to_thread(self._load_writes, value["pending_writes"]),
)
return await self._load_checkpoint_tuple(value)
async def aput(
self,
@@ -422,13 +374,52 @@ class AsyncPostgresSaver(BasePostgresSaver):
async with conn.cursor(binary=True, row_factory=dict_row) as cur:
yield cur
async def _load_checkpoint_tuple(self, value: DictRow) -> CheckpointTuple:
"""
Convert a database row into a CheckpointTuple object.
Args:
value: A row from the database containing checkpoint data.
Returns:
CheckpointTuple: A structured representation of the checkpoint,
including its configuration, metadata, parent checkpoint (if any),
and pending writes.
"""
return CheckpointTuple(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["checkpoint_id"],
}
},
{
**value["checkpoint"],
"channel_values": self._load_blobs(value["channel_values"]),
},
value["metadata"],
(
{
"configurable": {
"thread_id": value["thread_id"],
"checkpoint_ns": value["checkpoint_ns"],
"checkpoint_id": value["parent_checkpoint_id"],
}
}
if value["parent_checkpoint_id"]
else None
),
await asyncio.to_thread(self._load_writes, value["pending_writes"]),
)
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints from the database.
@@ -466,7 +457,7 @@ class AsyncPostgresSaver(BasePostgresSaver):
except StopAsyncIteration:
break
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database.
This method retrieves a checkpoint tuple from the Postgres database based on the
@@ -1,3 +1,5 @@
from __future__ import annotations
import random
from collections.abc import Sequence
from typing import Any, Optional, cast
@@ -186,7 +188,7 @@ class BasePostgresSaver(BaseCheckpointSaver[str]):
checkpoint_ns: str,
values: dict[str, Any],
versions: ChannelVersions,
) -> list[tuple[str, str, str, str, str, Optional[bytes]]]:
) -> list[tuple[str, str, str, str, str, bytes | None]]:
if not versions:
return []
@@ -244,7 +246,7 @@ class BasePostgresSaver(BaseCheckpointSaver[str]):
for idx, (channel, value) in enumerate(writes)
]
def get_next_version(self, current: Optional[str]) -> str:
def get_next_version(self, current: str | None) -> str:
if current is None:
current_v = 0
elif isinstance(current, int):
@@ -257,9 +259,9 @@ class BasePostgresSaver(BaseCheckpointSaver[str]):
def _search_where(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
filter: MetadataInput,
before: Optional[RunnableConfig] = None,
before: RunnableConfig | None = None,
) -> tuple[str, list[Any]]:
"""Return WHERE clause predicates for alist() given config, filter, before.
@@ -1,9 +1,11 @@
from __future__ import annotations
import asyncio
import logging
from collections.abc import AsyncIterator, Iterable, Sequence
from contextlib import asynccontextmanager
from types import TracebackType
from typing import Any, Callable, Optional, Union, cast
from typing import Any, Callable, cast
import orjson
from psycopg import AsyncConnection, AsyncCursor, AsyncPipeline, Capabilities
@@ -132,12 +134,10 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
self,
conn: _ainternal.Conn,
*,
pipe: Optional[AsyncPipeline] = None,
deserializer: Optional[
Callable[[Union[bytes, orjson.Fragment]], dict[str, Any]]
] = None,
index: Optional[PostgresIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
pipe: AsyncPipeline | None = None,
deserializer: Callable[[bytes | orjson.Fragment], dict[str, Any]] | None = None,
index: PostgresIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> None:
if isinstance(conn, AsyncConnectionPool) and pipe is not None:
raise ValueError(
@@ -157,7 +157,7 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
self.embeddings = None
self.ttl_config = ttl
self._ttl_sweeper_task: Optional[asyncio.Task[None]] = None
self._ttl_sweeper_task: asyncio.Task[None] | None = None
self._ttl_stop_event = asyncio.Event()
async def abatch(self, ops: Iterable[Op]) -> list[Result]:
@@ -180,10 +180,10 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
conn_string: str,
*,
pipeline: bool = False,
pool_config: Optional[PoolConfig] = None,
index: Optional[PostgresIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
) -> AsyncIterator["AsyncPostgresStore"]:
pool_config: PoolConfig | None = None,
index: PostgresIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> AsyncIterator[AsyncPostgresStore]:
"""Create a new AsyncPostgresStore instance from a connection string.
Args:
@@ -289,7 +289,7 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
return deleted_count
async def start_ttl_sweeper(
self, sweep_interval_minutes: Optional[int] = None
self, sweep_interval_minutes: int | None = None
) -> asyncio.Task[None]:
"""Periodically delete expired store items based on TTL.
@@ -334,7 +334,7 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
self._ttl_sweeper_task = task
return task
async def stop_ttl_sweeper(self, timeout: Optional[float] = None) -> bool:
async def stop_ttl_sweeper(self, timeout: float | None = None) -> bool:
"""Stop the TTL sweeper task if it's running.
Args:
@@ -369,14 +369,14 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con
return success
async def __aenter__(self) -> "AsyncPostgresStore":
async def __aenter__(self) -> AsyncPostgresStore:
return self
async def __aexit__(
self,
exc_type: Optional[type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional["TracebackType"],
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
# Ensure the TTL sweeper task is stopped when exiting the context
if hasattr(self, "_ttl_sweeper_task") and self._ttl_sweeper_task is not None:
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import concurrent.futures
import json
@@ -14,7 +16,6 @@ from typing import (
Generic,
Literal,
NamedTuple,
Optional,
TypeVar,
Union,
cast,
@@ -56,8 +57,8 @@ class Migration(NamedTuple):
"""A database migration with optional conditions and parameters."""
sql: str
params: Optional[dict[str, Any]] = None
condition: Optional[Callable[["BasePostgresStore"], bool]] = None
params: dict[str, Any] | None = None
condition: Callable[[BasePostgresStore], bool] | None = None
MIGRATIONS: Sequence[str] = [
@@ -155,7 +156,7 @@ class PoolConfig(TypedDict, total=False):
min_size: int
"""Minimum number of connections maintained in the pool. Defaults to 1."""
max_size: Optional[int]
max_size: int | None
"""Maximum number of connections allowed in the pool. None means unlimited."""
kwargs: dict
@@ -230,8 +231,8 @@ class BasePostgresStore(Generic[C]):
MIGRATIONS = MIGRATIONS
VECTOR_MIGRATIONS = VECTOR_MIGRATIONS
conn: C
_deserializer: Optional[Callable[[Union[bytes, orjson.Fragment]], dict[str, Any]]]
index_config: Optional[PostgresIndexConfig]
_deserializer: Callable[[bytes | orjson.Fragment], dict[str, Any]] | None
index_config: PostgresIndexConfig | None
def _get_batch_GET_ops_queries(
self,
@@ -293,7 +294,7 @@ class BasePostgresStore(Generic[C]):
put_ops: Sequence[tuple[int, PutOp]],
) -> tuple[
list[tuple[str, Sequence]],
Optional[tuple[str, Sequence[tuple[str, str, str, str]]]],
tuple[str, Sequence[tuple[str, str, str, str]]] | None,
]:
dedupped_ops: dict[tuple[tuple[str, ...], str], PutOp] = {}
for _, op in put_ops:
@@ -320,9 +321,7 @@ class BasePostgresStore(Generic[C]):
)
params = (_namespace_to_text(namespace), *keys)
queries.append((query, params))
embedding_request: Optional[tuple[str, Sequence[tuple[str, str, str, str]]]] = (
None
)
embedding_request: tuple[str, Sequence[tuple[str, str, str, str]]] | None = None
if inserts:
values = []
insertion_params = []
@@ -403,7 +402,7 @@ class BasePostgresStore(Generic[C]):
self,
search_ops: Sequence[tuple[int, SearchOp]],
) -> tuple[
list[tuple[str, list[Union[None, str, list[float]]]]], # queries, params
list[tuple[str, list[None | str | list[float]]]], # queries, params
list[tuple[int, str]], # idx, query_text pairs to embed
]:
"""
@@ -432,7 +431,7 @@ class BasePostgresStore(Generic[C]):
filter_params.extend([key, orjson.dumps(value).decode("utf-8")])
ns_condition = "TRUE"
ns_param: Optional[Sequence[Union[str]]] = None
ns_param: Sequence[str] | None = None
if op.namespace_prefix:
ns_condition = "store.prefix LIKE %s"
ns_param = (f"{_namespace_to_text(op.namespace_prefix)}%",)
@@ -719,12 +718,10 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
self,
conn: _pg_internal.Conn,
*,
pipe: Optional[Pipeline] = None,
deserializer: Optional[
Callable[[Union[bytes, orjson.Fragment]], dict[str, Any]]
] = None,
index: Optional[PostgresIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
pipe: Pipeline | None = None,
deserializer: Callable[[bytes | orjson.Fragment], dict[str, Any]] | None = None,
index: PostgresIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> None:
super().__init__()
self._deserializer = deserializer
@@ -738,7 +735,7 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
else:
self.embeddings = None
self.ttl_config = ttl
self._ttl_sweeper_thread: Optional[threading.Thread] = None
self._ttl_sweeper_thread: threading.Thread | None = None
self._ttl_stop_event = threading.Event()
@classmethod
@@ -748,10 +745,10 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
conn_string: str,
*,
pipeline: bool = False,
pool_config: Optional[PoolConfig] = None,
index: Optional[PostgresIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
) -> Iterator["PostgresStore"]:
pool_config: PoolConfig | None = None,
index: PostgresIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> Iterator[PostgresStore]:
"""Create a new PostgresStore instance from a connection string.
Args:
@@ -810,7 +807,7 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
return deleted_count
def start_ttl_sweeper(
self, sweep_interval_minutes: Optional[int] = None
self, sweep_interval_minutes: int | None = None
) -> concurrent.futures.Future[None]:
"""Periodically delete expired store items based on TTL.
@@ -867,7 +864,7 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]):
)
return future
def stop_ttl_sweeper(self, timeout: Optional[float] = None) -> bool:
def stop_ttl_sweeper(self, timeout: float | None = None) -> bool:
"""Stop the TTL sweeper thread if it's running.
Args:
@@ -1196,7 +1193,7 @@ def _row_to_item(
namespace: tuple[str, ...],
row: Row,
*,
loader: Optional[Callable[[Union[bytes, orjson.Fragment]], dict[str, Any]]] = None,
loader: Callable[[bytes | orjson.Fragment], dict[str, Any]] | None = None,
) -> Item:
"""Convert a row from the database into an Item.
@@ -1224,7 +1221,7 @@ def _row_to_search_item(
namespace: tuple[str, ...],
row: Row,
*,
loader: Optional[Callable[[Union[bytes, orjson.Fragment]], dict[str, Any]]] = None,
loader: Callable[[bytes | orjson.Fragment], dict[str, Any]] | None = None,
) -> SearchItem:
"""Convert a row from the database into an Item."""
loader = loader or _json_loads
@@ -1255,7 +1252,7 @@ def _group_ops(ops: Iterable[Op]) -> tuple[dict[type, list[tuple[int, Op]]], int
return grouped_ops, tot
def _json_loads(content: Union[bytes, orjson.Fragment]) -> Any:
def _json_loads(content: bytes | orjson.Fragment) -> Any:
if isinstance(content, orjson.Fragment):
if hasattr(content, "buf"):
content = content.buf
@@ -1267,7 +1264,7 @@ def _json_loads(content: Union[bytes, orjson.Fragment]) -> Any:
return orjson.loads(cast(bytes, content))
def _decode_ns_bytes(namespace: Union[str, bytes, list]) -> tuple[str, ...]:
def _decode_ns_bytes(namespace: str | bytes | list) -> tuple[str, ...]:
if isinstance(namespace, list):
return tuple(namespace)
if isinstance(namespace, bytes):
@@ -1316,16 +1313,16 @@ def get_distance_operator(store: Any) -> tuple[str, str]:
def _ensure_index_config(
index_config: PostgresIndexConfig,
) -> tuple[Optional["Embeddings"], PostgresIndexConfig]:
) -> tuple[Embeddings | None, PostgresIndexConfig]:
index_config = index_config.copy()
tokenized: list[tuple[str, Union[Literal["$"], list[str]]]] = []
tokenized: list[tuple[str, Literal["$"] | list[str]]] = []
tot = 0
text_fields = index_config.get("fields") or ["$"]
if isinstance(text_fields, str):
text_fields = [text_fields]
if not isinstance(text_fields, list):
raise ValueError(f"Text fields must be a list or a string. Got {text_fields}")
for p in text_fields:
fields = index_config.get("fields") or ["$"]
if isinstance(fields, str):
fields = [fields]
if not isinstance(fields, list):
raise ValueError(f"Text fields must be a list or a string. Got {fields}")
for p in fields:
if p == "$":
tokenized.append((p, "$"))
tot += 1
+1 -1
View File
@@ -56,7 +56,7 @@ lint.select = [
"B", # flake8-bugbear
"I", # isort
]
lint.ignore = ["E501", "B008", "UP007", "UP006"]
lint.ignore = ["E501", "B008"]
[tool.mypy]
# https://mypy.readthedocs.io/en/stable/config_file.html
@@ -1,13 +1,15 @@
from __future__ import annotations
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import Any, Optional, Protocol
from typing import Any, Protocol
from langgraph.checkpoint.base import Checkpoint, EmptyChannelError
from langgraph.checkpoint.base.id import uuid6
class ChannelProtocol(Protocol):
def checkpoint(self) -> Optional[Any]: ...
def checkpoint(self) -> Any | None: ...
def empty_checkpoint() -> Checkpoint:
@@ -23,10 +25,10 @@ def empty_checkpoint() -> Checkpoint:
def create_checkpoint(
checkpoint: Checkpoint,
channels: Optional[Mapping[str, ChannelProtocol]],
channels: Mapping[str, ChannelProtocol] | None,
step: int,
*,
id: Optional[str] = None,
id: str | None = None,
) -> Checkpoint:
"""Create a checkpoint for the given channels."""
ts = datetime.now(timezone.utc).isoformat()
@@ -1,4 +1,6 @@
# type: ignore
from __future__ import annotations
import asyncio
import itertools
import sys
@@ -6,7 +8,7 @@ import uuid
from collections.abc import AsyncIterator
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from typing import Any, Optional
from typing import Any
import pytest
from langchain_core.embeddings import Embeddings
@@ -353,7 +355,7 @@ async def _create_vector_store(
vector_type: str,
distance_type: str,
fake_embeddings: CharacterEmbeddings,
text_fields: Optional[list[str]] = None,
text_fields: list[str] | None = None,
) -> AsyncIterator[AsyncPostgresStore]:
"""Create a store with vector search enabled."""
if sys.version_info < (3, 10):
+3 -2
View File
@@ -1,9 +1,10 @@
# type: ignore
from __future__ import annotations
import re
import time
from contextlib import contextmanager
from typing import Any, Optional
from typing import Any
from uuid import uuid4
import pytest
@@ -379,7 +380,7 @@ def _create_vector_store(
vector_type: str,
distance_type: str,
fake_embeddings: Embeddings,
text_fields: Optional[list[str]] = None,
text_fields: list[str] | None = None,
enable_ttl: bool = True,
) -> PostgresStore:
"""Create a store with vector search enabled."""
+704 -703
View File
File diff suppressed because it is too large Load Diff
@@ -1,9 +1,11 @@
from __future__ import annotations
import random
import sqlite3
import threading
from collections.abc import AsyncIterator, Iterator, Sequence
from contextlib import closing, contextmanager
from typing import Any, Optional, cast
from typing import Any, cast
from langchain_core.runnables import RunnableConfig
@@ -76,7 +78,7 @@ class SqliteSaver(BaseCheckpointSaver[str]):
self,
conn: sqlite3.Connection,
*,
serde: Optional[SerializerProtocol] = None,
serde: SerializerProtocol | None = None,
) -> None:
super().__init__(serde=serde)
self.jsonplus_serde = JsonPlusSerializer()
@@ -86,7 +88,7 @@ class SqliteSaver(BaseCheckpointSaver[str]):
@classmethod
@contextmanager
def from_conn_string(cls, conn_string: str) -> Iterator["SqliteSaver"]:
def from_conn_string(cls, conn_string: str) -> Iterator[SqliteSaver]:
"""Create a new SqliteSaver instance from a connection string.
Args:
@@ -178,7 +180,7 @@ class SqliteSaver(BaseCheckpointSaver[str]):
self.conn.commit()
cur.close()
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database.
This method retrieves a checkpoint tuple from the SQLite database based on the
@@ -286,11 +288,11 @@ class SqliteSaver(BaseCheckpointSaver[str]):
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints from the database.
@@ -493,7 +495,7 @@ class SqliteSaver(BaseCheckpointSaver[str]):
(str(thread_id),),
)
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
async def aget_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database asynchronously.
Note:
@@ -504,11 +506,11 @@ class SqliteSaver(BaseCheckpointSaver[str]):
async def alist(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[CheckpointTuple]:
"""List checkpoints from the database asynchronously.
@@ -534,7 +536,7 @@ class SqliteSaver(BaseCheckpointSaver[str]):
"""
raise NotImplementedError(_AIO_ERROR_MSG)
def get_next_version(self, current: Optional[str]) -> str:
def get_next_version(self, current: str | None) -> str:
"""Generate the next version ID for a channel.
This method creates a new version identifier for a channel based on its current version.
@@ -1,8 +1,10 @@
from __future__ import annotations
import asyncio
import random
from collections.abc import AsyncIterator, Iterator, Sequence
from contextlib import asynccontextmanager
from typing import Any, Callable, Optional, TypeVar, cast
from typing import Any, Callable, TypeVar, cast
import aiosqlite
from langchain_core.runnables import RunnableConfig
@@ -108,7 +110,7 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
self,
conn: aiosqlite.Connection,
*,
serde: Optional[SerializerProtocol] = None,
serde: SerializerProtocol | None = None,
):
super().__init__(serde=serde)
self.jsonplus_serde = JsonPlusSerializer()
@@ -121,7 +123,7 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
@asynccontextmanager
async def from_conn_string(
cls, conn_string: str
) -> AsyncIterator["AsyncSqliteSaver"]:
) -> AsyncIterator[AsyncSqliteSaver]:
"""Create a new AsyncSqliteSaver instance from a connection string.
Args:
@@ -133,7 +135,7 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
async with aiosqlite.connect(conn_string) as conn:
yield cls(conn)
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database.
This method retrieves a checkpoint tuple from the SQLite database based on the
@@ -165,11 +167,11 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints from the database asynchronously.
@@ -310,7 +312,7 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
self.is_setup = True
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
async def aget_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the database asynchronously.
This method retrieves a checkpoint tuple from the SQLite database based on the
@@ -398,11 +400,11 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
async def alist(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[CheckpointTuple]:
"""List checkpoints from the database asynchronously.
@@ -589,7 +591,7 @@ class AsyncSqliteSaver(BaseCheckpointSaver[str]):
)
await self.conn.commit()
def get_next_version(self, current: Optional[str]) -> str:
def get_next_version(self, current: str | None) -> str:
"""Generate the next version ID for a channel.
This method creates a new version identifier for a channel based on its current version.
@@ -1,6 +1,8 @@
from __future__ import annotations
import json
from collections.abc import Sequence
from typing import Any, Optional
from typing import Any
from langchain_core.runnables import RunnableConfig
@@ -52,9 +54,9 @@ def _metadata_predicate(
def search_where(
config: Optional[RunnableConfig],
filter: Optional[dict[str, Any]],
before: Optional[RunnableConfig] = None,
config: RunnableConfig | None,
filter: dict[str, Any] | None,
before: RunnableConfig | None = None,
) -> tuple[str, Sequence[Any]]:
"""Return WHERE clause predicates for (a)search() given metadata filter
and `before` config.
@@ -1,10 +1,12 @@
from __future__ import annotations
import asyncio
import logging
from collections import defaultdict
from collections.abc import AsyncIterator, Iterable, Sequence
from contextlib import asynccontextmanager
from types import TracebackType
from typing import Any, Callable, Optional, Union, cast
from typing import Any, Callable, cast
import aiosqlite
import orjson
@@ -88,11 +90,10 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
self,
conn: aiosqlite.Connection,
*,
deserializer: Optional[
Callable[[Union[bytes, str, orjson.Fragment]], dict[str, Any]]
] = None,
index: Optional[SqliteIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
deserializer: Callable[[bytes | str | orjson.Fragment], dict[str, Any]]
| None = None,
index: SqliteIndexConfig | None = None,
ttl: TTLConfig | None = None,
):
"""Initialize the async SQLite store.
@@ -114,7 +115,7 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
else:
self.embeddings = None
self.ttl_config = ttl
self._ttl_sweeper_task: Optional[asyncio.Task[None]] = None
self._ttl_sweeper_task: asyncio.Task[None] | None = None
self._ttl_stop_event = asyncio.Event()
@classmethod
@@ -123,9 +124,9 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
cls,
conn_string: str,
*,
index: Optional[SqliteIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
) -> AsyncIterator["AsyncSqliteStore"]:
index: SqliteIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> AsyncIterator[AsyncSqliteStore]:
"""Create a new AsyncSqliteStore instance from a connection string.
Args:
@@ -253,7 +254,7 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
return deleted_count
async def start_ttl_sweeper(
self, sweep_interval_minutes: Optional[int] = None
self, sweep_interval_minutes: int | None = None
) -> asyncio.Task[None]:
"""Periodically delete expired store items based on TTL.
@@ -298,7 +299,7 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
self._ttl_sweeper_task = task
return task
async def stop_ttl_sweeper(self, timeout: Optional[float] = None) -> bool:
async def stop_ttl_sweeper(self, timeout: float | None = None) -> bool:
"""Stop the TTL sweeper task if it's running.
Args:
@@ -333,14 +334,14 @@ class AsyncSqliteStore(AsyncBatchedBaseStore, BaseSqliteStore):
return success
async def __aenter__(self) -> "AsyncSqliteStore":
async def __aenter__(self) -> AsyncSqliteStore:
return self
async def __aexit__(
self,
exc_type: Optional[type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional["TracebackType"],
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
# Ensure the TTL sweeper task is stopped when exiting the context
if hasattr(self, "_ttl_sweeper_task") and self._ttl_sweeper_task is not None:
@@ -1,3 +1,5 @@
from __future__ import annotations
import concurrent.futures
import datetime
import logging
@@ -6,7 +8,7 @@ import threading
from collections import defaultdict
from collections.abc import Iterable, Iterator, Sequence
from contextlib import contextmanager
from typing import Any, Callable, Literal, NamedTuple, Optional, Union, cast
from typing import Any, Callable, Literal, NamedTuple, cast
import orjson
import sqlite_vec # type: ignore[import-untyped]
@@ -105,7 +107,7 @@ def _decode_ns_text(namespace: str) -> tuple[str, ...]:
return tuple(namespace.split("."))
def _json_loads(content: Union[bytes, str, orjson.Fragment]) -> Any:
def _json_loads(content: bytes | str | orjson.Fragment) -> Any:
if isinstance(content, orjson.Fragment):
if hasattr(content, "buf"):
content = content.buf
@@ -125,9 +127,7 @@ def _row_to_item(
namespace: tuple[str, ...],
row: dict[str, Any],
*,
loader: Optional[
Callable[[Union[bytes, str, orjson.Fragment]], dict[str, Any]]
] = None,
loader: Callable[[bytes | str | orjson.Fragment], dict[str, Any]] | None = None,
) -> Item:
"""Convert a row from the database into an Item."""
val = row["value"]
@@ -149,9 +149,7 @@ def _row_to_search_item(
namespace: tuple[str, ...],
row: dict[str, Any],
*,
loader: Optional[
Callable[[Union[bytes, str, orjson.Fragment]], dict[str, Any]]
] = None,
loader: Callable[[bytes | str | orjson.Fragment], dict[str, Any]] | None = None,
) -> SearchItem:
"""Convert a row from the database into a SearchItem."""
loader = loader or _json_loads
@@ -196,8 +194,8 @@ class BaseSqliteStore:
MIGRATIONS = MIGRATIONS
VECTOR_MIGRATIONS = VECTOR_MIGRATIONS
supports_ttl = True
index_config: Optional[SqliteIndexConfig] = None
ttl_config: Optional[TTLConfig] = None
index_config: SqliteIndexConfig | None = None
ttl_config: TTLConfig | None = None
def _get_batch_GET_ops_queries(
self, get_ops: Sequence[tuple[int, GetOp]]
@@ -259,7 +257,7 @@ class BaseSqliteStore:
self, put_ops: Sequence[tuple[int, PutOp]]
) -> tuple[
list[tuple[str, Sequence]],
Optional[tuple[str, Sequence[tuple[str, str, str, str]]]],
tuple[str, Sequence[tuple[str, str, str, str]]] | None,
]:
# Last-write wins
dedupped_ops: dict[tuple[tuple[str, ...], str], PutOp] = {}
@@ -288,9 +286,7 @@ class BaseSqliteStore:
params = (_namespace_to_text(namespace), *keys)
queries.append((query, params))
embedding_request: Optional[tuple[str, Sequence[tuple[str, str, str, str]]]] = (
None
)
embedding_request: tuple[str, Sequence[tuple[str, str, str, str]]] | None = None
if inserts:
values = []
insertion_params = []
@@ -358,7 +354,7 @@ class BaseSqliteStore:
def _prepare_batch_search_queries(
self, search_ops: Sequence[tuple[int, SearchOp]]
) -> tuple[
list[tuple[str, list[Union[None, str, list[float]]]]], # queries, params
list[tuple[str, list[None | str | list[float]]]], # queries, params
list[tuple[int, str]], # idx, query_text pairs to embed
]:
"""
@@ -785,11 +781,10 @@ class SqliteStore(BaseSqliteStore, BaseStore):
self,
conn: sqlite3.Connection,
*,
deserializer: Optional[
Callable[[Union[bytes, str, orjson.Fragment]], dict[str, Any]]
] = None,
index: Optional[SqliteIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
deserializer: Callable[[bytes | str | orjson.Fragment], dict[str, Any]]
| None = None,
index: SqliteIndexConfig | None = None,
ttl: TTLConfig | None = None,
):
super().__init__()
self._deserializer = deserializer
@@ -802,7 +797,7 @@ class SqliteStore(BaseSqliteStore, BaseStore):
else:
self.embeddings = None
self.ttl_config = ttl
self._ttl_sweeper_thread: Optional[threading.Thread] = None
self._ttl_sweeper_thread: threading.Thread | None = None
self._ttl_stop_event = threading.Event()
def _get_batch_GET_ops_queries(
@@ -956,9 +951,9 @@ class SqliteStore(BaseSqliteStore, BaseStore):
cls,
conn_string: str,
*,
index: Optional[SqliteIndexConfig] = None,
ttl: Optional[TTLConfig] = None,
) -> Iterator["SqliteStore"]:
index: SqliteIndexConfig | None = None,
ttl: TTLConfig | None = None,
) -> Iterator[SqliteStore]:
"""Create a new SqliteStore instance from a connection string.
Args:
@@ -1087,7 +1082,7 @@ class SqliteStore(BaseSqliteStore, BaseStore):
return deleted_count
def start_ttl_sweeper(
self, sweep_interval_minutes: Optional[int] = None
self, sweep_interval_minutes: int | None = None
) -> concurrent.futures.Future[None]:
"""Periodically delete expired store items based on TTL.
@@ -1144,7 +1139,7 @@ class SqliteStore(BaseSqliteStore, BaseStore):
)
return future
def stop_ttl_sweeper(self, timeout: Optional[float] = None) -> bool:
def stop_ttl_sweeper(self, timeout: float | None = None) -> bool:
"""Stop the TTL sweeper thread if it's running.
Args:
@@ -1396,7 +1391,7 @@ def _ensure_index_config(
) -> tuple[Any, SqliteIndexConfig]:
"""Process and validate index configuration."""
index_config = index_config.copy()
tokenized: list[tuple[str, Union[Literal["$"], list[str]]]] = []
tokenized: list[tuple[str, Literal["$"] | list[str]]] = []
tot = 0
text_fields = index_config.get("text_fields") or ["$"]
if isinstance(text_fields, str):
+1 -1
View File
@@ -54,7 +54,7 @@ lint.select = [
"B", # flake8-bugbear
"I", # isort
]
lint.ignore = ["E501", "B008", "UP007", "UP006"]
lint.ignore = ["E501", "B008"]
[tool.pytest-watcher]
now = true
@@ -1,13 +1,15 @@
from __future__ import annotations
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import Any, Optional, Protocol
from typing import Any, Protocol
from langgraph.checkpoint.base import Checkpoint, EmptyChannelError
from langgraph.checkpoint.base.id import uuid6
class ChannelProtocol(Protocol):
def checkpoint(self) -> Optional[Any]: ...
def checkpoint(self) -> Any | None: ...
def empty_checkpoint() -> Checkpoint:
@@ -23,10 +25,10 @@ def empty_checkpoint() -> Checkpoint:
def create_checkpoint(
checkpoint: Checkpoint,
channels: Optional[Mapping[str, ChannelProtocol]],
channels: Mapping[str, ChannelProtocol] | None,
step: int,
*,
id: Optional[str] = None,
id: str | None = None,
) -> Checkpoint:
"""Create a checkpoint for the given channels."""
ts = datetime.now(timezone.utc).isoformat()
+652 -650
View File
File diff suppressed because it is too large Load Diff
@@ -1,11 +1,11 @@
from __future__ import annotations
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import ( # noqa: UP035
Any,
Generic,
List,
Literal,
NamedTuple,
Optional,
TypedDict,
TypeVar,
Union,
@@ -98,8 +98,8 @@ class CheckpointTuple(NamedTuple):
config: RunnableConfig
checkpoint: Checkpoint
metadata: CheckpointMetadata
parent_config: Optional[RunnableConfig] = None
pending_writes: Optional[List[PendingWrite]] = None
parent_config: RunnableConfig | None = None
pending_writes: list[PendingWrite] | None = None
class BaseCheckpointSaver(Generic[V]):
@@ -121,11 +121,11 @@ class BaseCheckpointSaver(Generic[V]):
def __init__(
self,
*,
serde: Optional[SerializerProtocol] = None,
serde: SerializerProtocol | None = None,
) -> None:
self.serde = maybe_add_typed_methods(serde or self.serde)
def get(self, config: RunnableConfig) -> Optional[Checkpoint]:
def get(self, config: RunnableConfig) -> Checkpoint | None:
"""Fetch a checkpoint using the given configuration.
Args:
@@ -137,7 +137,7 @@ class BaseCheckpointSaver(Generic[V]):
if value := self.get_tuple(config):
return value.checkpoint
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Fetch a checkpoint tuple using the given configuration.
Args:
@@ -153,11 +153,11 @@ class BaseCheckpointSaver(Generic[V]):
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints that match the given criteria.
@@ -229,7 +229,7 @@ class BaseCheckpointSaver(Generic[V]):
"""
raise NotImplementedError
async def aget(self, config: RunnableConfig) -> Optional[Checkpoint]:
async def aget(self, config: RunnableConfig) -> Checkpoint | None:
"""Asynchronously fetch a checkpoint using the given configuration.
Args:
@@ -241,7 +241,7 @@ class BaseCheckpointSaver(Generic[V]):
if value := await self.aget_tuple(config):
return value.checkpoint
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
async def aget_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Asynchronously fetch a checkpoint tuple using the given configuration.
Args:
@@ -257,11 +257,11 @@ class BaseCheckpointSaver(Generic[V]):
async def alist(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[CheckpointTuple]:
"""Asynchronously list checkpoints that match the given criteria.
@@ -334,7 +334,7 @@ class BaseCheckpointSaver(Generic[V]):
"""
raise NotImplementedError
def get_next_version(self, current: Optional[V]) -> V:
def get_next_version(self, current: V | None) -> V:
"""Generate the next version ID for a channel.
Default is to use integer versions, incrementing by 1. If you override, you can use str/int/float versions,
@@ -361,7 +361,7 @@ class EmptyChannelError(Exception):
pass
def get_checkpoint_id(config: RunnableConfig) -> Optional[str]:
def get_checkpoint_id(config: RunnableConfig) -> str | None:
"""Get checkpoint ID in a backwards-compatible manner (fallback on thread_ts)."""
return config["configurable"].get(
"checkpoint_id", config["configurable"].get("thread_ts")
@@ -3,10 +3,11 @@ https://github.com/oittaa/uuid6-python/blob/main/src/uuid6/__init__.py#L95
Bundled in to avoid install issues with uuid6 package
"""
from __future__ import annotations
import random
import time
import uuid
from typing import Optional
_last_v6_timestamp = None
@@ -18,12 +19,12 @@ class UUID(uuid.UUID):
def __init__(
self,
hex: Optional[str] = None,
bytes: Optional[bytes] = None,
bytes_le: Optional[bytes] = None,
fields: Optional[tuple[int, int, int, int, int, int]] = None,
int: Optional[int] = None,
version: Optional[int] = None,
hex: str | None = None,
bytes: bytes | None = None,
bytes_le: bytes | None = None,
fields: tuple[int, int, int, int, int, int] | None = None,
int: int | None = None,
version: int | None = None,
*,
is_safe: uuid.SafeUUID = uuid.SafeUUID.unknown,
) -> None:
@@ -75,7 +76,7 @@ def _subsec_decode(value: int) -> int:
return -(-value * 10**6 // 2**20)
def uuid6(node: Optional[int] = None, clock_seq: Optional[int] = None) -> UUID:
def uuid6(node: int | None = None, clock_seq: int | None = None) -> UUID:
r"""UUID version 6 is a field-compatible version of UUIDv1, reordered for
improved DB locality. It is expected that UUIDv6 will primarily be
used in contexts where there are existing v1 UUIDs. Systems that do
@@ -1,3 +1,5 @@
from __future__ import annotations
import logging
import os
import pickle
@@ -7,7 +9,7 @@ from collections import defaultdict
from collections.abc import AsyncIterator, Iterator, Sequence
from contextlib import AbstractAsyncContextManager, AbstractContextManager, ExitStack
from types import TracebackType
from typing import Any, Optional, Union
from typing import Any
from langchain_core.runnables import RunnableConfig
@@ -63,9 +65,7 @@ class InMemorySaver(
# thread ID -> checkpoint NS -> checkpoint ID -> checkpoint mapping
storage: defaultdict[
str,
dict[
str, dict[str, tuple[tuple[str, bytes], tuple[str, bytes], Optional[str]]]
],
dict[str, dict[str, tuple[tuple[str, bytes], tuple[str, bytes], str | None]]],
]
# (thread ID, checkpoint NS, checkpoint ID) -> (task ID, write idx)
writes: defaultdict[
@@ -74,7 +74,7 @@ class InMemorySaver(
]
blobs: dict[
tuple[
str, str, str, Union[str, int, float]
str, str, str, str | int | float
], # thread id, checkpoint ns, channel, version
tuple[str, bytes],
]
@@ -82,7 +82,7 @@ class InMemorySaver(
def __init__(
self,
*,
serde: Optional[SerializerProtocol] = None,
serde: SerializerProtocol | None = None,
factory: type[defaultdict] = defaultdict,
) -> None:
super().__init__(serde=serde)
@@ -95,26 +95,26 @@ class InMemorySaver(
self.stack.enter_context(self.writes) # type: ignore[arg-type]
self.stack.enter_context(self.blobs) # type: ignore[arg-type]
def __enter__(self) -> "InMemorySaver":
def __enter__(self) -> InMemorySaver:
return self.stack.__enter__()
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
return self.stack.__exit__(exc_type, exc_value, traceback)
async def __aenter__(self) -> "InMemorySaver":
async def __aenter__(self) -> InMemorySaver:
return self.stack.__enter__()
async def __aexit__(
self,
__exc_type: Optional[type[BaseException]],
__exc_value: Optional[BaseException],
__traceback: Optional[TracebackType],
) -> Optional[bool]:
__exc_type: type[BaseException] | None,
__exc_value: BaseException | None,
__traceback: TracebackType | None,
) -> bool | None:
return self.stack.__exit__(__exc_type, __exc_value, __traceback)
def _load_blobs(
@@ -129,7 +129,7 @@ class InMemorySaver(
channel_values[k] = self.serde.loads_typed(vv)
return channel_values
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
def get_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Get a checkpoint tuple from the in-memory storage.
This method retrieves a checkpoint tuple from the in-memory storage based on the
@@ -213,11 +213,11 @@ class InMemorySaver(
def list(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[CheckpointTuple]:
"""List checkpoints from the in-memory storage.
@@ -422,7 +422,7 @@ class InMemorySaver(
if k[0] == thread_id:
del self.blobs[k]
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
async def aget_tuple(self, config: RunnableConfig) -> CheckpointTuple | None:
"""Asynchronous version of get_tuple.
This method is an asynchronous wrapper around get_tuple that runs the synchronous
@@ -438,11 +438,11 @@ class InMemorySaver(
async def alist(
self,
config: Optional[RunnableConfig],
config: RunnableConfig | None,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[CheckpointTuple]:
"""Asynchronous version of list.
@@ -512,7 +512,7 @@ class InMemorySaver(
"""
return self.delete_thread(thread_id)
def get_next_version(self, current: Optional[str]) -> str:
def get_next_version(self, current: str | None) -> str:
if current is None:
current_v = 0
elif isinstance(current, int):
@@ -571,7 +571,7 @@ class PersistentDict(defaultdict):
self.sync()
self.clear()
def __enter__(self) -> "PersistentDict":
def __enter__(self) -> PersistentDict:
return self
def __exit__(self, *exc_info: Any) -> None:
@@ -1,3 +1,5 @@
from __future__ import annotations
import dataclasses
import decimal
import importlib
@@ -5,6 +7,7 @@ import json
import pathlib
import pickle
import re
import sys
from collections import deque
from collections.abc import Sequence
from datetime import date, datetime, time, timedelta, timezone
@@ -18,7 +21,7 @@ from ipaddress import (
IPv6Interface,
IPv6Network,
)
from typing import Any, Callable, Optional, Union, cast
from typing import Any, Callable, cast
from uuid import UUID
from zoneinfo import ZoneInfo
@@ -41,7 +44,7 @@ class JsonPlusSerializer(SerializerProtocol):
self,
*,
pickle_fallback: bool = False,
__unpack_ext_hook__: Optional[Callable[[int, bytes], Any]] = None,
__unpack_ext_hook__: Callable[[int, bytes], Any] | None = None,
) -> None:
self.pickle_fallback = pickle_fallback
self._unpack_ext_hook = (
@@ -52,11 +55,11 @@ class JsonPlusSerializer(SerializerProtocol):
def _encode_constructor_args(
self,
constructor: Union[Callable, type[Any]],
constructor: Callable | type[Any],
*,
method: Union[None, str, Sequence[Union[None, str]]] = None,
args: Optional[Sequence[Any]] = None,
kwargs: Optional[dict[str, Any]] = None,
method: None | str | Sequence[None | str] = None,
args: Sequence[Any] | None = None,
kwargs: dict[str, Any] | None = None,
) -> dict[str, Any]:
out = {
"lc": 2,
@@ -71,7 +74,7 @@ class JsonPlusSerializer(SerializerProtocol):
out["kwargs"] = kwargs
return out
def _default(self, obj: Any) -> Union[str, dict[str, Any]]:
def _default(self, obj: Any) -> str | dict[str, Any]:
if isinstance(obj, Serializable):
return cast(dict[str, Any], obj.to_json())
elif hasattr(obj, "model_dump") and callable(obj.model_dump):
@@ -249,9 +252,10 @@ EXT_CONSTRUCTOR_KW_ARGS = 2
EXT_METHOD_SINGLE_ARG = 3
EXT_PYDANTIC_V1 = 4
EXT_PYDANTIC_V2 = 5
EXT_NUMPY_ARRAY = 6
def _msgpack_default(obj: Any) -> Union[str, ormsgpack.Ext]:
def _msgpack_default(obj: Any) -> str | ormsgpack.Ext:
if hasattr(obj, "model_dump") and callable(obj.model_dump): # pydantic v2
return ormsgpack.Ext(
EXT_PYDANTIC_V2,
@@ -318,13 +322,6 @@ def _msgpack_default(obj: Any) -> Union[str, ormsgpack.Ext]:
(obj.__class__.__module__, obj.__class__.__name__, obj.hex),
),
)
elif isinstance(obj, bytearray):
return ormsgpack.Ext(
EXT_CONSTRUCTOR_SINGLE_ARG,
_msgpack_enc(
(obj.__class__.__module__, obj.__class__.__name__, bytes(obj)),
),
)
elif isinstance(obj, decimal.Decimal):
return ormsgpack.Ext(
EXT_CONSTRUCTOR_SINGLE_ARG,
@@ -463,6 +460,21 @@ def _msgpack_default(obj: Any) -> Union[str, ormsgpack.Ext]:
),
),
)
elif (np_mod := sys.modules.get("numpy")) is not None and isinstance(
obj, np_mod.ndarray
):
order = "F" if obj.flags.f_contiguous and not obj.flags.c_contiguous else "C"
if obj.flags.c_contiguous:
mv = memoryview(obj)
try:
meta = (obj.dtype.str, obj.shape, order, mv)
return ormsgpack.Ext(EXT_NUMPY_ARRAY, _msgpack_enc(meta))
finally:
mv.release()
else:
buf = obj.tobytes(order="A")
meta = (obj.dtype.str, obj.shape, order, buf)
return ormsgpack.Ext(EXT_NUMPY_ARRAY, _msgpack_enc(meta))
elif isinstance(obj, BaseException):
return repr(obj)
else:
@@ -544,6 +556,17 @@ def _msgpack_ext_hook(code: int, data: bytes) -> Any:
return tup[2]
except NameError:
return
elif code == EXT_NUMPY_ARRAY:
try:
import numpy as _np
dtype_str, shape, order, buf = ormsgpack.unpackb(
data, ext_hook=_msgpack_ext_hook, option=ormsgpack.OPT_NON_STR_KEYS
)
arr = _np.frombuffer(buf, dtype=_np.dtype(dtype_str))
return arr.reshape(shape, order=order)
except Exception:
return
def _msgpack_ext_hook_to_json(code: int, data: bytes) -> Any:
@@ -624,6 +647,19 @@ def _msgpack_ext_hook_to_json(code: int, data: bytes) -> Any:
return tup[2]
except Exception:
return
elif code == EXT_NUMPY_ARRAY:
try:
import numpy as _np
dtype_str, shape, order, buf = ormsgpack.unpackb(
data,
ext_hook=_msgpack_ext_hook_to_json,
option=ormsgpack.OPT_NON_STR_KEYS,
)
arr = _np.frombuffer(buf, dtype=_np.dtype(dtype_str))
return arr.reshape(shape, order=order).tolist()
except Exception:
return
_option = (
@@ -9,6 +9,8 @@ Core types:
- Op: Get/Put/Search/List operations
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Iterable
from datetime import datetime
@@ -16,7 +18,6 @@ from typing import (
Any,
Literal,
NamedTuple,
Optional,
TypedDict,
Union,
cast,
@@ -127,7 +128,7 @@ class SearchItem(Item):
value: dict[str, Any],
created_at: datetime,
updated_at: datetime,
score: Optional[float] = None,
score: float | None = None,
) -> None:
"""Initialize a result item.
@@ -242,7 +243,7 @@ class SearchOp(NamedTuple):
```
"""
filter: Optional[dict[str, Any]] = None
filter: dict[str, Any] | None = None
"""Key-value pairs for filtering results based on exact matches or comparison operators.
The filter supports both exact matches and operator-based comparisons.
@@ -284,7 +285,7 @@ class SearchOp(NamedTuple):
offset: int = 0
"""Number of matching items to skip for pagination."""
query: Optional[str] = None
query: str | None = None
"""Natural language search query for semantic search capabilities.
???+ example "Examples"
@@ -379,7 +380,7 @@ class ListNamespacesOp(NamedTuple):
"""
match_conditions: Optional[tuple[MatchCondition, ...]] = None
match_conditions: tuple[MatchCondition, ...] | None = None
"""Optional conditions for filtering namespaces.
???+ example "Examples"
@@ -397,7 +398,7 @@ class ListNamespacesOp(NamedTuple):
```
"""
max_depth: Optional[int] = None
max_depth: int | None = None
"""Maximum depth of namespace hierarchy to return.
Note:
@@ -452,7 +453,7 @@ class PutOp(NamedTuple):
the full path would effectively be "documents/user123/report1"
"""
value: Optional[dict[str, Any]]
value: dict[str, Any] | None
"""The data to store, or None to mark the item for deletion.
The value must be a dictionary with string keys and JSON-serializable values.
@@ -466,7 +467,7 @@ class PutOp(NamedTuple):
}
"""
index: Optional[Union[Literal[False], list[str]]] = None # type: ignore[assignment]
index: Literal[False] | list[str] | None = None # type: ignore[assignment]
"""Controls how the item's fields are indexed for search operations.
Indexing configuration determines how the item can be found through search:
@@ -501,7 +502,7 @@ class PutOp(NamedTuple):
]
```
"""
ttl: Optional[float] = None
ttl: float | None = None
"""Controls the TTL (time-to-live) for the item in minutes.
If provided, and if the store you are using supports this feature, the item
@@ -530,14 +531,14 @@ class TTLConfig(TypedDict, total=False):
This can be overridden per-operation by explicitly setting refresh_ttl.
Defaults to True if not configured.
"""
default_ttl: Optional[float]
default_ttl: float | None
"""Default TTL (time-to-live) in minutes for new items.
If provided, new items will expire after this many minutes after their last access.
The expiration timer refreshes on both read and write operations.
Defaults to None (no expiration).
"""
sweep_interval_minutes: Optional[int]
sweep_interval_minutes: int | None
"""Interval in minutes between TTL sweep operations.
If provided, the store will periodically delete expired items based on TTL.
@@ -565,7 +566,7 @@ class IndexConfig(TypedDict, total=False):
- cohere:embed-multilingual-light-v3.0: 384
"""
embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc, str]
embed: Embeddings | EmbeddingsFunc | AEmbeddingsFunc | str
"""Optional function to generate embeddings from text.
Can be specified in three ways:
@@ -633,7 +634,7 @@ class IndexConfig(TypedDict, total=False):
```
"""
fields: Optional[list[str]]
fields: list[str] | None
"""Fields to extract text from for embedding generation.
Controls which parts of stored items are embedded for semantic search. Follows JSON path syntax:
@@ -690,7 +691,7 @@ class BaseStore(ABC):
"""
supports_ttl: bool = False
ttl_config: Optional[TTLConfig] = None
ttl_config: TTLConfig | None = None
__slots__ = ("__weakref__",)
@@ -723,8 +724,8 @@ class BaseStore(ABC):
namespace: tuple[str, ...],
key: str,
*,
refresh_ttl: Optional[bool] = None,
) -> Optional[Item]:
refresh_ttl: bool | None = None,
) -> Item | None:
"""Retrieve a single item.
Args:
@@ -746,11 +747,11 @@ class BaseStore(ABC):
namespace_prefix: tuple[str, ...],
/,
*,
query: Optional[str] = None,
filter: Optional[dict[str, Any]] = None,
query: str | None = None,
filter: dict[str, Any] | None = None,
limit: int = 10,
offset: int = 0,
refresh_ttl: Optional[bool] = None,
refresh_ttl: bool | None = None,
) -> list[SearchItem]:
"""Search for items within a namespace prefix.
@@ -817,9 +818,9 @@ class BaseStore(ABC):
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
index: Literal[False] | list[str] | None = None,
*,
ttl: Union[Optional[float], "NotProvided"] = NOT_PROVIDED,
ttl: float | None | NotProvided = NOT_PROVIDED,
) -> None:
"""Store or update an item in the store.
@@ -901,9 +902,9 @@ class BaseStore(ABC):
def list_namespaces(
self,
*,
prefix: Optional[NamespacePath] = None,
suffix: Optional[NamespacePath] = None,
max_depth: Optional[int] = None,
prefix: NamespacePath | None = None,
suffix: NamespacePath | None = None,
max_depth: int | None = None,
limit: int = 100,
offset: int = 0,
) -> list[tuple[str, ...]]:
@@ -956,8 +957,8 @@ class BaseStore(ABC):
namespace: tuple[str, ...],
key: str,
*,
refresh_ttl: Optional[bool] = None,
) -> Optional[Item]:
refresh_ttl: bool | None = None,
) -> Item | None:
"""Asynchronously retrieve a single item.
Args:
@@ -984,11 +985,11 @@ class BaseStore(ABC):
namespace_prefix: tuple[str, ...],
/,
*,
query: Optional[str] = None,
filter: Optional[dict[str, Any]] = None,
query: str | None = None,
filter: dict[str, Any] | None = None,
limit: int = 10,
offset: int = 0,
refresh_ttl: Optional[bool] = None,
refresh_ttl: bool | None = None,
) -> list[SearchItem]:
"""Asynchronously search for items within a namespace prefix.
@@ -1058,9 +1059,9 @@ class BaseStore(ABC):
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
index: Literal[False] | list[str] | None = None,
*,
ttl: Union[Optional[float], "NotProvided"] = NOT_PROVIDED,
ttl: float | None | NotProvided = NOT_PROVIDED,
) -> None:
"""Asynchronously store or update an item in the store.
@@ -1150,9 +1151,9 @@ class BaseStore(ABC):
async def alist_namespaces(
self,
*,
prefix: Optional[NamespacePath] = None,
suffix: Optional[NamespacePath] = None,
max_depth: Optional[int] = None,
prefix: NamespacePath | None = None,
suffix: NamespacePath | None = None,
max_depth: int | None = None,
limit: int = 100,
offset: int = 0,
) -> list[tuple[str, ...]]:
@@ -1226,7 +1227,7 @@ def _validate_namespace(namespace: tuple[str, ...]) -> None:
def _ensure_refresh(
ttl_config: Optional[TTLConfig], refresh_ttl: Optional[bool] = None
ttl_config: TTLConfig | None, refresh_ttl: bool | None = None
) -> bool:
if refresh_ttl is not None:
return refresh_ttl
@@ -1236,9 +1237,9 @@ def _ensure_refresh(
def _ensure_ttl(
ttl_config: Optional[TTLConfig],
ttl: Union[Optional[float], "NotProvided"] = NOT_PROVIDED,
) -> Optional[float]:
ttl_config: TTLConfig | None,
ttl: float | None | NotProvided = NOT_PROVIDED,
) -> float | None:
if ttl is NOT_PROVIDED:
if ttl_config:
return ttl_config.get("default_ttl")
+25 -23
View File
@@ -1,10 +1,12 @@
"""Utilities for batching operations in a background task."""
from __future__ import annotations
import asyncio
import functools
import weakref
from collections.abc import Iterable
from typing import Any, Callable, Literal, Optional, TypeVar, Union
from typing import Any, Callable, Literal, TypeVar
from langgraph.store.base import (
NOT_PROVIDED,
@@ -30,7 +32,7 @@ F = TypeVar("F", bound=Callable)
def _check_loop(func: F) -> F:
@functools.wraps(func)
def wrapper(store: "AsyncBatchedBaseStore", *args: Any, **kwargs: Any) -> Any:
def wrapper(store: AsyncBatchedBaseStore, *args: Any, **kwargs: Any) -> Any:
method_name: str = func.__name__
try:
current_loop = asyncio.get_running_loop()
@@ -75,8 +77,8 @@ class AsyncBatchedBaseStore(BaseStore):
namespace: tuple[str, ...],
key: str,
*,
refresh_ttl: Optional[bool] = None,
) -> Optional[Item]:
refresh_ttl: bool | None = None,
) -> Item | None:
assert not self._task.done()
fut = self._loop.create_future()
self._aqueue.put_nowait(
@@ -96,11 +98,11 @@ class AsyncBatchedBaseStore(BaseStore):
namespace_prefix: tuple[str, ...],
/,
*,
query: Optional[str] = None,
filter: Optional[dict[str, Any]] = None,
query: str | None = None,
filter: dict[str, Any] | None = None,
limit: int = 10,
offset: int = 0,
refresh_ttl: Optional[bool] = None,
refresh_ttl: bool | None = None,
) -> list[SearchItem]:
assert not self._task.done()
fut = self._loop.create_future()
@@ -124,9 +126,9 @@ class AsyncBatchedBaseStore(BaseStore):
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
index: Literal[False] | list[str] | None = None,
*,
ttl: Union[Optional[float], "NotProvided"] = NOT_PROVIDED,
ttl: float | None | NotProvided = NOT_PROVIDED,
) -> None:
assert not self._task.done()
_validate_namespace(namespace)
@@ -154,9 +156,9 @@ class AsyncBatchedBaseStore(BaseStore):
async def alist_namespaces(
self,
*,
prefix: Optional[NamespacePath] = None,
suffix: Optional[NamespacePath] = None,
max_depth: Optional[int] = None,
prefix: NamespacePath | None = None,
suffix: NamespacePath | None = None,
max_depth: int | None = None,
limit: int = 100,
offset: int = 0,
) -> list[tuple[str, ...]]:
@@ -187,8 +189,8 @@ class AsyncBatchedBaseStore(BaseStore):
namespace: tuple[str, ...],
key: str,
*,
refresh_ttl: Optional[bool] = None,
) -> Optional[Item]:
refresh_ttl: bool | None = None,
) -> Item | None:
return asyncio.run_coroutine_threadsafe(
self.aget(namespace, key=key, refresh_ttl=refresh_ttl), self._loop
).result()
@@ -199,11 +201,11 @@ class AsyncBatchedBaseStore(BaseStore):
namespace_prefix: tuple[str, ...],
/,
*,
query: Optional[str] = None,
filter: Optional[dict[str, Any]] = None,
query: str | None = None,
filter: dict[str, Any] | None = None,
limit: int = 10,
offset: int = 0,
refresh_ttl: Optional[bool] = None,
refresh_ttl: bool | None = None,
) -> list[SearchItem]:
return asyncio.run_coroutine_threadsafe(
self.asearch(
@@ -223,9 +225,9 @@ class AsyncBatchedBaseStore(BaseStore):
namespace: tuple[str, ...],
key: str,
value: dict[str, Any],
index: Optional[Union[Literal[False], list[str]]] = None,
index: Literal[False] | list[str] | None = None,
*,
ttl: Union[Optional[float], "NotProvided"] = NOT_PROVIDED,
ttl: float | None | NotProvided = NOT_PROVIDED,
) -> None:
_validate_namespace(namespace)
asyncio.run_coroutine_threadsafe(
@@ -253,9 +255,9 @@ class AsyncBatchedBaseStore(BaseStore):
def list_namespaces(
self,
*,
prefix: Optional[NamespacePath] = None,
suffix: Optional[NamespacePath] = None,
max_depth: Optional[int] = None,
prefix: NamespacePath | None = None,
suffix: NamespacePath | None = None,
max_depth: int | None = None,
limit: int = 100,
offset: int = 0,
) -> list[tuple[str, ...]]:
@@ -271,7 +273,7 @@ class AsyncBatchedBaseStore(BaseStore):
).result()
def _dedupe_ops(values: list[Op]) -> tuple[Optional[list[int]], list[Op]]:
def _dedupe_ops(values: list[Op]) -> tuple[list[int] | None, list[Op]]:
"""Dedupe operations while preserving order for results.
Args:
@@ -6,11 +6,13 @@ with LangChain-compatible tools while maintaining support for both synchronous a
asynchronous operations.
"""
from __future__ import annotations
import asyncio
import functools
import json
from collections.abc import Awaitable, Sequence
from typing import Any, Callable, Optional, Union
from typing import Any, Callable
from langchain_core.embeddings import Embeddings
@@ -30,7 +32,7 @@ Similar to EmbeddingsFunc, but returns an awaitable that resolves to the embeddi
def ensure_embeddings(
embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc, str, None],
embed: Embeddings | EmbeddingsFunc | AEmbeddingsFunc | str | None,
) -> Embeddings:
"""Ensure that an embedding function conforms to LangChain's Embeddings interface.
@@ -141,7 +143,7 @@ class EmbeddingsLambda(Embeddings):
def __init__(
self,
func: Union[EmbeddingsFunc, AEmbeddingsFunc],
func: EmbeddingsFunc | AEmbeddingsFunc,
) -> None:
if func is None:
raise ValueError("func must be provided")
@@ -221,7 +223,7 @@ class EmbeddingsLambda(Embeddings):
return (await afunc([text]))[0]
def get_text_at_path(obj: Any, path: Union[str, list[str]]) -> list[str]:
def get_text_at_path(obj: Any, path: str | list[str]) -> list[str]:
"""Extract text from an object using a path expression or pre-tokenized path.
Args:
@@ -279,7 +281,7 @@ def get_text_at_path(obj: Any, path: Union[str, list[str]]) -> list[str]:
for field in fields:
nested_tokens = tokenize_path(field)
if nested_tokens:
current_obj: Optional[dict] = obj
current_obj: dict | None = obj
for nested_token in nested_tokens:
if (
isinstance(current_obj, dict)
@@ -404,7 +406,7 @@ def _is_async_callable(
@functools.lru_cache
def _get_init_embeddings() -> Optional[Callable[[str], Embeddings]]:
def _get_init_embeddings() -> Callable[[str], Embeddings] | None:
try:
from langchain.embeddings import init_embeddings # type: ignore
@@ -99,6 +99,8 @@ Tip:
```
"""
from __future__ import annotations
import asyncio
import concurrent.futures as cf
import functools
@@ -107,7 +109,7 @@ from collections import defaultdict
from collections.abc import Iterable
from datetime import datetime, timezone
from importlib import util
from typing import Any, Optional
from typing import Any
from langchain_core.embeddings import Embeddings
@@ -178,7 +180,7 @@ class InMemoryStore(BaseStore):
"embeddings",
)
def __init__(self, *, index: Optional[IndexConfig] = None) -> None:
def __init__(self, *, index: IndexConfig | None = None) -> None:
# Both _data and _vectors are wrapped in the In-memory API
# Do not change their names
self._data: dict[tuple[str, ...], dict[str, Item]] = defaultdict(dict)
@@ -189,7 +191,7 @@ class InMemoryStore(BaseStore):
self.index_config = index
if self.index_config:
self.index_config = self.index_config.copy()
self.embeddings: Optional[Embeddings] = ensure_embeddings(
self.embeddings: Embeddings | None = ensure_embeddings(
self.index_config.get("embed"),
)
self.index_config["__tokenized_fields"] = [
@@ -325,7 +327,7 @@ class InMemoryStore(BaseStore):
)
# max pooling
seen: set[tuple[tuple[str, ...], str]] = set()
kept: list[tuple[Optional[float], Item]] = []
kept: list[tuple[float | None, Item]] = []
for score, item in sorted_results:
key = (item.namespace, item.key)
if key in seen:
@@ -494,7 +496,7 @@ def _cosine_similarity(X: list[float], Y: list[list[float]]) -> list[float]:
if not Y:
return []
if _check_numpy():
import numpy as np # type: ignore[import-not-found]
import numpy as np
X_arr = np.array(X) if not isinstance(X, np.ndarray) else X
Y_arr = np.array(Y) if not isinstance(Y, np.ndarray) else Y
+3 -2
View File
@@ -13,7 +13,7 @@ license = "MIT"
license-files = ['LICENSE']
dependencies = [
"langchain-core>=0.2.38",
"ormsgpack>=1.8.0",
"ormsgpack>=1.10.0",
]
[project.urls]
@@ -29,6 +29,7 @@ dev = [
"pytest-watcher",
"mypy",
"dataclasses-json",
"numpy",
]
[tool.hatch.build.targets.wheel]
@@ -46,7 +47,7 @@ lint.select = [
"B", # flake8-bugbear
"I", # isort
]
lint.ignore = ["E501", "B008", "UP007", "UP006"]
lint.ignore = ["E501", "B008"]
[tool.pytest-watcher]
now = true
+6 -4
View File
@@ -1,13 +1,15 @@
from __future__ import annotations
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import Any, Optional, Protocol
from typing import Any, Protocol
from langgraph.checkpoint.base import Checkpoint, EmptyChannelError
from langgraph.checkpoint.base.id import uuid6
class ChannelProtocol(Protocol):
def checkpoint(self) -> Optional[Any]: ...
def checkpoint(self) -> Any | None: ...
def empty_checkpoint() -> Checkpoint:
@@ -23,10 +25,10 @@ def empty_checkpoint() -> Checkpoint:
def create_checkpoint(
checkpoint: Checkpoint,
channels: Optional[Mapping[str, ChannelProtocol]],
channels: Mapping[str, ChannelProtocol] | None,
step: int,
*,
id: Optional[str] = None,
id: str | None = None,
) -> Checkpoint:
"""Create a checkpoint for the given channels."""
ts = datetime.now(timezone.utc).isoformat()
+37
View File
@@ -11,6 +11,8 @@ from ipaddress import IPv4Address
from zoneinfo import ZoneInfo
import dataclasses_json
import numpy as np
import pytest
from pydantic import BaseModel, SecretStr
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic.v1 import SecretStr as SecretStrV1
@@ -295,6 +297,41 @@ def test_serde_jsonplus_bytearray() -> None:
assert serde.loads_typed(dumped) == some_bytearray
@pytest.mark.parametrize(
"arr",
[
np.arange(9, dtype=np.int32).reshape(3, 3),
np.asfortranarray(np.arange(9, dtype=np.float64).reshape(3, 3)),
np.arange(12, dtype=np.int16)[::2].reshape(3, 2),
],
)
def test_serde_jsonplus_numpy_array(arr: np.ndarray) -> None:
serde = JsonPlusSerializer()
dumped = serde.dumps_typed(arr)
assert dumped[0] == "msgpack"
result = serde.loads_typed(dumped)
assert isinstance(result, np.ndarray)
assert result.dtype == arr.dtype
assert np.array_equal(result, arr)
@pytest.mark.parametrize(
"arr",
[
np.arange(6, dtype=np.float32).reshape(2, 3),
np.asfortranarray(np.arange(4, dtype=np.complex128).reshape(2, 2)),
],
)
def test_serde_jsonplus_numpy_array_json_hook(arr: np.ndarray) -> None:
serde = JsonPlusSerializer(__unpack_ext_hook__=_msgpack_ext_hook_to_json)
dumped = serde.dumps_typed(arr)
assert dumped[0] == "msgpack"
result = serde.loads_typed(dumped)
assert isinstance(result, list)
assert result == arr.tolist()
def test_loads_cannot_find() -> None:
serde = JsonPlusSerializer()
+838 -650
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -22,10 +22,10 @@ from langgraph.graph import END, StateGraph
from pydantic import BaseModel, Field
from typing_extensions import TypedDict
fast_llm = ChatOpenAI(model="gpt-3.5-turbo")
fast_llm = ChatOpenAI(model="gpt-4o-mini")
# Uncomment for a Fireworks model
# fast_llm = ChatFireworks(model="accounts/fireworks/models/firefunction-v1", max_tokens=32_000)
long_context_llm = ChatOpenAI(model="gpt-4-turbo-preview")
long_context_llm = ChatOpenAI(model="gpt-4o")
direct_gen_outline_prompt = ChatPromptTemplate.from_messages(
@@ -144,7 +144,7 @@ gen_perspectives_prompt = ChatPromptTemplate.from_messages(
)
gen_perspectives_chain = gen_perspectives_prompt | ChatOpenAI(
model="gpt-3.5-turbo"
model="gpt-4o-mini"
).with_structured_output(Perspectives)
@@ -270,7 +270,7 @@ gen_queries_prompt = ChatPromptTemplate.from_messages(
]
)
gen_queries_chain = gen_queries_prompt | ChatOpenAI(
model="gpt-3.5-turbo"
model="gpt-4o-mini"
).with_structured_output(Queries, include_raw=True)
+34 -6
View File
@@ -1,6 +1,7 @@
import json
import os
import pathlib
import re
import textwrap
from collections import Counter
from typing import Any, Literal, NamedTuple, Optional, TypedDict, Union
@@ -461,7 +462,7 @@ class Config(TypedDict, total=False):
PIP_CLEANUP_LINES = """# -- Ensure user deps didn't inadvertently overwrite langgraph-api
RUN mkdir -p /api/langgraph_api /api/langgraph_runtime /api/langgraph_license && \
touch /api/langgraph_api/__init__.py /api/langgraph_runtime/__init__.py /api/langgraph_license/__init__.py
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir --no-deps -e /api
RUN PYTHONDONTWRITEBYTECODE=1 {install_cmd} --no-cache-dir --no-deps -e /api
# -- End of ensuring user deps didn't inadvertently overwrite langgraph-api --
# -- Removing pip from the final image ~<:===~~~ --
RUN pip uninstall -y pip setuptools wheel && \
@@ -470,6 +471,7 @@ RUN pip uninstall -y pip setuptools wheel && \
# pip removal for wolfi
RUN rm -rf /usr/lib/python*/site-packages/pip* /usr/lib/python*/site-packages/setuptools* /usr/lib/python*/site-packages/wheel* && \
find /usr/bin -name "pip*" -delete || true
{uv_removal}
# -- End of pip removal --"""
@@ -1089,16 +1091,38 @@ def _get_node_pm_install_cmd(config_path: pathlib.Path, config: Config) -> str:
return install_cmd
semver_pattern = re.compile(r":(\d+(?:\.\d+)?(?:\.\d+)?)(?:-|$)")
def _image_supports_uv(base_image: str) -> bool:
if base_image == "langchain/langgraph-trial":
return False
match = semver_pattern.search(base_image)
if not match:
# Default image (langchain/langgraph-api) supports it.
return True
version_str = match.group(1)
version = tuple(map(int, version_str.split(".")))
min_uv = (0, 2, 47)
return version >= min_uv
def python_config_to_docker(
config_path: pathlib.Path,
config: Config,
base_image: str,
) -> tuple[str, dict[str, str]]:
"""Generate a Dockerfile from the configuration."""
if _image_supports_uv(base_image):
install_cmd = "uv pip install --system"
uv_removal = "RUN uv pip uninstall --system pip setuptools wheel && rm /usr/bin/uv /usr/bin/uvx"
else:
install_cmd = "pip install"
uv_removal = ""
# configure pip
pip_install = (
"PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt"
)
pip_install = f"PYTHONDONTWRITEBYTECODE=1 {install_cmd} --no-cache-dir -c /api/constraints.txt"
if config.get("pip_config_file"):
pip_install = f"PIP_CONFIG_FILE=/pipconfig.txt {pip_install}"
pip_config_file_str = (
@@ -1151,7 +1175,10 @@ RUN set -ex && \\
'name = "{fullpath.name}"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_{fullpath.name}/pyproject.toml; \\
done
# -- End of non-package dependency {fullpath.name} --"""
@@ -1240,7 +1267,8 @@ ADD {relpath} /deps/{name}
"",
js_inst_str,
"",
PIP_CLEANUP_LINES, # Add pip cleanup after all installations are complete
# Add pip cleanup after all installations are complete
PIP_CLEANUP_LINES.format(install_cmd=install_cmd, uv_removal=uv_removal),
"",
f"WORKDIR {local_deps.working_dir}" if local_deps.working_dir else "",
]
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "langgraph-cli"
version = "0.2.12"
version = "0.3.1"
description = "CLI for interacting with LangGraph API"
authors = []
requires-python = ">=3.9"
+6 -2
View File
@@ -14,6 +14,10 @@ from langgraph_cli.config import PIP_CLEANUP_LINES, Config, validate_config
from langgraph_cli.docker import DEFAULT_POSTGRES_URI, DockerCapabilities, Version
from langgraph_cli.util import clean_empty_lines
FORMATTED_CLEANUP_LINES = PIP_CLEANUP_LINES.format(
install_cmd="uv pip install --system",
uv_removal="RUN uv pip uninstall --system pip setuptools wheel && rm /usr/bin/uv /usr/bin/uvx",
)
DEFAULT_DOCKER_CAPABILITIES = DockerCapabilities(
version_docker=Version(26, 1, 1),
version_compose=Version(2, 27, 0),
@@ -144,10 +148,10 @@ services:
COPY --from=cli_1 . /deps/cli_1
# -- End of local package ../../.. --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/cli
develop:
+90 -43
View File
@@ -17,6 +17,11 @@ from langgraph_cli.config import (
)
from langgraph_cli.util import clean_empty_lines
FORMATTED_CLEANUP_LINES = PIP_CLEANUP_LINES.format(
install_cmd="uv pip install --system",
uv_removal="RUN uv pip uninstall --system pip setuptools wheel && rm /usr/bin/uv /usr/bin/uvx",
)
PATH_TO_CONFIG = pathlib.Path(__file__).parent / "test_config.json"
@@ -345,7 +350,7 @@ def test_config_to_docker_simple():
FROM langchain/langgraph-api:3.11
# -- Installing local requirements --
COPY --from=__outer_requirements.txt requirements.txt /deps/__outer_graphs_reqs_a/graphs_reqs_a/requirements.txt
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -r /deps/__outer_graphs_reqs_a/graphs_reqs_a/requirements.txt
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -r /deps/__outer_graphs_reqs_a/graphs_reqs_a/requirements.txt
# -- End of local requirements install --
# -- Adding local package ../../examples --
COPY --from=examples . /deps/examples
@@ -357,7 +362,10 @@ RUN set -ex && \\
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
@@ -368,16 +376,19 @@ RUN set -ex && \\
'name = "graphs_reqs_a"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_graphs_reqs_a/pyproject.toml; \\
done
# -- End of non-package dependency graphs_reqs_a --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGGRAPH_HTTP='{{"app": "/deps/examples/my_app.py:app"}}'
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{PIP_CLEANUP_LINES}
{FORMATTED_CLEANUP_LINES}
WORKDIR /deps/__outer_unit_tests/unit_tests\
"""
assert clean_empty_lines(actual_docker_stdin) == expected_docker_stdin
@@ -407,7 +418,10 @@ RUN set -ex && \\
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
@@ -418,16 +432,19 @@ RUN set -ex && \\
'name = "tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_tests/pyproject.toml; \\
done
# -- End of non-package dependency tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}'
"""
+ PIP_CLEANUP_LINES
+ FORMATTED_CLEANUP_LINES
+ """
WORKDIR /deps/__outer_unit_tests/unit_tests\
"""
@@ -462,16 +479,19 @@ RUN set -ex && \\
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}'
"""
+ PIP_CLEANUP_LINES
+ FORMATTED_CLEANUP_LINES
+ """
WORKDIR /deps/__outer_unit_tests/unit_tests\
"""
@@ -521,15 +541,18 @@ RUN set -ex && \\
'name = "graphs"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_graphs/pyproject.toml; \\
done
# -- End of non-package dependency graphs --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_graphs/src/agent.py:graph"}}'
{PIP_CLEANUP_LINES}\
{FORMATTED_CLEANUP_LINES}\
"""
assert clean_empty_lines(actual_docker_stdin) == expected_docker_stdin
assert additional_contexts == {}
@@ -562,11 +585,11 @@ dependencies = ["langchain"]"""
ADD . /deps/unit_tests
# -- End of local package . --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{"agent": "/deps/unit_tests/graphs/agent.py:graph"}'
"""
+ PIP_CLEANUP_LINES
+ FORMATTED_CLEANUP_LINES
+ "\n"
+ "WORKDIR /deps/unit_tests"
""
@@ -594,7 +617,7 @@ def test_config_to_docker_end_to_end():
ARG meow
ARG foo
ADD pipconfig.txt /pipconfig.txt
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt langchain langchain_openai
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt langchain langchain_openai
# -- Adding non-package dependency graphs --
ADD ./graphs/ /deps/__outer_graphs/src
RUN set -ex && \\
@@ -602,15 +625,18 @@ RUN set -ex && \\
'name = "graphs"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_graphs/pyproject.toml; \\
done
# -- End of non-package dependency graphs --
# -- Installing all local dependencies --
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PIP_CONFIG_FILE=/pipconfig.txt PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_graphs/src/agent.py:graph"}}'
{PIP_CLEANUP_LINES}"""
{FORMATTED_CLEANUP_LINES}"""
assert clean_empty_lines(actual_docker_stdin) == expected_docker_stdin
assert additional_contexts == {}
@@ -705,12 +731,15 @@ RUN set -ex && \\
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGGRAPH_UI='{{"agent": "./graphs/agent.ui.jsx"}}'
ENV LANGGRAPH_UI_CONFIG='{{"shared": ["nuqs"]}}'
@@ -719,7 +748,7 @@ ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:g
ENV NODE_VERSION=20
RUN cd /deps/__outer_unit_tests/unit_tests && npm i && tsx /api/langgraph_api/js/build.mts
# -- End of JS dependencies install --
{PIP_CLEANUP_LINES}
{FORMATTED_CLEANUP_LINES}
WORKDIR /deps/__outer_unit_tests/unit_tests"""
assert clean_empty_lines(actual_docker_stdin) == expected_docker_stdin
@@ -748,19 +777,22 @@ RUN set -ex && \\
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"python": "/deps/__outer_unit_tests/unit_tests/multiplatform/python.py:graph", "js": "/deps/__outer_unit_tests/unit_tests/multiplatform/js.mts:graph"}}'
# -- Installing JS dependencies --
ENV NODE_VERSION=22
RUN cd /deps/__outer_unit_tests/unit_tests && npm i && tsx /api/langgraph_api/js/build.mts
# -- End of JS dependencies install --
{PIP_CLEANUP_LINES}
{FORMATTED_CLEANUP_LINES}
WORKDIR /deps/__outer_unit_tests/unit_tests"""
assert clean_empty_lines(actual_docker_stdin) == expected_docker_stdin
@@ -770,7 +802,7 @@ WORKDIR /deps/__outer_unit_tests/unit_tests"""
# config_to_compose
def test_config_to_compose_simple_config():
graphs = {"agent": "./agent.py:graph"}
# Create a properly indented version of PIP_CLEANUP_LINES for compose files
# Create a properly indented version of FORMATTED_CLEANUP_LINES for compose files
expected_compose_stdin = f"""
pull_policy: build
build:
@@ -784,15 +816,18 @@ def test_config_to_compose_simple_config():
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/__outer_unit_tests/unit_tests
"""
actual_compose_stdin = config_to_compose(
@@ -822,15 +857,18 @@ def test_config_to_compose_env_vars():
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/__outer_unit_tests/unit_tests
"""
openai_api_key = "key"
@@ -864,15 +902,18 @@ def test_config_to_compose_env_file():
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/__outer_unit_tests/unit_tests
"""
actual_compose_stdin = config_to_compose(
@@ -899,15 +940,18 @@ def test_config_to_compose_watch():
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/__outer_unit_tests/unit_tests
develop:
@@ -943,15 +987,18 @@ def test_config_to_compose_end_to_end():
'name = "unit_tests"' \\
'version = "0.1"' \\
'[tool.setuptools.package-data]' \\
'"*" = ["**/*"]'; do \\
'"*" = ["**/*"]' \\
'[build-system]' \\
'requires = ["setuptools>=61"]' \\
'build-backend = "setuptools.build_meta"'; do \\
echo "$line" >> /deps/__outer_unit_tests/pyproject.toml; \\
done
# -- End of non-package dependency unit_tests --
# -- Installing all local dependencies --
RUN PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -c /api/constraints.txt -e /deps/*
RUN PYTHONDONTWRITEBYTECODE=1 uv pip install --system --no-cache-dir -c /api/constraints.txt -e /deps/*
# -- End of local dependencies install --
ENV LANGSERVE_GRAPHS='{{"agent": "/deps/__outer_unit_tests/unit_tests/agent.py:graph"}}'
{textwrap.indent(textwrap.dedent(PIP_CLEANUP_LINES), " ")}
{textwrap.indent(textwrap.dedent(FORMATTED_CLEANUP_LINES), " ")}
WORKDIR /deps/__outer_unit_tests/unit_tests
develop:
+1 -1
View File
@@ -501,7 +501,7 @@ wheels = [
[[package]]
name = "langgraph-cli"
version = "0.2.12"
version = "0.3.1"
source = { editable = "." }
dependencies = [
{ name = "click" },
-1
View File
@@ -12,7 +12,6 @@
[![Downloads](https://static.pepy.tech/badge/langgraph/month)](https://pepy.tech/project/langgraph)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langgraph)](https://github.com/langchain-ai/langgraph/issues)
[![Docs](https://img.shields.io/badge/docs-latest-blue)](https://langchain-ai.github.io/langgraph/)
[![GitMCP](https://img.shields.io/endpoint?url=https://gitmcp.io/badge/langchain-ai/langgraph)](https://gitmcp.io/langchain-ai/langgraph)
Trusted by companies shaping the future of agents including Klarna, Replit, Elastic, and more LangGraph is a low-level orchestration framework for building, managing, and deploying long-running, stateful agents.
+2 -2
View File
@@ -37,7 +37,7 @@ def fanout_to_subgraph() -> StateGraph:
return END if state["jokes"][0].endswith(" a" * 10) else "bump"
# subgraph
subgraph = StateGraph(JokeState, input=JokeInput, output=JokeOutput)
subgraph = StateGraph(JokeState, input_schema=JokeInput, output_schema=JokeOutput)
subgraph.add_node("edit", edit)
subgraph.add_node("generate", generate)
subgraph.add_node("bump", bump)
@@ -87,7 +87,7 @@ def fanout_to_subgraph_sync() -> StateGraph:
return END if state["jokes"][0].endswith(" a" * 10) else "bump"
# subgraph
subgraph = StateGraph(JokeState, input=JokeInput, output=JokeOutput)
subgraph = StateGraph(JokeState, input_schema=JokeInput, output_schema=JokeOutput)
subgraph.add_node("edit", edit)
subgraph.add_node("generate", generate)
subgraph.add_node("bump", bump)
+54
View File
@@ -0,0 +1,54 @@
"""Private typing utilities for LangGraph."""
from __future__ import annotations
from dataclasses import Field
from typing import Any, ClassVar, Protocol, Union
from pydantic import BaseModel
from typing_extensions import TypeAlias, TypedDict
class TypedDictLikeV1(Protocol):
"""Protocol to represent types that behave like TypedDicts
Version 1: using `ClassVar` for keys."""
__required_keys__: ClassVar[frozenset[str]]
__optional_keys__: ClassVar[frozenset[str]]
class TypedDictLikeV2(Protocol):
"""Protocol to represent types that behave like TypedDicts
Version 2: not using `ClassVar` for keys."""
__required_keys__: frozenset[str]
__optional_keys__: frozenset[str]
class DataclassLike(Protocol):
"""Protocol to represent types that behave like dataclasses.
Inspired by the private _DataclassT from dataclasses that uses a similar protocol as a bound."""
__dataclass_fields__: ClassVar[dict[str, Field[Any]]]
StateLike: TypeAlias = Union[TypedDictLikeV1, TypedDictLikeV2, DataclassLike, BaseModel]
"""Type alias for state-like types.
It can either be a `TypedDict`, `dataclass`, or Pydantic `BaseModel`.
Note: we cannot use either `TypedDict` or `dataclass` directly due to limitations in type checking.
"""
class Unset:
"""A sentinel value to represent an unset type."""
UNSET: Unset = Unset()
class DeprecatedKwargs(TypedDict):
"""TypedDict to use for extra keyword arguments, enabling type checking warnings for deprecated arguments."""
+4 -2
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
from collections.abc import Iterator, Sequence
from typing import Any, Generic, Union
@@ -8,7 +10,7 @@ from langgraph.constants import MISSING
from langgraph.errors import EmptyChannelError
def flatten(values: Sequence[Union[Value, list[Value]]]) -> Iterator[Value]:
def flatten(values: Sequence[Value | list[Value]]) -> Iterator[Value]:
for value in values:
if isinstance(value, list):
yield from value
@@ -70,7 +72,7 @@ class Topic(
empty.values = checkpoint
return empty
def update(self, values: Sequence[Union[Value, list[Value]]]) -> bool:
def update(self, values: Sequence[Value | list[Value]]) -> bool:
updated = False
if not self.accumulate:
updated = bool(self.values)
+29 -34
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import concurrent.futures
import functools
@@ -9,9 +11,7 @@ from typing import (
Any,
Callable,
Generic,
Optional,
TypeVar,
Union,
get_args,
get_origin,
overload,
@@ -19,6 +19,7 @@ from typing import (
from typing_extensions import Unpack
from langgraph._typing import UNSET, DeprecatedKwargs
from langgraph.cache.base import BaseCache
from langgraph.channels.ephemeral_value import EphemeralValue
from langgraph.channels.last_value import LastValue
@@ -37,7 +38,6 @@ from langgraph.pregel.read import PregelNode
from langgraph.pregel.write import ChannelWrite, ChannelWriteEntry
from langgraph.store.base import BaseStore
from langgraph.types import _DC_KWARGS, CachePolicy, RetryPolicy, StreamMode
from langgraph.typing import DeprecatedKwargs
from langgraph.warnings import LangGraphDeprecatedSinceV10
@@ -47,8 +47,8 @@ class TaskFunction(Generic[P, T]):
func: Callable[P, T],
*,
retry_policy: Sequence[RetryPolicy],
cache_policy: Optional[CachePolicy[Callable[P, Union[str, bytes]]]] = None,
name: Optional[str] = None,
cache_policy: CachePolicy[Callable[P, str | bytes]] | None = None,
name: str | None = None,
) -> None:
if name is not None:
if hasattr(func, "__func__"):
@@ -91,36 +91,33 @@ class TaskFunction(Generic[P, T]):
@overload
def task(
*,
name: Optional[str] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
cache_policy: Optional[CachePolicy[Callable[P, Union[str, bytes]]]] = None,
name: str | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy[Callable[P, str | bytes]] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> Callable[
[Union[Callable[P, Awaitable[T]], Callable[P, T]]],
[Callable[P, Awaitable[T]] | Callable[P, T]],
TaskFunction[P, T],
]: ...
@overload
def task(
__func_or_none__: Union[Callable[P, Awaitable[T]], Callable[P, T]],
__func_or_none__: Callable[P, Awaitable[T]] | Callable[P, T],
) -> TaskFunction[P, T]: ...
def task(
__func_or_none__: Optional[Union[Callable[P, Awaitable[T]], Callable[P, T]]] = None,
__func_or_none__: Callable[P, Awaitable[T]] | Callable[P, T] | None = None,
*,
name: Optional[str] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
cache_policy: Optional[CachePolicy[Callable[P, Union[str, bytes]]]] = None,
name: str | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy[Callable[P, str | bytes]] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> Union[
Callable[
[Union[Callable[P, Awaitable[T]], Callable[P, T]]],
TaskFunction[P, T],
],
TaskFunction[P, T],
]:
) -> (
Callable[[Callable[P, Awaitable[T]] | Callable[P, T]], TaskFunction[P, T]]
| TaskFunction[P, T]
):
"""Define a LangGraph task using the `task` decorator.
!!! important "Requires python 3.11 or higher for async functions"
@@ -179,7 +176,7 @@ def task(
await add_one.ainvoke([1, 2, 3]) # Returns [2, 3, 4]
```
"""
if (retry := kwargs.get("retry")) is not None:
if (retry := kwargs.get("retry", UNSET)) is not UNSET:
warnings.warn(
"`retry` is deprecated and will be removed. Please use `retry_policy` instead.",
category=LangGraphDeprecatedSinceV10,
@@ -196,10 +193,8 @@ def task(
)
def decorator(
func: Union[Callable[P, Awaitable[T]], Callable[P, T]],
) -> Union[
Callable[P, concurrent.futures.Future[T]], Callable[P, asyncio.Future[T]]
]:
func: Callable[P, Awaitable[T]] | Callable[P, T],
) -> Callable[P, concurrent.futures.Future[T]] | Callable[P, asyncio.Future[T]]:
return TaskFunction(
func, retry_policy=retry_policies, cache_policy=cache_policy, name=name
)
@@ -376,16 +371,16 @@ class entrypoint:
def __init__(
self,
checkpointer: Optional[BaseCheckpointSaver] = None,
store: Optional[BaseStore] = None,
cache: Optional[BaseCache] = None,
config_schema: Optional[type[Any]] = None,
cache_policy: Optional[CachePolicy] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
checkpointer: BaseCheckpointSaver | None = None,
store: BaseStore | None = None,
cache: BaseCache | None = None,
config_schema: type[Any] | None = None,
cache_policy: CachePolicy | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> None:
"""Initialize the entrypoint decorator."""
if (retry := kwargs.get("retry")) is not None:
if (retry := kwargs.get("retry", UNSET)) is not UNSET:
warnings.warn(
"`retry` is deprecated and will be removed. Please use `retry_policy` instead.",
category=LangGraphDeprecatedSinceV10,
@@ -504,7 +499,7 @@ class entrypoint:
func.__name__: PregelNode(
bound=bound,
triggers=[START],
channels=[START],
channels=START,
writers=[
ChannelWrite(
[
+24 -26
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
from collections.abc import Awaitable, Hashable, Sequence
from inspect import (
isfunction,
@@ -11,7 +13,6 @@ from typing import (
Callable,
Literal,
NamedTuple,
Optional,
Union,
cast,
get_args,
@@ -40,21 +41,18 @@ Writer = Callable[
def _get_branch_path_input_schema(
path: Union[
Callable[..., Union[Hashable, list[Hashable]]],
Callable[..., Awaitable[Union[Hashable, list[Hashable]]]],
Runnable[Any, Union[Hashable, list[Hashable]]],
],
) -> Optional[type[Any]]:
path: Callable[..., Hashable | list[Hashable]]
| Callable[..., Awaitable[Hashable | list[Hashable]]]
| Runnable[Any, Hashable | list[Hashable]],
) -> type[Any] | None:
input = None
# detect input schema annotation in the branch callable
try:
callable_: Optional[
Union[
Callable[..., Union[Hashable, list[Hashable]]],
Callable[..., Awaitable[Union[Hashable, list[Hashable]]]],
]
] = None
callable_: (
Callable[..., Hashable | list[Hashable]]
| Callable[..., Awaitable[Hashable | list[Hashable]]]
| None
) = None
if isinstance(path, (RunnableCallable, RunnableLambda)):
if isfunction(path.func) or ismethod(path.func):
callable_ = path.func
@@ -85,19 +83,19 @@ def _get_branch_path_input_schema(
class Branch(NamedTuple):
path: Runnable[Any, Union[Hashable, list[Hashable]]]
ends: Optional[dict[Hashable, str]]
input_schema: Optional[type[Any]] = None
path: Runnable[Any, Hashable | list[Hashable]]
ends: dict[Hashable, str] | None
input_schema: type[Any] | None = None
@classmethod
def from_path(
cls,
path: Runnable[Any, Union[Hashable, list[Hashable]]],
path_map: Optional[Union[dict[Hashable, str], list[str]]],
path: Runnable[Any, Hashable | list[Hashable]],
path_map: dict[Hashable, str] | list[str] | None,
infer_schema: bool = False,
) -> "Branch":
) -> Branch:
# coerce path_map to a dictionary
path_map_: Optional[dict[Hashable, str]] = None
path_map_: dict[Hashable, str] | None = None
try:
if isinstance(path_map, dict):
path_map_ = path_map.copy()
@@ -105,7 +103,7 @@ class Branch(NamedTuple):
path_map_ = {name: name for name in path_map}
else:
# find func
func: Optional[Callable] = None
func: Callable | None = None
if isinstance(path, (RunnableCallable, RunnableLambda)):
func = path.func or path.afunc
if func is not None:
@@ -126,7 +124,7 @@ class Branch(NamedTuple):
def run(
self,
writer: Writer,
reader: Optional[Callable[[RunnableConfig], Any]] = None,
reader: Callable[[RunnableConfig], Any] | None = None,
) -> RunnableCallable:
return ChannelWrite.register_writer(
RunnableCallable(
@@ -153,7 +151,7 @@ class Branch(NamedTuple):
input: Any,
config: RunnableConfig,
*,
reader: Optional[Callable[[RunnableConfig], Any]],
reader: Callable[[RunnableConfig], Any] | None,
writer: Writer,
) -> Runnable:
if reader:
@@ -176,7 +174,7 @@ class Branch(NamedTuple):
input: Any,
config: RunnableConfig,
*,
reader: Optional[Callable[[RunnableConfig], Any]],
reader: Callable[[RunnableConfig], Any] | None,
writer: Writer,
) -> Runnable:
if reader:
@@ -200,11 +198,11 @@ class Branch(NamedTuple):
input: Any,
result: Any,
config: RunnableConfig,
) -> Union[Runnable, Any]:
) -> Runnable | Any:
if not isinstance(result, (list, tuple)):
result = [result]
if self.ends:
destinations: Sequence[Union[Send, str]] = [
destinations: Sequence[Send | str] = [
r if isinstance(r, Send) else self.ends[r] for r in result
]
else:
+7 -6
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import uuid
import warnings
from collections.abc import Sequence
@@ -7,7 +9,6 @@ from typing import (
Any,
Callable,
Literal,
Optional,
Union,
cast,
)
@@ -32,8 +33,8 @@ REMOVE_ALL_MESSAGES = "__remove_all__"
def _add_messages_wrapper(func: Callable) -> Callable[[Messages, Messages], Messages]:
def _add_messages(
left: Optional[Messages] = None, right: Optional[Messages] = None, **kwargs: Any
) -> Union[Messages, Callable[[Messages, Messages], Messages]]:
left: Messages | None = None, right: Messages | None = None, **kwargs: Any
) -> Messages | Callable[[Messages, Messages], Messages]:
if left is not None and right is not None:
return func(left, right, **kwargs)
elif left is not None or right is not None:
@@ -54,7 +55,7 @@ def add_messages(
left: Messages,
right: Messages,
*,
format: Optional[Literal["langchain-openai"]] = None,
format: Literal["langchain-openai"] | None = None,
) -> Messages:
"""Merges two lists of messages, updating existing messages by ID.
@@ -246,9 +247,9 @@ def _format_messages(messages: Sequence[BaseMessage]) -> list[BaseMessage]:
def push_message(
message: Union[MessageLikeRepresentation, BaseMessageChunk],
message: MessageLikeRepresentation | BaseMessageChunk,
*,
state_key: Optional[str] = "messages",
state_key: str | None = "messages",
) -> AnyMessage:
"""Write a message manually to the `messages` / `messages-tuple` stream mode.
+152 -144
View File
@@ -15,7 +15,6 @@ from typing import (
Generic,
Literal,
NamedTuple,
Optional,
Protocol,
Union,
cast,
@@ -29,6 +28,7 @@ from langchain_core.runnables import Runnable, RunnableConfig
from pydantic import BaseModel
from typing_extensions import Self, TypeAlias, Unpack
from langgraph._typing import UNSET, DeprecatedKwargs
from langgraph.cache.base import BaseCache
from langgraph.channels.base import BaseChannel
from langgraph.channels.binop import BinaryOperatorAggregate
@@ -78,7 +78,7 @@ from langgraph.types import (
Send,
StreamWriter,
)
from langgraph.typing import DeprecatedKwargs, InputT, StateT, StateT_contra, Unset
from langgraph.typing import InputT, OutputT, StateT, StateT_contra
from langgraph.utils.fields import (
get_cached_annotated_keys,
get_field_default,
@@ -91,7 +91,7 @@ from langgraph.warnings import LangGraphDeprecatedSinceV10
logger = logging.getLogger(__name__)
def _warn_invalid_state_schema(schema: Union[type[Any], Any]) -> None:
def _warn_invalid_state_schema(schema: type[Any] | Any) -> None:
if isinstance(schema, type):
return
if typing.get_args(schema):
@@ -174,15 +174,17 @@ class StateNodeSpec(NamedTuple):
# TODO: rename this callable, also move away from NamedTuple so that we can use
# a generic StateNode, so maybe a dataclass
runnable: StateNode
metadata: Optional[dict[str, Any]]
metadata: dict[str, Any] | None
# TODO: rename to input_schema, though we really just want to modify this structure to
# be a dataclass
input: type[Any]
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]]
cache_policy: Optional[CachePolicy]
ends: Optional[Union[tuple[str, ...], dict[str, str]]] = EMPTY_SEQ
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None
cache_policy: CachePolicy | None
ends: tuple[str, ...] | dict[str, str] | None = EMPTY_SEQ
defer: bool = False
class StateGraph(Generic[StateT, InputT]):
class StateGraph(Generic[StateT, InputT, OutputT]):
"""A graph whose nodes communicate by reading and writing to a shared state.
The signature of each node is State -> Partial<State>.
@@ -239,35 +241,58 @@ class StateGraph(Generic[StateT, InputT]):
branches: defaultdict[str, dict[str, Branch]]
channels: dict[str, BaseChannel]
managed: dict[str, ManagedValueSpec]
schemas: dict[type[Any], dict[str, Union[BaseChannel, ManagedValueSpec]]]
schemas: dict[type[Any], dict[str, BaseChannel | ManagedValueSpec]]
waiting_edges: set[tuple[tuple[str, ...], str]]
compiled: bool
state_schema: type[StateT]
input_schema: type[InputT]
output_schema: type[OutputT]
def __init__(
self,
state_schema: type[StateT],
config_schema: type[Any] | None = None,
*,
input: type[InputT] | None = None,
output: type[Any] | None = None,
input_schema: type[InputT] | None = None,
output_schema: type[OutputT] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> None:
input = input or state_schema
output = output or state_schema
if (input_ := kwargs.get("input", UNSET)) is not UNSET:
warnings.warn(
"`input` is deprecated and will be removed. Please use `input_schema` instead.",
category=LangGraphDeprecatedSinceV10,
stacklevel=2,
)
if input_schema is None:
input_schema = cast(Union[type[InputT], None], input_)
if (output := kwargs.get("output", UNSET)) is not UNSET:
warnings.warn(
"`output` is deprecated and will be removed. Please use `output_schema` instead.",
category=LangGraphDeprecatedSinceV10,
stacklevel=2,
)
if output_schema is None:
output_schema = cast(Union[type[OutputT], None], output)
self.nodes = {}
self.edges = set[tuple[str, str]]()
self.edges = set()
self.branches = defaultdict(dict)
self.support_multiple_edges = False
self.compiled = False
self.schemas = {}
self.channels = {}
self.managed = {}
self.schema = state_schema
self.input = input
self.output = output
self._add_schema(state_schema)
self._add_schema(input, allow_managed=False)
self._add_schema(output, allow_managed=False)
self.compiled = False
self.waiting_edges = set()
self.state_schema = state_schema
self.input_schema = cast(type[InputT], input_schema or state_schema)
self.output_schema = cast(type[OutputT], output_schema or state_schema)
self.config_schema = config_schema
self.waiting_edges: set[tuple[tuple[str, ...], str]] = set()
self._add_schema(self.state_schema)
self._add_schema(self.input_schema, allow_managed=False)
self._add_schema(self.output_schema, allow_managed=False)
@property
def _all_edges(self) -> set[tuple[str, str]]:
@@ -313,11 +338,11 @@ class StateGraph(Generic[StateT, InputT]):
node: StateNode[StateT],
*,
defer: bool = False,
metadata: Optional[dict[str, Any]] = None,
input: Optional[type[Any]] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
cache_policy: Optional[CachePolicy] = None,
destinations: Optional[Union[dict[str, str], tuple[str, ...]]] = None,
metadata: dict[str, Any] | None = None,
input_schema: type[Any] | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
destinations: dict[str, str] | tuple[str, ...] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> Self:
"""Add a new node to the state graph.
@@ -332,11 +357,11 @@ class StateGraph(Generic[StateT, InputT]):
action: StateNode[StateT],
*,
defer: bool = False,
metadata: Optional[dict[str, Any]] = None,
input: Optional[type[Any]] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
cache_policy: Optional[CachePolicy] = None,
destinations: Optional[Union[dict[str, str], tuple[str, ...]]] = None,
metadata: dict[str, Any] | None = None,
input_schema: type[Any] | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
destinations: dict[str, str] | tuple[str, ...] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> Self:
"""Add a new node to the state graph."""
@@ -344,15 +369,15 @@ class StateGraph(Generic[StateT, InputT]):
def add_node(
self,
node: Union[str, StateNode[StateT]],
action: Optional[StateNode[StateT]] = None,
node: str | StateNode[StateT],
action: StateNode[StateT] | None = None,
*,
defer: bool = False,
metadata: Optional[dict[str, Any]] = None,
input: Optional[type[Any]] = None,
retry_policy: Optional[Union[RetryPolicy, Sequence[RetryPolicy]]] = None,
cache_policy: Optional[CachePolicy] = None,
destinations: Optional[Union[dict[str, str], tuple[str, ...]]] = None,
metadata: dict[str, Any] | None = None,
input_schema: type[Any] | None = None,
retry_policy: RetryPolicy | Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
destinations: dict[str, str] | tuple[str, ...] | None = None,
**kwargs: Unpack[DeprecatedKwargs],
) -> Self:
"""Add a new node to the state graph.
@@ -364,7 +389,7 @@ class StateGraph(Generic[StateT, InputT]):
Will be used as the node function or runnable if `node` is a string (node name).
defer: Whether to defer the execution of the node until the run is about to end.
metadata: The metadata associated with the node. (default: None)
input: The input schema for the node. (default: the graph's input schema)
input_schema: The input schema for the node. (default: the graph's state schema)
retry_policy: The retry policy for the node. (default: None)
If a sequence is provided, the first matching policy will be applied.
cache_policy: The cache policy for the node. (default: None)
@@ -376,12 +401,18 @@ class StateGraph(Generic[StateT, InputT]):
Example:
```python
from typing_extensions import TypedDict
from langchain_core.runnables import RunnableConfig
from langgraph.graph import START, StateGraph
def my_node(state, config):
class State(TypedDict):
x: int
def my_node(state: State, config: RunnableConfig) -> State:
return {"x": state["x"] + 1}
builder = StateGraph(dict)
builder = StateGraph(State)
builder.add_node(my_node) # node name will be 'my_node'
builder.add_edge(START, "my_node")
graph = builder.compile()
@@ -391,7 +422,7 @@ class StateGraph(Generic[StateT, InputT]):
Example: Customize the name:
```python
builder = StateGraph(dict)
builder = StateGraph(State)
builder.add_node("my_fair_node", my_node)
builder.add_edge(START, "my_fair_node")
graph = builder.compile()
@@ -402,7 +433,7 @@ class StateGraph(Generic[StateT, InputT]):
Returns:
Self: The instance of the state graph, allowing for method chaining.
"""
if (retry := kwargs.get("retry")) is not None:
if (retry := kwargs.get("retry", UNSET)) is not UNSET:
warnings.warn(
"`retry` is deprecated and will be removed. Please use `retry_policy` instead.",
category=LangGraphDeprecatedSinceV10,
@@ -410,6 +441,14 @@ class StateGraph(Generic[StateT, InputT]):
if retry_policy is None:
retry_policy = retry # type: ignore[assignment]
if (input_ := kwargs.get("input", UNSET)) is not UNSET:
warnings.warn(
"`input` is deprecated and will be removed. Please use `input_schema` instead.",
category=LangGraphDeprecatedSinceV10,
)
if input_schema is None:
input_schema = cast(Union[type[InputT], None], input_)
if not isinstance(node, str):
action = node
if isinstance(action, Runnable):
@@ -445,7 +484,7 @@ class StateGraph(Generic[StateT, InputT]):
f"'{character}' is a reserved character and is not allowed in the node names."
)
ends: Union[tuple[str, ...], dict[str, str]] = EMPTY_SEQ
ends: tuple[str, ...] | dict[str, str] = EMPTY_SEQ
try:
if (
isfunction(action)
@@ -455,7 +494,7 @@ class StateGraph(Generic[StateT, InputT]):
hints := get_type_hints(getattr(action, "__call__"))
or get_type_hints(action)
):
if input is None:
if input_schema is None:
first_parameter_name = next(
iter(
inspect.signature(
@@ -465,7 +504,7 @@ class StateGraph(Generic[StateT, InputT]):
)
if input_hint := hints.get(first_parameter_name):
if isinstance(input_hint, type) and get_type_hints(input_hint):
input = input_hint
input_schema = input_hint
if rtn := hints.get("return"):
# Handle Union types
rtn_origin = get_origin(rtn)
@@ -493,12 +532,12 @@ class StateGraph(Generic[StateT, InputT]):
if destinations is not None:
ends = destinations
if input is not None:
self._add_schema(input)
if input_schema is not None:
self._add_schema(input_schema)
self.nodes[node] = StateNodeSpec(
coerce_to_runnable(action, name=node, trace=False), # type: ignore
metadata,
input=input or self.schema,
input=input_schema or self.state_schema,
retry_policy=retry_policy,
cache_policy=cache_policy,
ends=ends,
@@ -506,7 +545,7 @@ class StateGraph(Generic[StateT, InputT]):
)
return self
def add_edge(self, start_key: Union[str, list[str]], end_key: str) -> Self:
def add_edge(self, start_key: str | list[str], end_key: str) -> Self:
"""Add a directed edge from the start node (or list of start nodes) to the end node.
When a single start node is provided, the graph will wait for that node to complete
@@ -563,12 +602,10 @@ class StateGraph(Generic[StateT, InputT]):
def add_conditional_edges(
self,
source: str,
path: Union[
Callable[..., Union[Hashable, list[Hashable]]],
Callable[..., Awaitable[Union[Hashable, list[Hashable]]]],
Runnable[Any, Union[Hashable, list[Hashable]]],
],
path_map: Optional[Union[dict[Hashable, str], list[str]]] = None,
path: Callable[..., Hashable | list[Hashable]]
| Callable[..., Awaitable[Hashable | list[Hashable]]]
| Runnable[Any, Hashable | list[Hashable]],
path_map: dict[Hashable, str] | list[str] | None = None,
) -> Self:
"""Add a conditional edge from the starting node to any number of destination nodes.
@@ -610,7 +647,7 @@ class StateGraph(Generic[StateT, InputT]):
def add_sequence(
self,
nodes: Sequence[Union[StateNode[StateT], tuple[str, StateNode[StateT]]]],
nodes: Sequence[StateNode[StateT] | tuple[str, StateNode[StateT]]],
) -> Self:
"""Add a sequence of nodes that will be executed in the provided order.
@@ -629,7 +666,7 @@ class StateGraph(Generic[StateT, InputT]):
if len(nodes) < 1:
raise ValueError("Sequence requires at least one node.")
previous_name: Optional[str] = None
previous_name: str | None = None
for node in nodes:
if isinstance(node, tuple) and len(node) == 2:
name, node = node
@@ -665,12 +702,10 @@ class StateGraph(Generic[StateT, InputT]):
def set_conditional_entry_point(
self,
path: Union[
Callable[..., Union[Hashable, list[Hashable]]],
Callable[..., Awaitable[Union[Hashable, list[Hashable]]]],
Runnable[Any, Union[Hashable, list[Hashable]]],
],
path_map: Optional[Union[dict[Hashable, str], list[str]]] = None,
path: Callable[..., Hashable | list[Hashable]]
| Callable[..., Awaitable[Hashable | list[Hashable]]]
| Runnable[Any, Hashable | list[Hashable]],
path_map: dict[Hashable, str] | list[str] | None = None,
) -> Self:
"""Sets a conditional entry point in the graph.
@@ -699,7 +734,7 @@ class StateGraph(Generic[StateT, InputT]):
"""
return self.add_edge(key, END)
def validate(self, interrupt: Optional[Sequence[str]] = None) -> Self:
def validate(self, interrupt: Sequence[str] | None = None) -> Self:
# assemble sources
all_sources = {src for src, _ in self._all_edges}
for start, branches in self.branches.items():
@@ -748,43 +783,17 @@ class StateGraph(Generic[StateT, InputT]):
self.compiled = True
return self
@overload
def compile(
self: StateGraph[StateT, Unset],
checkpointer: Checkpointer = None,
*,
cache: Optional[BaseCache] = None,
store: Optional[BaseStore] = None,
interrupt_before: Optional[Union[All, list[str]]] = None,
interrupt_after: Optional[Union[All, list[str]]] = None,
debug: bool = False,
name: Optional[str] = None,
) -> CompiledStateGraph[StateT, StateT]: ...
@overload
def compile(
self: StateGraph[StateT, InputT],
checkpointer: Checkpointer = None,
*,
cache: Optional[BaseCache] = None,
store: Optional[BaseStore] = None,
interrupt_before: Optional[Union[All, list[str]]] = None,
interrupt_after: Optional[Union[All, list[str]]] = None,
debug: bool = False,
name: Optional[str] = None,
) -> CompiledStateGraph[StateT, InputT]: ...
def compile(
self,
checkpointer: Checkpointer = None,
*,
cache: Optional[BaseCache] = None,
store: Optional[BaseStore] = None,
interrupt_before: Optional[Union[All, list[str]]] = None,
interrupt_after: Optional[Union[All, list[str]]] = None,
cache: BaseCache | None = None,
store: BaseStore | None = None,
interrupt_before: All | list[str] | None = None,
interrupt_after: All | list[str] | None = None,
debug: bool = False,
name: Optional[str] = None,
) -> Union[CompiledStateGraph[StateT, StateT], CompiledStateGraph[StateT, InputT]]:
name: str | None = None,
) -> CompiledStateGraph[StateT, InputT]:
"""Compiles the state graph into a `CompiledStateGraph` object.
The compiled graph implements the `Runnable` interface and can be invoked,
@@ -820,11 +829,11 @@ class StateGraph(Generic[StateT, InputT]):
# prepare output channels
output_channels = (
"__root__"
if len(self.schemas[self.output]) == 1
and "__root__" in self.schemas[self.output]
if len(self.schemas[self.output_schema]) == 1
and "__root__" in self.schemas[self.output_schema]
else [
key
for key, val in self.schemas[self.output].items()
for key, val in self.schemas[self.output_schema].items()
if not is_managed_value(val)
]
)
@@ -836,23 +845,22 @@ class StateGraph(Generic[StateT, InputT]):
]
)
ResolvedInputT: Union[type[InputT], type[StateT]] = self.input or self.schema
compiled = CompiledStateGraph[StateT, ResolvedInputT]( # type: ignore[valid-type]
compiled = CompiledStateGraph[StateT, InputT, OutputT](
builder=self,
schema_to_mapper={},
config_type=self.config_schema,
input_model=(
self.input
self.input_schema
if len(self.channels) > 1
and isclass(self.input)
and issubclass(self.input, BaseModel)
and isclass(self.input_schema)
and issubclass(self.input_schema, BaseModel)
else None
),
nodes={},
channels={
**self.channels,
**self.managed,
START: EphemeralValue(self.input),
START: EphemeralValue(self.input_schema),
},
input_channels=START,
stream_mode="updates",
@@ -885,46 +893,46 @@ class StateGraph(Generic[StateT, InputT]):
return compiled.validate()
class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
builder: StateGraph[StateT, InputT]
schema_to_mapper: dict[type[Any], Optional[Callable[[Any], Any]]]
class CompiledStateGraph(
Pregel[StateT, InputT, OutputT], Generic[StateT, InputT, OutputT]
):
builder: StateGraph[StateT, InputT, OutputT]
schema_to_mapper: dict[type[Any], Callable[[Any], Any] | None]
def __init__(
self,
*,
builder: StateGraph[StateT, InputT],
schema_to_mapper: dict[type[Any], Optional[Callable[[Any], Any]]],
builder: StateGraph[StateT, InputT, OutputT],
schema_to_mapper: dict[type[Any], Callable[[Any], Any] | None],
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
self.builder = builder
self.schema_to_mapper = schema_to_mapper
def get_input_schema(
self, config: Optional[RunnableConfig] = None
) -> type[BaseModel]:
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
return _get_schema(
typ=self.builder.input,
typ=self.builder.input_schema,
schemas=self.builder.schemas,
channels=self.builder.channels,
name=self.get_name("Input"),
)
def get_output_schema(
self, config: Optional[RunnableConfig] = None
self, config: RunnableConfig | None = None
) -> type[BaseModel]:
return _get_schema(
typ=self.builder.output,
typ=self.builder.output_schema,
schemas=self.builder.schemas,
channels=self.builder.channels,
name=self.get_name("Output"),
)
def attach_node(self, key: str, node: Optional[StateNodeSpec]) -> None:
def attach_node(self, key: str, node: StateNodeSpec | None) -> None:
if key == START:
output_keys = [
k
for k, v in self.builder.schemas[self.builder.input].items()
for k, v in self.builder.schemas[self.builder.input_schema].items()
if not is_managed_value(v)
]
else:
@@ -933,8 +941,8 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
]
def _get_updates(
input: Union[None, dict, Any],
) -> Optional[Sequence[tuple[str, Any]]]:
input: None | dict | Any,
) -> Sequence[tuple[str, Any]] | None:
if input is None:
return None
elif isinstance(input, dict):
@@ -971,7 +979,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
raise InvalidUpdateError(msg)
# state updaters
write_entries: tuple[Union[ChannelWriteEntry, ChannelWriteTupleEntry], ...] = (
write_entries: tuple[ChannelWriteEntry | ChannelWriteTupleEntry, ...] = (
ChannelWriteTupleEntry(
mapper=_get_root if output_keys == ["__root__"] else _get_updates
),
@@ -988,18 +996,18 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
self.nodes[key] = PregelNode(
tags=[TAG_HIDDEN],
triggers=[START],
channels=[START],
channels=START,
writers=[ChannelWrite(write_entries)],
)
elif node is not None:
input_schema = node.input if node else self.builder.schema
input_values = {k: k for k in self.builder.schemas[input_schema]}
is_single_input = len(input_values) == 1 and "__root__" in input_values
input_schema = node.input if node else self.builder._state_schema
input_channels = list(self.builder.schemas[input_schema])
is_single_input = len(input_channels) == 1 and "__root__" in input_channels
if input_schema in self.schema_to_mapper:
mapper = self.schema_to_mapper[input_schema]
else:
mapper = _pick_mapper(
list(input_values),
input_channels,
input_schema,
)
self.schema_to_mapper[input_schema] = mapper
@@ -1013,7 +1021,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
self.nodes[key] = PregelNode(
triggers=[branch_channel],
# read state keys and managed values
channels=(list(input_values) if is_single_input else input_values),
channels=("__root__" if is_single_input else input_channels),
# coerce state dict to schema class (eg. pydantic model)
mapper=mapper,
# publish to state keys
@@ -1026,7 +1034,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
else:
raise RuntimeError
def attach_edge(self, starts: Union[str, Sequence[str]], end: str) -> None:
def attach_edge(self, starts: str | Sequence[str], end: str) -> None:
if isinstance(starts, str):
# subscribe to start channel
if end != END:
@@ -1056,8 +1064,8 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
self, start: str, name: str, branch: Branch, *, with_reader: bool = True
) -> None:
def get_writes(
packets: Sequence[Union[str, Send]], static: bool = False
) -> Sequence[Union[ChannelWriteEntry, Send]]:
packets: Sequence[str | Send], static: bool = False
) -> Sequence[ChannelWriteEntry | Send]:
writes = [
(
ChannelWriteEntry(
@@ -1078,7 +1086,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
schema = branch.input_schema or (
self.builder.nodes[start].input
if start in self.builder.nodes
else self.builder.schema
else self.builder.state_schema
)
channels = list(self.builder.schemas[schema])
# get mapper
@@ -1088,7 +1096,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
mapper = _pick_mapper(channels, schema)
self.schema_to_mapper[schema] = mapper
# create reader
reader: Optional[Callable[[RunnableConfig], Any]] = partial(
reader: Callable[[RunnableConfig], Any] | None = partial(
ChannelRead.do_read,
select=channels[0] if channels == ["__root__"] else channels,
fresh=True,
@@ -1208,7 +1216,7 @@ class CompiledStateGraph(Pregel[InputT], Generic[StateT, InputT]):
def _pick_mapper(
state_keys: Sequence[str], schema: type[Any]
) -> Optional[Callable[[Any], Any]]:
) -> Callable[[Any], Any] | None:
if state_keys == ["__root__"]:
return None
if isclass(schema) and issubclass(schema, dict):
@@ -1249,8 +1257,8 @@ def _control_branch(value: Any) -> Sequence[tuple[str, Any]]:
def _control_static(
ends: Union[tuple[str, ...], dict[str, str]],
) -> Sequence[tuple[str, Any, Optional[str]]]:
ends: tuple[str, ...] | dict[str, str],
) -> Sequence[tuple[str, Any, str | None]]:
if isinstance(ends, dict):
return [
(k if k == END else CHANNEL_BRANCH_TO.format(k), None, label)
@@ -1262,7 +1270,7 @@ def _control_static(
]
def _get_root(input: Any) -> Optional[Sequence[tuple[str, Any]]]:
def _get_root(input: Any) -> Sequence[tuple[str, Any]] | None:
if isinstance(input, Command):
if input.graph == Command.PARENT:
return ()
@@ -1317,12 +1325,12 @@ def _get_channel(
@overload
def _get_channel(
name: str, annotation: Any, *, allow_managed: Literal[True] = True
) -> Union[BaseChannel, ManagedValueSpec]: ...
) -> BaseChannel | ManagedValueSpec: ...
def _get_channel(
name: str, annotation: Any, *, allow_managed: bool = True
) -> Union[BaseChannel, ManagedValueSpec]:
) -> BaseChannel | ManagedValueSpec:
if manager := _is_field_managed_value(name, annotation):
if allow_managed:
return manager
@@ -1340,7 +1348,7 @@ def _get_channel(
return fallback
def _is_field_channel(typ: type[Any]) -> Optional[BaseChannel]:
def _is_field_channel(typ: type[Any]) -> BaseChannel | None:
if hasattr(typ, "__metadata__"):
meta = typ.__metadata__
if len(meta) >= 1 and isinstance(meta[-1], BaseChannel):
@@ -1350,7 +1358,7 @@ def _is_field_channel(typ: type[Any]) -> Optional[BaseChannel]:
return None
def _is_field_binop(typ: type[Any]) -> Optional[BinaryOperatorAggregate]:
def _is_field_binop(typ: type[Any]) -> BinaryOperatorAggregate | None:
if hasattr(typ, "__metadata__"):
meta = typ.__metadata__
if len(meta) >= 1 and callable(meta[-1]):
@@ -1371,7 +1379,7 @@ def _is_field_binop(typ: type[Any]) -> Optional[BinaryOperatorAggregate]:
return None
def _is_field_managed_value(name: str, typ: type[Any]) -> Optional[ManagedValueSpec]:
def _is_field_managed_value(name: str, typ: type[Any]) -> ManagedValueSpec | None:
if hasattr(typ, "__metadata__"):
meta = typ.__metadata__
if len(meta) >= 1:
+9 -7
View File
@@ -1,4 +1,6 @@
from typing import Any, Literal, Optional, Union, cast
from __future__ import annotations
from typing import Any, Literal, Union, cast
from uuid import uuid4
from langchain_core.messages import AnyMessage
@@ -51,10 +53,10 @@ def push_ui_message(
name: str,
props: dict[str, Any],
*,
id: Optional[str] = None,
metadata: Optional[dict[str, Any]] = None,
message: Optional[AnyMessage] = None,
state_key: Optional[str] = "ui",
id: str | None = None,
metadata: dict[str, Any] | None = None,
message: AnyMessage | None = None,
state_key: str | None = "ui",
merge: bool = False,
) -> UIMessage:
"""Push a new UI message to update the UI state.
@@ -149,8 +151,8 @@ def delete_ui_message(id: str, *, state_key: str = "ui") -> RemoveUIMessage:
def ui_message_reducer(
left: Union[list[AnyUIMessage], AnyUIMessage],
right: Union[list[AnyUIMessage], AnyUIMessage],
left: list[AnyUIMessage] | AnyUIMessage,
right: list[AnyUIMessage] | AnyUIMessage,
) -> list[AnyUIMessage]:
"""Merge two lists of UI messages, supporting removing UI messages.
+15 -18
View File
@@ -107,7 +107,7 @@ from langgraph.types import (
StreamChunk,
StreamMode,
)
from langgraph.typing import InputT
from langgraph.typing import InputT, OutputT, StateT
from langgraph.utils.config import (
ensure_config,
merge_configs,
@@ -145,7 +145,7 @@ class NodeBuilder:
"_cache_policy",
)
_channels: list[str] | dict[str, str]
_channels: str | list[str]
_triggers: list[str]
_tags: list[str]
_metadata: dict[str, Any]
@@ -157,7 +157,7 @@ class NodeBuilder:
def __init__(
self,
) -> None:
self._channels = {}
self._channels = []
self._triggers = []
self._tags = []
self._metadata = {}
@@ -171,10 +171,8 @@ class NodeBuilder:
channel: str,
) -> Self:
"""Subscribe to a single channel."""
if isinstance(self._channels, list):
self._channels.append(channel)
elif not self._channels:
self._channels = [channel]
if not self._channels:
self._channels = channel
else:
raise ValueError(
"Cannot subscribe to single channels when other channels are already subscribed to"
@@ -200,15 +198,15 @@ class NodeBuilder:
Returns:
Self for chaining
"""
if isinstance(self._channels, list):
if isinstance(self._channels, str):
raise ValueError(
"Cannot subscribe to channels when subscribed to a single channel"
)
if read:
if not self._channels:
self._channels = {chan: chan for chan in channels}
self._channels = list(channels)
else:
self._channels.update({chan: chan for chan in channels})
self._channels.extend(channels)
if isinstance(channels, str):
self._triggers.append(channels)
@@ -222,11 +220,10 @@ class NodeBuilder:
*channels: str,
) -> Self:
"""Adds the specified channels to read from, without subscribing to them."""
assert self._channels, "Channels must be specified first"
assert isinstance(self._channels, dict), (
assert isinstance(self._channels, list), (
"Cannot read additional channels when subscribed to single channels"
)
self._channels.update({c: c for c in channels})
self._channels.extend(channels)
return self
def do(
@@ -298,7 +295,7 @@ class NodeBuilder:
)
class Pregel(PregelProtocol[InputT], Generic[InputT]):
class Pregel(PregelProtocol[StateT, InputT, OutputT], Generic[StateT, InputT, OutputT]):
"""Pregel manages the runtime behavior for LangGraph applications.
## Overview
@@ -2766,8 +2763,8 @@ class Pregel(PregelProtocol[InputT], Generic[InputT]):
"""
output_keys = output_keys if output_keys is not None else self.output_channels
latest: Union[dict[str, Any], Any] = None
chunks: list[Union[dict[str, Any], Any]] = []
latest: dict[str, Any] | Any = None
chunks: list[dict[str, Any] | Any] = []
interrupts: list[Interrupt] = []
for chunk in self.stream(
@@ -2833,8 +2830,8 @@ class Pregel(PregelProtocol[InputT], Generic[InputT]):
output_keys = output_keys if output_keys is not None else self.output_channels
latest: Union[dict[str, Any], Any] = None
chunks: list[Union[dict[str, Any], Any]] = []
latest: dict[str, Any] | Any = None
chunks: list[dict[str, Any] | Any] = []
interrupts: list[Interrupt] = []
async for chunk in self.astream(
+61 -63
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import binascii
import itertools
import sys
@@ -14,7 +16,6 @@ from typing import (
NamedTuple,
Optional,
Protocol,
Union,
cast,
overload,
)
@@ -91,7 +92,7 @@ class WritesProtocol(Protocol):
Implemented by PregelTaskWrites and PregelExecutableTask."""
@property
def path(self) -> tuple[Union[str, int, tuple], ...]: ...
def path(self) -> tuple[str | int | tuple, ...]: ...
@property
def name(self) -> str: ...
@@ -107,7 +108,7 @@ class PregelTaskWrites(NamedTuple):
"""Simplest implementation of WritesProtocol, for usage with writes that
don't originate from a runnable task, eg. graph input, update_state, etc."""
path: tuple[Union[str, int, tuple], ...]
path: tuple[str | int | tuple, ...]
name: str
writes: Sequence[tuple[str, Any]]
triggers: Sequence[str]
@@ -118,8 +119,8 @@ class Call:
func: Callable
input: tuple[tuple[Any, ...], dict[str, Any]]
retry_policy: Optional[Sequence[RetryPolicy]]
cache_policy: Optional[CachePolicy]
retry_policy: Sequence[RetryPolicy] | None
cache_policy: CachePolicy | None
callbacks: Callbacks
def __init__(
@@ -127,8 +128,8 @@ class Call:
func: Callable,
input: tuple[tuple[Any, ...], dict[str, Any]],
*,
retry_policy: Optional[Sequence[RetryPolicy]],
cache_policy: Optional[CachePolicy],
retry_policy: Sequence[RetryPolicy] | None,
cache_policy: CachePolicy | None,
callbacks: Callbacks,
) -> None:
self.func = func
@@ -140,7 +141,7 @@ class Call:
def should_interrupt(
checkpoint: Checkpoint,
interrupt_nodes: Union[All, Sequence[str]],
interrupt_nodes: All | Sequence[str],
tasks: Iterable[PregelExecutableTask],
) -> list[PregelExecutableTask]:
"""Check if the graph should be interrupted based on current state."""
@@ -176,9 +177,9 @@ def local_read(
channels: Mapping[str, BaseChannel],
managed: ManagedValueMapping,
task: WritesProtocol,
select: Union[list[str], str],
select: list[str] | str,
fresh: bool = False,
) -> Union[dict[str, Any], Any]:
) -> dict[str, Any] | Any:
"""Function injected under CONFIG_KEY_READ in task config, to read current state.
Used by conditional edges to read a copy of the state with reflecting the writes
from that node only."""
@@ -213,7 +214,7 @@ def local_read(
return values
def increment(current: Optional[int]) -> int:
def increment(current: int | None) -> int:
"""Default channel versioning function, increments the current int version."""
return current + 1 if current is not None else 1
@@ -222,7 +223,7 @@ def apply_writes(
checkpoint: Checkpoint,
channels: Mapping[str, BaseChannel],
tasks: Iterable[WritesProtocol],
get_next_version: Optional[GetNextVersion],
get_next_version: GetNextVersion | None,
trigger_to_nodes: Mapping[str, Sequence[str]],
) -> set[str]:
"""Apply writes from a set of tasks (usually the tasks from a Pregel step)
@@ -338,8 +339,8 @@ def prepare_next_tasks(
store: Literal[None] = None,
checkpointer: Literal[None] = None,
manager: Literal[None] = None,
trigger_to_nodes: Optional[Mapping[str, Sequence[str]]] = None,
updated_channels: Optional[set[str]] = None,
trigger_to_nodes: Mapping[str, Sequence[str]] | None = None,
updated_channels: set[str] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Literal[None] = None,
) -> dict[str, PregelTask]: ...
@@ -357,13 +358,13 @@ def prepare_next_tasks(
stop: int,
*,
for_execution: Literal[True],
store: Optional[BaseStore],
checkpointer: Optional[BaseCheckpointSaver],
manager: Union[None, ParentRunManager, AsyncParentRunManager],
trigger_to_nodes: Optional[Mapping[str, Sequence[str]]] = None,
updated_channels: Optional[set[str]] = None,
store: BaseStore | None,
checkpointer: BaseCheckpointSaver | None,
manager: None | ParentRunManager | AsyncParentRunManager,
trigger_to_nodes: Mapping[str, Sequence[str]] | None = None,
updated_channels: set[str] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Optional[CachePolicy] = None,
cache_policy: CachePolicy | None = None,
) -> dict[str, PregelExecutableTask]: ...
@@ -378,14 +379,14 @@ def prepare_next_tasks(
stop: int,
*,
for_execution: bool,
store: Optional[BaseStore] = None,
checkpointer: Optional[BaseCheckpointSaver] = None,
manager: Union[None, ParentRunManager, AsyncParentRunManager] = None,
trigger_to_nodes: Optional[Mapping[str, Sequence[str]]] = None,
updated_channels: Optional[set[str]] = None,
store: BaseStore | None = None,
checkpointer: BaseCheckpointSaver | None = None,
manager: None | ParentRunManager | AsyncParentRunManager = None,
trigger_to_nodes: Mapping[str, Sequence[str]] | None = None,
updated_channels: set[str] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Optional[CachePolicy] = None,
) -> Union[dict[str, PregelTask], dict[str, PregelExecutableTask]]:
cache_policy: CachePolicy | None = None,
) -> dict[str, PregelTask] | dict[str, PregelExecutableTask]:
"""Prepare the set of tasks that will make up the next Pregel step.
Args:
@@ -415,7 +416,7 @@ def prepare_next_tasks(
input_cache: dict[INPUT_CACHE_KEY_TYPE, Any] = {}
checkpoint_id_bytes = binascii.unhexlify(checkpoint["id"].replace("-", ""))
null_version = checkpoint_null_version(checkpoint)
tasks: list[Union[PregelTask, PregelExecutableTask]] = []
tasks: list[PregelTask | PregelExecutableTask] = []
# Consume pending tasks
tasks_channel = cast(Optional[Topic[Send]], channels.get(TASKS))
if tasks_channel and tasks_channel.is_available():
@@ -496,11 +497,11 @@ PUSH_TRIGGER = (PUSH,)
def prepare_single_task(
task_path: tuple[Any, ...],
task_id_checksum: Optional[str],
task_id_checksum: str | None,
*,
checkpoint: Checkpoint,
checkpoint_id_bytes: bytes,
checkpoint_null_version: Optional[V],
checkpoint_null_version: V | None,
pending_writes: list[PendingWrite],
processes: Mapping[str, PregelNode],
channels: Mapping[str, BaseChannel],
@@ -509,13 +510,13 @@ def prepare_single_task(
step: int,
stop: int,
for_execution: bool,
store: Optional[BaseStore] = None,
checkpointer: Optional[BaseCheckpointSaver] = None,
manager: Union[None, ParentRunManager, AsyncParentRunManager] = None,
input_cache: Optional[dict[INPUT_CACHE_KEY_TYPE, Any]] = None,
cache_policy: Optional[CachePolicy] = None,
store: BaseStore | None = None,
checkpointer: BaseCheckpointSaver | None = None,
manager: None | ParentRunManager | AsyncParentRunManager = None,
input_cache: dict[INPUT_CACHE_KEY_TYPE, Any] | None = None,
cache_policy: CachePolicy | None = None,
retry_policy: Sequence[RetryPolicy] = (),
) -> Union[None, PregelTask, PregelExecutableTask]:
) -> None | PregelTask | PregelExecutableTask:
"""Prepares a single task for the next Pregel step, given a task path, which
uniquely identifies a PUSH or PULL task within the graph."""
configurable = config.get(CONF, {})
@@ -560,7 +561,7 @@ def prepare_single_task(
cache_policy = call.cache_policy or cache_policy
if cache_policy:
args_key = cache_policy.key_func(*call.input[0], **call.input[1])
cache_key: Optional[CacheKey] = CacheKey(
cache_key: CacheKey | None = CacheKey(
(
CACHE_NS_WRITES,
(identifier(call.func) or "__dynamic__"),
@@ -908,7 +909,7 @@ def prepare_single_task(
def checkpoint_null_version(
checkpoint: Checkpoint,
) -> Optional[V]:
) -> V | None:
"""Get the null version for the checkpoint, if available."""
for version in checkpoint["channel_versions"].values():
return type(version)()
@@ -918,29 +919,29 @@ def checkpoint_null_version(
def _triggers(
channels: Mapping[str, BaseChannel],
versions: ChannelVersions,
seen: Optional[ChannelVersions],
seen: ChannelVersions | None,
null_version: V,
proc: PregelNode,
) -> Sequence[str]:
) -> bool:
if seen is None:
for chan in proc.triggers:
if channels[chan].is_available():
return (chan,)
return True
else:
for chan in proc.triggers:
if channels[chan].is_available() and versions.get( # type: ignore[operator]
chan, null_version
) > seen.get(chan, null_version):
return (chan,)
return EMPTY_SEQ
return True
return False
def _scratchpad(
parent_scratchpad: Optional[PregelScratchpad],
parent_scratchpad: PregelScratchpad | None,
pending_writes: list[PendingWrite],
task_id: str,
namespace_hash: str,
resume_map: Optional[dict[str, Any]],
resume_map: dict[str, Any] | None,
step: int,
stop: int,
) -> PregelScratchpad:
@@ -1010,7 +1011,7 @@ def _proc_input(
*,
for_execution: bool,
scratchpad: PregelScratchpad,
input_cache: Optional[dict[INPUT_CACHE_KEY_TYPE, Any]],
input_cache: dict[INPUT_CACHE_KEY_TYPE, Any] | None,
) -> Any:
"""Prepare input for a PULL task, based on the process's channels and triggers."""
# if in cache return shallow copy
@@ -1018,25 +1019,22 @@ def _proc_input(
return copy(input_cache[proc.input_cache_key])
# If all trigger channels subscribed by this process are not empty
# then invoke the process with the values of all non-empty channels
if isinstance(proc.channels, dict):
if isinstance(proc.channels, list):
val: dict[str, Any] = {}
for k, chan in proc.channels.items():
if chan in channels:
if channels[chan].is_available():
val[k] = channels[chan].get()
else:
val[k] = managed[k].get(scratchpad)
elif isinstance(proc.channels, list):
for chan in proc.channels:
if chan in channels:
if channels[chan].is_available():
val = channels[chan].get()
break
val[chan] = channels[chan].get()
else:
val = managed[chan].get(scratchpad)
break
val[chan] = managed[chan].get(scratchpad)
elif isinstance(proc.channels, str):
if proc.channels in channels:
if channels[proc.channels].is_available():
val = channels[proc.channels].get()
else:
return MISSING
else:
return MISSING
val = managed[proc.channels].get(scratchpad)
else:
raise RuntimeError(
f"Invalid channels type, expected list or dict, got {proc.channels}"
@@ -1053,7 +1051,7 @@ def _proc_input(
return val
def _uuid5_str(namespace: bytes, *parts: Union[str, bytes]) -> str:
def _uuid5_str(namespace: bytes, *parts: str | bytes) -> str:
"""Generate a UUID from the SHA-1 hash of a namespace and str parts."""
sha = sha1(namespace, usedforsecurity=False)
@@ -1062,7 +1060,7 @@ def _uuid5_str(namespace: bytes, *parts: Union[str, bytes]) -> str:
return f"{hex[:8]}-{hex[8:12]}-{hex[12:16]}-{hex[16:20]}-{hex[20:32]}"
def _xxhash_str(namespace: bytes, *parts: Union[str, bytes]) -> str:
def _xxhash_str(namespace: bytes, *parts: str | bytes) -> str:
"""Generate a UUID from the XXH3 hash of a namespace and str parts."""
hex = xxh3_128_hexdigest(
namespace + b"".join(p.encode() if isinstance(p, str) else p for p in parts)
@@ -1070,7 +1068,7 @@ def _xxhash_str(namespace: bytes, *parts: Union[str, bytes]) -> str:
return f"{hex[:8]}-{hex[8:12]}-{hex[12:16]}-{hex[16:20]}-{hex[20:32]}"
def task_path_str(tup: Union[str, int, tuple]) -> str:
def task_path_str(tup: str | int | tuple) -> str:
"""Generate a string representation of the task path."""
return (
f"~{', '.join(task_path_str(x) for x in tup)}"
@@ -1087,7 +1085,7 @@ LAZY_ATOMIC_COUNTER_LOCK = threading.Lock()
class LazyAtomicCounter:
__slots__ = ("_counter",)
_counter: Optional[Callable[[], int]]
_counter: Callable[[], int] | None
def __init__(self) -> None:
self._counter = None
+9 -7
View File
@@ -1,12 +1,14 @@
"""Utility to convert a user provided function into a Runnable with a ChannelWrite."""
from __future__ import annotations
import concurrent.futures
import functools
import inspect
import sys
import types
from collections.abc import Generator, Sequence
from typing import Any, Callable, Generic, Optional, TypeVar, cast
from typing import Any, Callable, Generic, TypeVar, cast
from langchain_core.runnables import Runnable
from typing_extensions import ParamSpec
@@ -40,7 +42,7 @@ def _getattribute(obj: Any, name: str) -> Any:
return obj, parent
def _whichmodule(obj: Any, name: str) -> Optional[str]:
def _whichmodule(obj: Any, name: str) -> str | None:
"""Find the module an object belongs to.
This function differs from ``pickle.whichmodule`` in two ways:
@@ -74,7 +76,7 @@ def _whichmodule(obj: Any, name: str) -> Optional[str]:
return None
def identifier(obj: Any, name: Optional[str] = None) -> Optional[str]:
def identifier(obj: Any, name: str | None = None) -> str | None:
"""Return the module and name of an object."""
from langgraph.pregel.read import PregelNode
from langgraph.utils.runnable import RunnableCallable, RunnableSeq
@@ -104,8 +106,8 @@ def identifier(obj: Any, name: Optional[str] = None) -> Optional[str]:
def _lookup_module_and_qualname(
obj: Any, name: Optional[str] = None
) -> Optional[tuple[types.ModuleType, str]]:
obj: Any, name: str | None = None
) -> tuple[types.ModuleType, str] | None:
if name is None:
name = getattr(obj, "__qualname__", None)
if name is None: # pragma: no cover
@@ -251,8 +253,8 @@ class SyncAsyncFuture(Generic[T], concurrent.futures.Future[T]):
def call(
func: Callable[P, T],
*args: Any,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
cache_policy: Optional[CachePolicy] = None,
retry_policy: Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
**kwargs: Any,
) -> SyncAsyncFuture[T]:
config = get_config()
@@ -1,6 +1,7 @@
from __future__ import annotations
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import Optional, Union
from langgraph.channels.base import BaseChannel
from langgraph.checkpoint.base import Checkpoint
@@ -24,10 +25,10 @@ def empty_checkpoint() -> Checkpoint:
def create_checkpoint(
checkpoint: Checkpoint,
channels: Optional[Mapping[str, BaseChannel]],
channels: Mapping[str, BaseChannel] | None,
step: int,
*,
id: Optional[str] = None,
id: str | None = None,
) -> Checkpoint:
"""Create a checkpoint for the given channels."""
ts = datetime.now(timezone.utc).isoformat()
@@ -52,7 +53,7 @@ def create_checkpoint(
def channels_from_checkpoint(
specs: Mapping[str, Union[BaseChannel, ManagedValueSpec]],
specs: Mapping[str, BaseChannel | ManagedValueSpec],
checkpoint: Checkpoint,
) -> tuple[Mapping[str, BaseChannel], ManagedValueMapping]:
"""Get channels from a checkpoint."""
+17 -16
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
from collections import defaultdict
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import asdict
@@ -6,7 +8,6 @@ from pprint import pformat
from typing import (
Any,
Literal,
Optional,
Union,
)
from uuid import UUID
@@ -43,7 +44,7 @@ class TaskPayload(TypedDict):
class TaskResultPayload(TypedDict):
id: str
name: str
error: Optional[str]
error: str | None
interrupts: list[dict]
result: list[tuple[str, Any]]
@@ -51,17 +52,17 @@ class TaskResultPayload(TypedDict):
class CheckpointTask(TypedDict):
id: str
name: str
error: Optional[str]
error: str | None
interrupts: list[dict]
state: Optional[RunnableConfig]
state: RunnableConfig | None
class CheckpointPayload(TypedDict):
config: Optional[RunnableConfig]
config: RunnableConfig | None
metadata: CheckpointMetadata
values: dict[str, Any]
next: list[str]
parent_config: Optional[RunnableConfig]
parent_config: RunnableConfig | None
tasks: list[CheckpointTask]
@@ -116,7 +117,7 @@ def map_debug_tasks(
def map_debug_task_results(
step: int,
task_tup: tuple[PregelExecutableTask, Sequence[tuple[str, Any]]],
stream_keys: Union[str, Sequence[str]],
stream_keys: str | Sequence[str],
) -> Iterator[DebugOutputTaskResult]:
"""Produce "task_result" events for stream_mode=debug."""
stream_channels_list = (
@@ -144,7 +145,7 @@ def map_debug_task_results(
}
def rm_pregel_keys(config: Optional[RunnableConfig]) -> Optional[RunnableConfig]:
def rm_pregel_keys(config: RunnableConfig | None) -> RunnableConfig | None:
"""Remove pregel-specific keys from the config."""
if config is None:
return config
@@ -161,18 +162,18 @@ def map_debug_checkpoint(
step: int,
config: RunnableConfig,
channels: Mapping[str, BaseChannel],
stream_channels: Union[str, Sequence[str]],
stream_channels: str | Sequence[str],
metadata: CheckpointMetadata,
checkpoint: Checkpoint,
tasks: Iterable[PregelExecutableTask],
pending_writes: list[PendingWrite],
parent_config: Optional[RunnableConfig],
output_keys: Union[str, Sequence[str]],
parent_config: RunnableConfig | None,
output_keys: str | Sequence[str],
) -> Iterator[DebugOutputCheckpoint]:
"""Produce "checkpoint" events for stream_mode=debug."""
parent_ns = config[CONF].get(CONFIG_KEY_CHECKPOINT_NS, "")
task_states: dict[str, Union[RunnableConfig, StateSnapshot]] = {}
task_states: dict[str, RunnableConfig | StateSnapshot] = {}
for task in tasks:
if not task.subgraphs:
@@ -278,10 +279,10 @@ def print_step_checkpoint(
def tasks_w_writes(
tasks: Iterable[Union[PregelTask, PregelExecutableTask]],
pending_writes: Optional[list[PendingWrite]],
states: Optional[dict[str, Union[RunnableConfig, StateSnapshot]]],
output_keys: Union[str, Sequence[str]],
tasks: Iterable[PregelTask | PregelExecutableTask],
pending_writes: list[PendingWrite] | None,
states: dict[str, RunnableConfig | StateSnapshot] | None,
output_keys: str | Sequence[str],
) -> tuple[PregelTask, ...]:
"""Apply writes / subgraph states to tasks to be returned in a StateSnapshot."""
pending_writes = pending_writes or []
+14 -12
View File
@@ -1,6 +1,8 @@
from __future__ import annotations
from collections import defaultdict
from collections.abc import Mapping, Sequence
from typing import Any, Optional, Union, cast
from typing import Any, cast
from langchain_core.runnables.config import RunnableConfig
from langchain_core.runnables.graph import Graph, Node
@@ -26,10 +28,10 @@ def draw_graph(
config: RunnableConfig,
*,
nodes: dict[str, PregelNode],
specs: dict[str, Union[BaseChannel, ManagedValueSpec]],
input_channels: Union[str, Sequence[str]],
interrupt_after_nodes: Union[All, Sequence[str]],
interrupt_before_nodes: Union[All, Sequence[str]],
specs: dict[str, BaseChannel | ManagedValueSpec],
input_channels: str | Sequence[str],
interrupt_after_nodes: All | Sequence[str],
interrupt_before_nodes: All | Sequence[str],
trigger_to_nodes: Mapping[str, Sequence[str]],
checkpointer: Checkpointer,
subgraphs: dict[str, Graph],
@@ -46,7 +48,7 @@ def draw_graph(
The graph for this Pregel instance.
"""
# (src, dest, is_conditional, label)
edges: set[tuple[str, str, bool, Optional[str]]] = set()
edges: set[tuple[str, str, bool, str | None]] = set()
step = -1
checkpoint = empty_checkpoint()
@@ -60,8 +62,8 @@ def draw_graph(
checkpoint,
)
static_seen: set[Any] = set()
sources: dict[str, set[tuple[str, bool, Optional[str]]]] = {}
step_sources: dict[str, set[tuple[str, bool, Optional[str]]]] = {}
sources: dict[str, set[tuple[str, bool, str | None]]] = {}
step_sources: dict[str, set[tuple[str, bool, str | None]]] = {}
# remove node mappers
nodes = {
k: v.copy(update={"mapper": None}) if v.mapper is not None else v
@@ -100,7 +102,7 @@ def draw_graph(
for step in range(step, limit):
if not tasks:
break
conditionals: dict[tuple[str, str, Any], Optional[str]] = {}
conditionals: dict[tuple[str, str, Any], str | None] = {}
# run task writers
for task in tasks.values():
for w in task.writers:
@@ -140,8 +142,8 @@ def draw_graph(
}
sources.update(step_sources)
# invert triggers
trigger_to_sources: dict[str, set[tuple[str, bool, Optional[str]]]] = (
defaultdict(set)
trigger_to_sources: dict[str, set[tuple[str, bool, str | None]]] = defaultdict(
set
)
for src, triggers in sources.items():
for trigger, cond, label in triggers:
@@ -246,7 +248,7 @@ def add_edge(
source: str,
target: str,
*,
data: Optional[Any] = None,
data: Any | None = None,
conditional: bool = False,
) -> None:
"""Add an edge to the graph."""
+13 -12
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import concurrent.futures
import time
@@ -7,7 +9,6 @@ from contextvars import copy_context
from types import TracebackType
from typing import (
Callable,
Optional,
Protocol,
TypeVar,
cast,
@@ -29,7 +30,7 @@ class Submit(Protocol[P, T]):
self,
fn: Callable[P, T],
*args: P.args,
__name__: Optional[str] = None,
__name__: str | None = None,
__cancel_on_exit__: bool = False,
__reraise_on_exit__: bool = True,
__next_tick__: bool = False,
@@ -55,7 +56,7 @@ class BackgroundExecutor(AbstractContextManager):
self,
fn: Callable[P, T],
*args: P.args,
__name__: Optional[str] = None, # currently not used in sync version
__name__: str | None = None, # currently not used in sync version
__cancel_on_exit__: bool = False, # for sync, can cancel only if not started
__reraise_on_exit__: bool = True,
__next_tick__: bool = False,
@@ -92,10 +93,10 @@ class BackgroundExecutor(AbstractContextManager):
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
# copy the tasks as done() callback may modify the dict
tasks = self.tasks.copy()
# cancel all tasks that should be cancelled
@@ -133,7 +134,7 @@ class AsyncBackgroundExecutor(AbstractAsyncContextManager):
self.sentinel = object()
self.loop = asyncio.get_running_loop()
if max_concurrency := config.get("max_concurrency"):
self.semaphore: Optional[asyncio.Semaphore] = asyncio.Semaphore(
self.semaphore: asyncio.Semaphore | None = asyncio.Semaphore(
max_concurrency
)
else:
@@ -143,7 +144,7 @@ class AsyncBackgroundExecutor(AbstractAsyncContextManager):
self,
fn: Callable[P, Awaitable[T]],
*args: P.args,
__name__: Optional[str] = None,
__name__: str | None = None,
__cancel_on_exit__: bool = False,
__reraise_on_exit__: bool = True,
__next_tick__: bool = False, # noop in async (always True)
@@ -185,9 +186,9 @@ class AsyncBackgroundExecutor(AbstractAsyncContextManager):
async def __aexit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
# copy the tasks as done() callback may modify the dict
tasks = self.tasks.copy()
+12 -10
View File
@@ -1,6 +1,8 @@
from __future__ import annotations
from collections import Counter
from collections.abc import Iterator, Mapping, Sequence
from typing import Any, Literal, Optional, Union
from typing import Any, Literal
from langgraph.channels.base import BaseChannel, EmptyChannelError
from langgraph.constants import (
@@ -37,10 +39,10 @@ def read_channel(
def read_channels(
channels: Mapping[str, BaseChannel],
select: Union[Sequence[str], str],
select: Sequence[str] | str,
*,
skip_empty: bool = True,
) -> Union[dict[str, Any], Any]:
) -> dict[str, Any] | Any:
if isinstance(select, str):
return read_channel(channels, select)
else:
@@ -79,8 +81,8 @@ def map_command(cmd: Command) -> Iterator[tuple[str, str, Any]]:
def map_input(
input_channels: Union[str, Sequence[str]],
chunk: Optional[Union[dict[str, Any], Any]],
input_channels: str | Sequence[str],
chunk: dict[str, Any] | Any | None,
) -> Iterator[tuple[str, Any]]:
"""Map input chunk to a sequence of pending writes in the form (channel, value)."""
if chunk is None:
@@ -98,10 +100,10 @@ def map_input(
def map_output_values(
output_channels: Union[str, Sequence[str]],
pending_writes: Union[Literal[True], Sequence[tuple[str, Any]]],
output_channels: str | Sequence[str],
pending_writes: Literal[True] | Sequence[tuple[str, Any]],
channels: Mapping[str, BaseChannel],
) -> Iterator[Union[dict[str, Any], Any]]:
) -> Iterator[dict[str, Any] | Any]:
"""Map pending writes (a sequence of tuples (channel, value)) to output chunk."""
if isinstance(output_channels, str):
if pending_writes is True or any(
@@ -116,10 +118,10 @@ def map_output_values(
def map_output_updates(
output_channels: Union[str, Sequence[str]],
output_channels: str | Sequence[str],
tasks: list[tuple[PregelExecutableTask, Sequence[tuple[str, Any]]]],
cached: bool = False,
) -> Iterator[dict[str, Union[Any, dict[str, Any]]]]:
) -> Iterator[dict[str, Any | dict[str, Any]]]:
"""Map pending writes (a sequence of tuples (channel, value)) to output chunk."""
output_tasks = [
(t, ww)
+90 -88
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import binascii
import concurrent.futures
@@ -18,7 +20,6 @@ from typing import (
Literal,
Optional,
TypeVar,
Union,
cast,
)
@@ -148,36 +149,36 @@ def DuplexStream(*streams: StreamProtocol) -> StreamProtocol:
class PregelLoop:
config: RunnableConfig
store: Optional["BaseStore"]
stream: Optional[StreamProtocol]
store: BaseStore | None
stream: StreamProtocol | None
step: int
stop: int
input: Optional[Any]
input_model: Optional[type[BaseModel]]
cache: Optional[BaseCache[WritesT]]
checkpointer: Optional[BaseCheckpointSaver]
input: Any | None
input_model: type[BaseModel] | None
cache: BaseCache[WritesT] | None
checkpointer: BaseCheckpointSaver | None
nodes: Mapping[str, PregelNode]
specs: Mapping[str, Union[BaseChannel, ManagedValueSpec]]
output_keys: Union[str, Sequence[str]]
stream_keys: Union[str, Sequence[str]]
specs: Mapping[str, BaseChannel | ManagedValueSpec]
output_keys: str | Sequence[str]
stream_keys: str | Sequence[str]
skip_done_tasks: bool
is_nested: bool
manager: Union[None, AsyncParentRunManager, ParentRunManager]
interrupt_after: Union[All, Sequence[str]]
interrupt_before: Union[All, Sequence[str]]
manager: None | AsyncParentRunManager | ParentRunManager
interrupt_after: All | Sequence[str]
interrupt_before: All | Sequence[str]
checkpoint_during: bool
debug: bool
retry_policy: Sequence[RetryPolicy]
cache_policy: Optional[CachePolicy]
cache_policy: CachePolicy | None
checkpointer_get_next_version: GetNextVersion
checkpointer_put_writes: Optional[Callable[[RunnableConfig, WritesT, str], Any]]
checkpointer_put_writes: Callable[[RunnableConfig, WritesT, str], Any] | None
checkpointer_put_writes_accepts_task_path: bool
_checkpointer_put_after_previous: Optional[
_checkpointer_put_after_previous: (
Callable[
[
Optional[concurrent.futures.Future],
concurrent.futures.Future | None,
RunnableConfig,
Checkpoint,
str,
@@ -185,8 +186,9 @@ class PregelLoop:
],
Any,
]
]
_migrate_checkpoint: Optional[Callable[[Checkpoint], None]]
| None
)
_migrate_checkpoint: Callable[[Checkpoint], None] | None
submit: Submit
channels: Mapping[str, BaseChannel]
managed: ManagedValueMapping
@@ -196,40 +198,40 @@ class PregelLoop:
checkpoint_config: RunnableConfig
checkpoint_metadata: CheckpointMetadata
checkpoint_pending_writes: list[PendingWrite]
checkpoint_previous_versions: dict[str, Union[str, float, int]]
prev_checkpoint_config: Optional[RunnableConfig]
checkpoint_previous_versions: dict[str, str | float | int]
prev_checkpoint_config: RunnableConfig | None
status: Literal[
"pending", "done", "interrupt_before", "interrupt_after", "out_of_steps"
]
tasks: dict[str, PregelExecutableTask]
to_interrupt: list[PregelExecutableTask]
output: Union[None, dict[str, Any], Any] = None
output: None | dict[str, Any] | Any = None
# public
def __init__(
self,
input: Optional[Any],
input: Any | None,
*,
stream: Optional[StreamProtocol],
stream: StreamProtocol | None,
config: RunnableConfig,
store: Optional[BaseStore],
cache: Optional[BaseCache],
checkpointer: Optional[BaseCheckpointSaver],
store: BaseStore | None,
cache: BaseCache | None,
checkpointer: BaseCheckpointSaver | None,
nodes: Mapping[str, PregelNode],
specs: Mapping[str, Union[BaseChannel, ManagedValueSpec]],
output_keys: Union[str, Sequence[str]],
stream_keys: Union[str, Sequence[str]],
specs: Mapping[str, BaseChannel | ManagedValueSpec],
output_keys: str | Sequence[str],
stream_keys: str | Sequence[str],
trigger_to_nodes: Mapping[str, Sequence[str]],
interrupt_after: Union[All, Sequence[str]] = EMPTY_SEQ,
interrupt_before: Union[All, Sequence[str]] = EMPTY_SEQ,
manager: Union[None, AsyncParentRunManager, ParentRunManager] = None,
input_model: Optional[type[BaseModel]] = None,
interrupt_after: All | Sequence[str] = EMPTY_SEQ,
interrupt_before: All | Sequence[str] = EMPTY_SEQ,
manager: None | AsyncParentRunManager | ParentRunManager = None,
input_model: type[BaseModel] | None = None,
debug: bool = False,
migrate_checkpoint: Optional[Callable[[Checkpoint], None]] = None,
migrate_checkpoint: Callable[[Checkpoint], None] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Optional[CachePolicy] = None,
cache_policy: CachePolicy | None = None,
checkpoint_during: bool = True,
) -> None:
self.stream = stream
@@ -261,7 +263,7 @@ class PregelLoop:
self.debug = debug
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)
scratchpad: PregelScratchpad | None = config[CONF].get(CONFIG_KEY_SCRATCHPAD)
if not self.config[CONF].get(CONFIG_KEY_DELEGATE) and isinstance(
scratchpad, PregelScratchpad
):
@@ -399,8 +401,8 @@ class PregelLoop:
)
def accept_push(
self, task: PregelExecutableTask, write_idx: int, call: Optional[Call] = None
) -> Optional[PregelExecutableTask]:
self, task: PregelExecutableTask, write_idx: int, call: Call | None = None
) -> PregelExecutableTask | None:
"""Accept a PUSH from a task, potentially returning a new task to start."""
# don't start if we should interrupt *after* the original task
if self.interrupt_after and should_interrupt(
@@ -455,7 +457,7 @@ class PregelLoop:
def tick(
self,
*,
input_keys: Union[str, Sequence[str]],
input_keys: str | Sequence[str],
) -> bool:
"""Execute a single iteration of the Pregel loop.
@@ -649,7 +651,7 @@ class PregelLoop:
else:
task.writes.append((k, v))
def _first(self, *, input_keys: Union[str, Sequence[str]]) -> Optional[set[str]]:
def _first(self, *, input_keys: str | Sequence[str]) -> set[str] | None:
# resuming from previous checkpoint requires
# - finding a previous checkpoint
# - receiving None input (outer graph) or RESUMING flag (subgraph)
@@ -667,7 +669,7 @@ class PregelLoop:
)
)
# this can be set only when there are input_writes
updated_channels: Optional[set[str]] = None
updated_channels: set[str] | None = None
# map command to writes
if isinstance(self.input, Command):
@@ -861,10 +863,10 @@ class PregelLoop:
def _suppress_interrupt(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
# persist current checkpoint and writes
if not self.checkpoint_during:
self._put_checkpoint(self.checkpoint_metadata)
@@ -977,26 +979,26 @@ class PregelLoop:
class SyncPregelLoop(PregelLoop, AbstractContextManager):
def __init__(
self,
input: Optional[Any],
input: Any | None,
*,
stream: Optional[StreamProtocol],
stream: StreamProtocol | None,
config: RunnableConfig,
store: Optional[BaseStore],
cache: Optional[BaseCache],
checkpointer: Optional[BaseCheckpointSaver],
store: BaseStore | None,
cache: BaseCache | None,
checkpointer: BaseCheckpointSaver | None,
nodes: Mapping[str, PregelNode],
specs: Mapping[str, Union[BaseChannel, ManagedValueSpec]],
specs: Mapping[str, BaseChannel | ManagedValueSpec],
trigger_to_nodes: Mapping[str, Sequence[str]],
manager: Union[None, AsyncParentRunManager, ParentRunManager] = None,
interrupt_after: Union[All, Sequence[str]] = EMPTY_SEQ,
interrupt_before: Union[All, Sequence[str]] = EMPTY_SEQ,
output_keys: Union[str, Sequence[str]] = EMPTY_SEQ,
stream_keys: Union[str, Sequence[str]] = EMPTY_SEQ,
input_model: Optional[type[BaseModel]] = None,
manager: None | AsyncParentRunManager | ParentRunManager = None,
interrupt_after: All | Sequence[str] = EMPTY_SEQ,
interrupt_before: All | Sequence[str] = EMPTY_SEQ,
output_keys: str | Sequence[str] = EMPTY_SEQ,
stream_keys: str | Sequence[str] = EMPTY_SEQ,
input_model: type[BaseModel] | None = None,
debug: bool = False,
migrate_checkpoint: Optional[Callable[[Checkpoint], None]] = None,
migrate_checkpoint: Callable[[Checkpoint], None] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Optional[CachePolicy] = None,
cache_policy: CachePolicy | None = None,
checkpoint_during: bool = True,
) -> None:
super().__init__(
@@ -1037,7 +1039,7 @@ class SyncPregelLoop(PregelLoop, AbstractContextManager):
def _checkpointer_put_after_previous(
self,
prev: Optional[concurrent.futures.Future],
prev: concurrent.futures.Future | None,
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
@@ -1067,8 +1069,8 @@ class SyncPregelLoop(PregelLoop, AbstractContextManager):
return matched
def accept_push(
self, task: PregelExecutableTask, write_idx: int, call: Optional[Call] = None
) -> Optional[PregelExecutableTask]:
self, task: PregelExecutableTask, write_idx: int, call: Call | None = None
) -> PregelExecutableTask | None:
if pushed := super().accept_push(task, write_idx, call):
for task in self.match_cached_writes():
self.output_writes(task.id, task.writes, cached=True)
@@ -1156,10 +1158,10 @@ class SyncPregelLoop(PregelLoop, AbstractContextManager):
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
# unwind stack
return self.stack.__exit__(exc_type, exc_value, traceback)
@@ -1167,26 +1169,26 @@ class SyncPregelLoop(PregelLoop, AbstractContextManager):
class AsyncPregelLoop(PregelLoop, AbstractAsyncContextManager):
def __init__(
self,
input: Optional[Any],
input: Any | None,
*,
stream: Optional[StreamProtocol],
stream: StreamProtocol | None,
config: RunnableConfig,
store: Optional[BaseStore],
cache: Optional[BaseCache],
checkpointer: Optional[BaseCheckpointSaver],
store: BaseStore | None,
cache: BaseCache | None,
checkpointer: BaseCheckpointSaver | None,
nodes: Mapping[str, PregelNode],
specs: Mapping[str, Union[BaseChannel, ManagedValueSpec]],
specs: Mapping[str, BaseChannel | ManagedValueSpec],
trigger_to_nodes: Mapping[str, Sequence[str]],
interrupt_after: Union[All, Sequence[str]] = EMPTY_SEQ,
interrupt_before: Union[All, Sequence[str]] = EMPTY_SEQ,
manager: Union[None, AsyncParentRunManager, ParentRunManager] = None,
output_keys: Union[str, Sequence[str]] = EMPTY_SEQ,
stream_keys: Union[str, Sequence[str]] = EMPTY_SEQ,
input_model: Optional[type[BaseModel]] = None,
interrupt_after: All | Sequence[str] = EMPTY_SEQ,
interrupt_before: All | Sequence[str] = EMPTY_SEQ,
manager: None | AsyncParentRunManager | ParentRunManager = None,
output_keys: str | Sequence[str] = EMPTY_SEQ,
stream_keys: str | Sequence[str] = EMPTY_SEQ,
input_model: type[BaseModel] | None = None,
debug: bool = False,
migrate_checkpoint: Optional[Callable[[Checkpoint], None]] = None,
migrate_checkpoint: Callable[[Checkpoint], None] | None = None,
retry_policy: Sequence[RetryPolicy] = (),
cache_policy: Optional[CachePolicy] = None,
cache_policy: CachePolicy | None = None,
checkpoint_during: bool = True,
) -> None:
super().__init__(
@@ -1227,7 +1229,7 @@ class AsyncPregelLoop(PregelLoop, AbstractAsyncContextManager):
async def _checkpointer_put_after_previous(
self,
prev: Optional[asyncio.Task],
prev: asyncio.Task | None,
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
@@ -1257,8 +1259,8 @@ class AsyncPregelLoop(PregelLoop, AbstractAsyncContextManager):
return matched
async def aaccept_push(
self, task: PregelExecutableTask, write_idx: int, call: Optional[Call] = None
) -> Optional[PregelExecutableTask]:
self, task: PregelExecutableTask, write_idx: int, call: Call | None = None
) -> PregelExecutableTask | None:
if pushed := super().accept_push(task, write_idx, call):
for task in await self.amatch_cached_writes():
self.output_writes(task.id, task.writes, cached=True)
@@ -1352,10 +1354,10 @@ class AsyncPregelLoop(PregelLoop, AbstractAsyncContextManager):
async def __aexit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
# unwind stack
exit_task = asyncio.create_task(
self.stack.__aexit__(exc_type, exc_value, traceback)
+16 -16
View File
@@ -1,10 +1,10 @@
from __future__ import annotations
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import (
Any,
Callable,
Optional,
TypeVar,
Union,
cast,
)
from uuid import UUID, uuid4
@@ -36,7 +36,7 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
self.stream = stream
self.subgraphs = subgraphs
self.metadata: dict[UUID, Meta] = {}
self.seen: set[Union[int, str]] = set()
self.seen: set[int | str] = set()
def _emit(self, meta: Meta, message: BaseMessage, *, dedupe: bool = False) -> None:
if dedupe and message.id in self.seen:
@@ -89,9 +89,9 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
messages: list[list[BaseMessage]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
parent_run_id: UUID | None = None,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> Any:
if metadata and (
@@ -111,10 +111,10 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
self,
token: str,
*,
chunk: Optional[ChatGenerationChunk] = None,
chunk: ChatGenerationChunk | None = None,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
parent_run_id: UUID | None = None,
tags: list[str] | None = None,
**kwargs: Any,
) -> Any:
if not isinstance(chunk, ChatGenerationChunk):
@@ -127,7 +127,7 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
response: LLMResult,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> Any:
if meta := self.metadata.get(run_id):
@@ -142,7 +142,7 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
error: BaseException,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> Any:
self.metadata.pop(run_id, None)
@@ -153,9 +153,9 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
inputs: dict[str, Any],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
parent_run_id: UUID | None = None,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
**kwargs: Any,
) -> Any:
if (
@@ -185,7 +185,7 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
response: Any,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> Any:
if meta := self.metadata.pop(run_id, None):
@@ -210,7 +210,7 @@ class StreamMessagesHandler(BaseCallbackHandler, _StreamingCallbackHandler):
error: BaseException,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> Any:
self.metadata.pop(run_id, None)
+36 -36
View File
@@ -2,37 +2,37 @@ from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import Any, Generic, Optional, Union
from typing import Any, Generic
from langchain_core.runnables import Runnable, RunnableConfig
from langchain_core.runnables.graph import Graph as DrawableGraph
from typing_extensions import Self
from langgraph.pregel.types import All, StateSnapshot, StateUpdate, StreamMode
from langgraph.typing import InputT
from langgraph.typing import InputT, OutputT, StateT
# TODO: remove Runnable inheritance here!
class PregelProtocol(Runnable[InputT, Any], Generic[InputT], ABC):
class PregelProtocol(Runnable[InputT, Any], Generic[StateT, InputT, OutputT], ABC):
@abstractmethod
def with_config(
self, config: Optional[RunnableConfig] = None, **kwargs: Any
self, config: RunnableConfig | None = None, **kwargs: Any
) -> Self: ...
@abstractmethod
def get_graph(
self,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
xray: Union[int, bool] = False,
xray: int | bool = False,
) -> DrawableGraph: ...
@abstractmethod
async def aget_graph(
self,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
xray: Union[int, bool] = False,
xray: int | bool = False,
) -> DrawableGraph: ...
@abstractmethod
@@ -50,9 +50,9 @@ class PregelProtocol(Runnable[InputT, Any], Generic[InputT], ABC):
self,
config: RunnableConfig,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[StateSnapshot]: ...
@abstractmethod
@@ -60,9 +60,9 @@ class PregelProtocol(Runnable[InputT, Any], Generic[InputT], ABC):
self,
config: RunnableConfig,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[StateSnapshot]: ...
@abstractmethod
@@ -83,58 +83,58 @@ class PregelProtocol(Runnable[InputT, Any], Generic[InputT], ABC):
def update_state(
self,
config: RunnableConfig,
values: Optional[Union[dict[str, Any], Any]],
as_node: Optional[str] = None,
values: dict[str, Any] | Any | None,
as_node: str | None = None,
) -> RunnableConfig: ...
@abstractmethod
async def aupdate_state(
self,
config: RunnableConfig,
values: Optional[Union[dict[str, Any], Any]],
as_node: Optional[str] = None,
values: dict[str, Any] | Any | None,
as_node: str | None = None,
) -> RunnableConfig: ...
@abstractmethod
def stream(
self,
input: InputT,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
stream_mode: Optional[Union[StreamMode, list[StreamMode]]] = None,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
stream_mode: StreamMode | list[StreamMode] | None = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
subgraphs: bool = False,
) -> Iterator[Union[dict[str, Any], Any]]: ...
) -> Iterator[dict[str, Any] | Any]: ...
@abstractmethod
def astream(
self,
input: InputT,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
stream_mode: Optional[Union[StreamMode, list[StreamMode]]] = None,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
stream_mode: StreamMode | list[StreamMode] | None = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
subgraphs: bool = False,
) -> AsyncIterator[Union[dict[str, Any], Any]]: ...
) -> AsyncIterator[dict[str, Any] | Any]: ...
@abstractmethod
def invoke(
self,
input: InputT,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
) -> Union[dict[str, Any], Any]: ...
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
) -> dict[str, Any] | Any: ...
@abstractmethod
async def ainvoke(
self,
input: InputT,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
) -> Union[dict[str, Any], Any]: ...
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
) -> dict[str, Any] | Any: ...
+7 -24
View File
@@ -101,11 +101,10 @@ class PregelNode(Runnable):
itself, but instead acts as a container for the components necessary to make
a PregelExecutableTask for a node."""
channels: list[str] | Mapping[str, str]
channels: str | list[str]
"""The channels that will be passed as input to `bound`.
If a list, the node will be invoked with the first of that isn't empty.
If a dict, the keys are the names of the channels, and the values are the keys
to use in the input to `bound`."""
If a str, the node will be invoked with its value if it isn't empty.
If a list, the node will be invoked with a dict of those channels' values."""
triggers: list[str]
"""If any of these channels is written to, this node will be triggered in
@@ -140,7 +139,7 @@ class PregelNode(Runnable):
def __init__(
self,
*,
channels: list[str] | Mapping[str, str],
channels: str | list[str],
triggers: Sequence[str],
mapper: Callable[[Any], Any] | None = None,
writers: list[Runnable] | None = None,
@@ -223,25 +222,9 @@ class PregelNode(Runnable):
This is used to avoid calculating the same input multiple times."""
return (
self.mapper,
tuple(f"{key}:{value}" for key, value in self.channels.items())
if isinstance(self.channels, dict)
else tuple(self.channels),
)
def join(self, channels: Sequence[str]) -> PregelNode:
assert isinstance(channels, list) or isinstance(channels, tuple), (
"channels must be a list or tuple"
)
assert isinstance(self.channels, dict), (
"all channels must be named when using .join()"
)
return self.copy(
update=dict(
channels={
**self.channels,
**{chan: chan for chan in channels},
}
),
tuple(self.channels)
if isinstance(self.channels, list)
else (self.channels,),
)
def __or__(
+63 -67
View File
@@ -1,10 +1,10 @@
from __future__ import annotations
from collections.abc import AsyncIterator, Iterator, Sequence
from dataclasses import asdict
from typing import (
Any,
Literal,
Optional,
Union,
cast,
)
@@ -96,20 +96,20 @@ class RemoteGraph(PregelProtocol):
"""
assistant_id: str
name: Optional[str]
name: str | None
def __init__(
self,
assistant_id: str, # graph_id
/,
*,
url: Optional[str] = None,
api_key: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
client: Optional[LangGraphClient] = None,
sync_client: Optional[SyncLangGraphClient] = None,
config: Optional[RunnableConfig] = None,
name: Optional[str] = None,
url: str | None = None,
api_key: str | None = None,
headers: dict[str, str] | None = None,
client: LangGraphClient | None = None,
sync_client: SyncLangGraphClient | None = None,
config: RunnableConfig | None = None,
name: str | None = None,
):
"""Specify `url`, `api_key`, and/or `headers` to create default sync and async clients.
@@ -162,9 +162,7 @@ class RemoteGraph(PregelProtocol):
attrs = {**self.__dict__, **update}
return self.__class__(attrs.pop("assistant_id"), **attrs)
def with_config(
self, config: Optional[RunnableConfig] = None, **kwargs: Any
) -> Self:
def with_config(self, config: RunnableConfig | None = None, **kwargs: Any) -> Self:
return self.copy(
{"config": merge_configs(self.config, config, cast(RunnableConfig, kwargs))}
)
@@ -195,9 +193,9 @@ class RemoteGraph(PregelProtocol):
def get_graph(
self,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
xray: Union[int, bool] = False,
xray: int | bool = False,
) -> DrawableGraph:
"""Get graph by graph name.
@@ -224,9 +222,9 @@ class RemoteGraph(PregelProtocol):
async def aget_graph(
self,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
xray: Union[int, bool] = False,
xray: int | bool = False,
) -> DrawableGraph:
"""Get graph by graph name.
@@ -309,7 +307,7 @@ class RemoteGraph(PregelProtocol):
interrupts=tuple([i for task in tasks for i in task.interrupts]),
)
def _get_checkpoint(self, config: Optional[RunnableConfig]) -> Optional[Checkpoint]:
def _get_checkpoint(self, config: RunnableConfig | None) -> Checkpoint | None:
if config is None:
return None
@@ -423,9 +421,9 @@ class RemoteGraph(PregelProtocol):
self,
config: RunnableConfig,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> Iterator[StateSnapshot]:
"""Get the state history of a thread.
@@ -458,9 +456,9 @@ class RemoteGraph(PregelProtocol):
self,
config: RunnableConfig,
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None,
filter: dict[str, Any] | None = None,
before: RunnableConfig | None = None,
limit: int | None = None,
) -> AsyncIterator[StateSnapshot]:
"""Get the state history of a thread.
@@ -492,22 +490,22 @@ class RemoteGraph(PregelProtocol):
def bulk_update_state(
self,
config: RunnableConfig,
updates: list[tuple[Optional[dict[str, Any]], Optional[str]]],
updates: list[tuple[dict[str, Any] | None, str | None]],
) -> RunnableConfig:
raise NotImplementedError
async def abulk_update_state(
self,
config: RunnableConfig,
updates: list[tuple[Optional[dict[str, Any]], Optional[str]]],
updates: list[tuple[dict[str, Any] | None, str | None]],
) -> RunnableConfig:
raise NotImplementedError
def update_state(
self,
config: RunnableConfig,
values: Optional[Union[dict[str, Any], Any]],
as_node: Optional[str] = None,
values: dict[str, Any] | Any | None,
as_node: str | None = None,
) -> RunnableConfig:
"""Update the state of a thread.
@@ -536,8 +534,8 @@ class RemoteGraph(PregelProtocol):
async def aupdate_state(
self,
config: RunnableConfig,
values: Optional[Union[dict[str, Any], Any]],
as_node: Optional[str] = None,
values: dict[str, Any] | Any | None,
as_node: str | None = None,
) -> RunnableConfig:
"""Update the state of a thread.
@@ -565,12 +563,10 @@ class RemoteGraph(PregelProtocol):
def _get_stream_modes(
self,
stream_mode: Optional[Union[StreamMode, list[StreamMode]]],
config: Optional[RunnableConfig],
stream_mode: StreamMode | list[StreamMode] | None,
config: RunnableConfig | None,
default: StreamMode = "updates",
) -> tuple[
list[StreamModeSDK], list[StreamModeSDK], bool, Optional[StreamProtocol]
]:
) -> tuple[list[StreamModeSDK], list[StreamModeSDK], bool, StreamProtocol | None]:
"""Return a tuple of the final list of stream modes sent to the
remote graph and a boolean flag indicating if stream mode 'updates'
was present in the original list of stream modes.
@@ -591,7 +587,7 @@ class RemoteGraph(PregelProtocol):
updated_stream_modes.append(default)
requested_stream_modes = updated_stream_modes.copy()
# add any from parent graph
stream: Optional[StreamProtocol] = (
stream: StreamProtocol | None = (
(config or {}).get(CONF, {}).get(CONFIG_KEY_STREAM)
)
if stream:
@@ -618,15 +614,15 @@ class RemoteGraph(PregelProtocol):
def stream(
self,
input: Union[dict[str, Any], Any],
config: Optional[RunnableConfig] = None,
input: dict[str, Any] | Any,
config: RunnableConfig | None = None,
*,
stream_mode: Optional[Union[StreamMode, list[StreamMode]]] = None,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
stream_mode: StreamMode | list[StreamMode] | None = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
subgraphs: bool = False,
**kwargs: Any,
) -> Iterator[Union[dict[str, Any], Any]]:
) -> Iterator[dict[str, Any] | Any]:
"""Create a run and stream the results.
This method calls `POST /threads/{thread_id}/runs/stream` if a `thread_id`
@@ -652,7 +648,7 @@ class RemoteGraph(PregelProtocol):
stream_mode, config
)
if isinstance(input, Command):
command: Optional[CommandSDK] = cast(CommandSDK, asdict(input))
command: CommandSDK | None = cast(CommandSDK, asdict(input))
input = None
else:
command = None
@@ -717,15 +713,15 @@ class RemoteGraph(PregelProtocol):
async def astream(
self,
input: Union[dict[str, Any], Any],
config: Optional[RunnableConfig] = None,
input: dict[str, Any] | Any,
config: RunnableConfig | None = None,
*,
stream_mode: Optional[Union[StreamMode, list[StreamMode]]] = None,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
stream_mode: StreamMode | list[StreamMode] | None = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
subgraphs: bool = False,
**kwargs: Any,
) -> AsyncIterator[Union[dict[str, Any], Any]]:
) -> AsyncIterator[dict[str, Any] | Any]:
"""Create a run and stream the results.
This method calls `POST /threads/{thread_id}/runs/stream` if a `thread_id`
@@ -751,7 +747,7 @@ class RemoteGraph(PregelProtocol):
stream_mode, config
)
if isinstance(input, Command):
command: Optional[CommandSDK] = cast(CommandSDK, asdict(input))
command: CommandSDK | None = cast(CommandSDK, asdict(input))
input = None
else:
command = None
@@ -817,28 +813,28 @@ class RemoteGraph(PregelProtocol):
async def astream_events(
self,
input: Any,
config: Optional[RunnableConfig] = None,
config: RunnableConfig | None = None,
*,
version: Literal["v1", "v2"],
include_names: Optional[Sequence[All]] = None,
include_types: Optional[Sequence[All]] = None,
include_tags: Optional[Sequence[All]] = None,
exclude_names: Optional[Sequence[All]] = None,
exclude_types: Optional[Sequence[All]] = None,
exclude_tags: Optional[Sequence[All]] = None,
include_names: Sequence[All] | None = None,
include_types: Sequence[All] | None = None,
include_tags: Sequence[All] | None = None,
exclude_names: Sequence[All] | None = None,
exclude_types: Sequence[All] | None = None,
exclude_tags: Sequence[All] | None = None,
**kwargs: Any,
) -> AsyncIterator[dict[str, Any]]:
raise NotImplementedError
def invoke(
self,
input: Union[dict[str, Any], Any],
config: Optional[RunnableConfig] = None,
input: dict[str, Any] | Any,
config: RunnableConfig | None = None,
*,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
**kwargs: Any,
) -> Union[dict[str, Any], Any]:
) -> dict[str, Any] | Any:
"""Create a run, wait until it finishes and return the final state.
Args:
@@ -867,13 +863,13 @@ class RemoteGraph(PregelProtocol):
async def ainvoke(
self,
input: Union[dict[str, Any], Any],
config: Optional[RunnableConfig] = None,
input: dict[str, Any] | Any,
config: RunnableConfig | None = None,
*,
interrupt_before: Optional[Union[All, Sequence[str]]] = None,
interrupt_after: Optional[Union[All, Sequence[str]]] = None,
interrupt_before: All | Sequence[str] | None = None,
interrupt_after: All | Sequence[str] | None = None,
**kwargs: Any,
) -> Union[dict[str, Any], Any]:
) -> dict[str, Any] | Any:
"""Create a run, wait until it finishes and return the final state.
Args:
+9 -8
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import logging
import random
@@ -5,7 +7,7 @@ import sys
import time
from collections.abc import Awaitable, Sequence
from dataclasses import replace
from typing import Any, Callable, Optional
from typing import Any, Callable
from langgraph.constants import (
CONF,
@@ -23,8 +25,8 @@ SUPPORTS_EXC_NOTES = sys.version_info >= (3, 11)
def run_with_retry(
task: PregelExecutableTask,
retry_policy: Optional[Sequence[RetryPolicy]],
configurable: Optional[dict[str, Any]] = None,
retry_policy: Sequence[RetryPolicy] | None,
configurable: dict[str, Any] | None = None,
) -> None:
"""Run a task with retries."""
retry_policy = task.retry_policy or retry_policy
@@ -104,12 +106,11 @@ def run_with_retry(
async def arun_with_retry(
task: PregelExecutableTask,
retry_policy: Optional[Sequence[RetryPolicy]],
retry_policy: Sequence[RetryPolicy] | None,
stream: bool = False,
match_cached_writes: Optional[
Callable[[], Awaitable[Sequence[PregelExecutableTask]]]
] = None,
configurable: Optional[dict[str, Any]] = None,
match_cached_writes: Callable[[], Awaitable[Sequence[PregelExecutableTask]]]
| None = None,
configurable: dict[str, Any] | None = None,
) -> None:
"""Run a task asynchronously with retries."""
retry_policy = task.retry_policy or retry_policy
+43 -43
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio
import concurrent.futures
import threading
@@ -56,16 +58,14 @@ EXCLUDED_FRAME_FNAMES = (
"concurrent/futures/_base.py",
)
SKIP_RERAISE_SET: weakref.WeakSet[Union[concurrent.futures.Future, asyncio.Future]] = (
SKIP_RERAISE_SET: weakref.WeakSet[concurrent.futures.Future | asyncio.Future] = (
weakref.WeakSet()
)
class FuturesDict(Generic[F, E], dict[F, Optional[PregelExecutableTask]]):
event: E
callback: weakref.ref[
Callable[[PregelExecutableTask, Optional[BaseException]], None]
]
callback: weakref.ref[Callable[[PregelExecutableTask, BaseException | None], None]]
counter: int
done: set[F]
lock: threading.Lock
@@ -74,7 +74,7 @@ class FuturesDict(Generic[F, E], dict[F, Optional[PregelExecutableTask]]):
self,
event: E,
callback: weakref.ref[
Callable[[PregelExecutableTask, Optional[BaseException]], None]
Callable[[PregelExecutableTask, BaseException | None], None]
],
future_type: type[F],
# used for generic typing, newer py supports FutureDict[...](...)
@@ -89,7 +89,7 @@ class FuturesDict(Generic[F, E], dict[F, Optional[PregelExecutableTask]]):
def __setitem__(
self,
key: F,
value: Optional[PregelExecutableTask],
value: PregelExecutableTask | None,
) -> None:
super().__setitem__(key, value) # type: ignore[index]
if value is not None:
@@ -124,7 +124,7 @@ class PregelRunner:
submit: weakref.ref[Submit],
put_writes: weakref.ref[Callable[[str, Sequence[tuple[str, Any]]], None]],
use_astream: bool = False,
node_finished: Optional[Callable[[str], None]] = None,
node_finished: Callable[[str], None] | None = None,
) -> None:
self.submit = submit
self.put_writes = put_writes
@@ -136,12 +136,12 @@ class PregelRunner:
tasks: Iterable[PregelExecutableTask],
*,
reraise: bool = True,
timeout: Optional[float] = None,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
get_waiter: Optional[Callable[[], concurrent.futures.Future[None]]] = None,
timeout: float | None = None,
retry_policy: Sequence[RetryPolicy] | None = None,
get_waiter: Callable[[], concurrent.futures.Future[None]] | None = None,
schedule_task: Callable[
[PregelExecutableTask, int, Optional[Call]],
Optional[PregelExecutableTask],
[PregelExecutableTask, int, Call | None],
PregelExecutableTask | None,
],
) -> Iterator[None]:
tasks = tuple(tasks)
@@ -268,12 +268,12 @@ class PregelRunner:
tasks: Iterable[PregelExecutableTask],
*,
reraise: bool = True,
timeout: Optional[float] = None,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
get_waiter: Optional[Callable[[], asyncio.Future[None]]] = None,
timeout: float | None = None,
retry_policy: Sequence[RetryPolicy] | None = None,
get_waiter: Callable[[], asyncio.Future[None]] | None = None,
schedule_task: Callable[
[PregelExecutableTask, int, Optional[Call]],
Awaitable[Optional[PregelExecutableTask]],
[PregelExecutableTask, int, Call | None],
Awaitable[PregelExecutableTask | None],
],
) -> AsyncIterator[None]:
loop = asyncio.get_event_loop()
@@ -415,7 +415,7 @@ class PregelRunner:
def commit(
self,
task: PregelExecutableTask,
exception: Optional[BaseException],
exception: BaseException | None,
) -> None:
if isinstance(exception, asyncio.CancelledError):
# for cancelled tasks, also save error in task,
@@ -465,8 +465,8 @@ def _should_stop_others(
def _exception(
fut: Union[concurrent.futures.Future[Any], asyncio.Future[Any]],
) -> Optional[BaseException]:
fut: concurrent.futures.Future[Any] | asyncio.Future[Any],
) -> BaseException | None:
"""Return the exception from a future, without raising CancelledError."""
if fut.cancelled():
if isinstance(fut, asyncio.Future):
@@ -478,14 +478,14 @@ def _exception(
def _panic_or_proceed(
futs: Union[set[concurrent.futures.Future], set[asyncio.Future]],
futs: set[concurrent.futures.Future] | set[asyncio.Future],
*,
timeout_exc_cls: type[Exception] = TimeoutError,
panic: bool = True,
) -> None:
"""Cancel remaining tasks if any failed, re-raise exception if panic is True."""
done: set[Union[concurrent.futures.Future[Any], asyncio.Future[Any]]] = set()
inflight: set[Union[concurrent.futures.Future[Any], asyncio.Future[Any]]] = set()
done: set[concurrent.futures.Future[Any] | asyncio.Future[Any]] = set()
inflight: set[concurrent.futures.Future[Any] | asyncio.Future[Any]] = set()
for fut in futs:
if fut.cancelled():
continue
@@ -522,22 +522,22 @@ def _panic_or_proceed(
def _call(
task: weakref.ref[PregelExecutableTask],
func: Callable[[Any], Union[Awaitable[Any], Any]],
func: Callable[[Any], Awaitable[Any] | Any],
input: Any,
*,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
cache_policy: Optional[CachePolicy] = None,
retry_policy: Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
callbacks: Callbacks = None,
futures: weakref.ref[FuturesDict],
schedule_task: Callable[
[PregelExecutableTask, int, Optional[Call]], Optional[PregelExecutableTask]
[PregelExecutableTask, int, Call | None], PregelExecutableTask | None
],
submit: weakref.ref[Submit],
) -> concurrent.futures.Future[Any]:
if asyncio.iscoroutinefunction(func):
raise RuntimeError("In an sync context async tasks cannot be called")
fut: Optional[concurrent.futures.Future] = None
fut: concurrent.futures.Future | None = None
# schedule PUSH tasks, collect futures
scratchpad: PregelScratchpad = task().config[CONF][CONFIG_KEY_SCRATCHPAD] # type: ignore[union-attr]
# schedule the next task, if the callback returns one
@@ -609,22 +609,22 @@ def _call(
def _acall(
task: weakref.ref[PregelExecutableTask],
func: Callable[[Any], Union[Awaitable[Any], Any]],
func: Callable[[Any], Awaitable[Any] | Any],
input: Any,
*,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
cache_policy: Optional[CachePolicy] = None,
retry_policy: Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
callbacks: Callbacks = None,
# injected dependencies
futures: weakref.ref[FuturesDict],
schedule_task: Callable[
[PregelExecutableTask, int, Optional[Call]],
Awaitable[Optional[PregelExecutableTask]],
[PregelExecutableTask, int, Call | None],
Awaitable[PregelExecutableTask | None],
],
submit: weakref.ref[Submit],
loop: asyncio.AbstractEventLoop,
stream: bool = False,
) -> Union[asyncio.Future[Any], concurrent.futures.Future[Any]]:
) -> asyncio.Future[Any] | concurrent.futures.Future[Any]:
# return a chained future to ensure commit() callback is called
# before the returned future is resolved, to ensure stream order etc
try:
@@ -633,8 +633,8 @@ def _acall(
in_async = False
# if in async context return an async future, otherwise return a sync future
if in_async:
fut: Union[asyncio.Future[Any], concurrent.futures.Future[Any]] = (
asyncio.Future(loop=loop)
fut: asyncio.Future[Any] | concurrent.futures.Future[Any] = asyncio.Future(
loop=loop
)
else:
fut = concurrent.futures.Future()
@@ -661,26 +661,26 @@ def _acall(
async def _acall_impl(
destination: Union[asyncio.Future[Any], concurrent.futures.Future[Any]],
destination: asyncio.Future[Any] | concurrent.futures.Future[Any],
task: weakref.ref[PregelExecutableTask],
func: Callable[[Any], Union[Awaitable[Any], Any]],
func: Callable[[Any], Awaitable[Any] | Any],
input: Any,
*,
retry_policy: Optional[Sequence[RetryPolicy]] = None,
cache_policy: Optional[CachePolicy] = None,
retry_policy: Sequence[RetryPolicy] | None = None,
cache_policy: CachePolicy | None = None,
callbacks: Callbacks = None,
# injected dependencies
futures: weakref.ref[FuturesDict[asyncio.Future, asyncio.Event]],
schedule_task: Callable[
[PregelExecutableTask, int, Optional[Call]],
Awaitable[Optional[PregelExecutableTask]],
[PregelExecutableTask, int, Call | None],
Awaitable[PregelExecutableTask | None],
],
submit: weakref.ref[Submit],
loop: asyncio.AbstractEventLoop,
stream: bool = False,
) -> None:
try:
fut: Optional[asyncio.Future] = None
fut: asyncio.Future | None = None
# schedule PUSH tasks, collect futures
scratchpad: PregelScratchpad = task().config[CONF][CONFIG_KEY_SCRATCHPAD] # type: ignore[union-attr]
# schedule the next task, if the callback returns one
+4 -2
View File
@@ -1,8 +1,10 @@
from __future__ import annotations
import ast
import inspect
import re
import textwrap
from typing import Any, Callable, Optional
from typing import Any, Callable
from langchain_core.runnables import RunnableLambda, RunnableSequence
from typing_extensions import override
@@ -30,7 +32,7 @@ def get_new_channel_versions(
return new_versions
def find_subgraph_pregel(candidate: Runnable) -> Optional[PregelProtocol]:
def find_subgraph_pregel(candidate: Runnable) -> PregelProtocol | None:
from langgraph.pregel import Pregel
candidates: list[Runnable] = [candidate]

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