langgraph_checkpoint_cosmosdb 0.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langgraph_checkpoint_cosmosdb/LICENSE +19 -0
- langgraph_checkpoint_cosmosdb/__init__.py +7 -0
- langgraph_checkpoint_cosmosdb/cosmosSerializer.py +24 -0
- langgraph_checkpoint_cosmosdb/cosmosdbSaver.py +453 -0
- langgraph_checkpoint_cosmosdb-0.2.6.dist-info/METADATA +74 -0
- langgraph_checkpoint_cosmosdb-0.2.6.dist-info/RECORD +7 -0
- langgraph_checkpoint_cosmosdb-0.2.6.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2018 The Python Packaging Authority
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
class CosmosSerializer:
|
|
4
|
+
def __init__(self, serde):
|
|
5
|
+
self.serde = serde
|
|
6
|
+
|
|
7
|
+
def dumps_typed(self, obj):
|
|
8
|
+
type_, data = self.serde.dumps_typed(obj)
|
|
9
|
+
data_base64 = base64.b64encode(data).decode('utf-8')
|
|
10
|
+
return type_, data_base64
|
|
11
|
+
|
|
12
|
+
def loads_typed(self, data):
|
|
13
|
+
type_name, serialized_obj = data
|
|
14
|
+
serialized_obj = base64.b64decode(serialized_obj.encode('utf-8'))
|
|
15
|
+
return self.serde.loads_typed((type_name, serialized_obj))
|
|
16
|
+
|
|
17
|
+
def dumps(self, obj):
|
|
18
|
+
data = self.serde.dumps(obj)
|
|
19
|
+
data_base64 = base64.b64encode(data).decode('utf-8')
|
|
20
|
+
return data_base64
|
|
21
|
+
|
|
22
|
+
def loads(self, serialized_obj):
|
|
23
|
+
serialized_obj = base64.b64decode(serialized_obj.encode('utf-8'))
|
|
24
|
+
return self.serde.loads(serialized_obj)
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# create cosmosdb Saver (langgraph checkpointer) basis dynamodb implementation in included code include=src/langgraph_dynamodb_saver/dynamodbSaver.py
|
|
2
|
+
# in case of cosmosdb PK becomes partition_key and SK will be id.
|
|
3
|
+
# @!
|
|
4
|
+
|
|
5
|
+
import copy
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
from typing import Any, Iterator, List, Optional, Tuple, Union
|
|
8
|
+
|
|
9
|
+
from langchain_core.runnables import RunnableConfig
|
|
10
|
+
|
|
11
|
+
from langgraph.checkpoint.base import WRITES_IDX_MAP, BaseCheckpointSaver, ChannelVersions, Checkpoint, CheckpointMetadata, CheckpointTuple, PendingWrite, get_checkpoint_id
|
|
12
|
+
from langgraph.checkpoint.serde.base import SerializerProtocol
|
|
13
|
+
from azure.cosmos import CosmosClient, exceptions, PartitionKey
|
|
14
|
+
from azure.cosmos.exceptions import CosmosHttpResponseError
|
|
15
|
+
from azure.identity import DefaultAzureCredential, CredentialUnavailableError
|
|
16
|
+
from langgraph_checkpoint_cosmosdb.cosmosSerializer import CosmosSerializer
|
|
17
|
+
import os
|
|
18
|
+
import asyncio
|
|
19
|
+
|
|
20
|
+
COSMOSDB_KEY_SEPARATOR = "$"
|
|
21
|
+
|
|
22
|
+
def _make_cosmosdb_checkpoint_key(thread_id: str, checkpoint_ns: str, checkpoint_id: str) -> str:
|
|
23
|
+
return COSMOSDB_KEY_SEPARATOR.join([
|
|
24
|
+
"checkpoint", thread_id, checkpoint_ns, checkpoint_id
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _make_cosmosdb_checkpoint_writes_key(thread_id: str, checkpoint_ns: str, checkpoint_id: str, task_id: str, idx: Optional[int]) -> str:
|
|
29
|
+
if idx is None:
|
|
30
|
+
return COSMOSDB_KEY_SEPARATOR.join([
|
|
31
|
+
"writes", thread_id, checkpoint_ns, checkpoint_id, task_id
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
return COSMOSDB_KEY_SEPARATOR.join([
|
|
35
|
+
"writes", thread_id, checkpoint_ns, checkpoint_id, task_id, str(idx)
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _parse_cosmosdb_checkpoint_key(cosmosdb_key: str) -> dict:
|
|
40
|
+
namespace, thread_id, checkpoint_ns, checkpoint_id = cosmosdb_key.split(
|
|
41
|
+
COSMOSDB_KEY_SEPARATOR
|
|
42
|
+
)
|
|
43
|
+
if namespace != "checkpoint":
|
|
44
|
+
raise ValueError("Expected checkpoint key to start with 'checkpoint'")
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
"thread_id": thread_id,
|
|
48
|
+
"checkpoint_ns": checkpoint_ns,
|
|
49
|
+
"checkpoint_id": checkpoint_id,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _parse_cosmosdb_checkpoint_writes_key(cosmosdb_key: str) -> dict:
|
|
54
|
+
namespace, thread_id, checkpoint_ns, checkpoint_id, task_id, idx = cosmosdb_key.split(
|
|
55
|
+
COSMOSDB_KEY_SEPARATOR
|
|
56
|
+
)
|
|
57
|
+
if namespace != "writes":
|
|
58
|
+
raise ValueError("Expected checkpoint key to start with 'writes'")
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
"thread_id": thread_id,
|
|
62
|
+
"checkpoint_ns": checkpoint_ns,
|
|
63
|
+
"checkpoint_id": checkpoint_id,
|
|
64
|
+
"task_id": task_id,
|
|
65
|
+
"idx": idx,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _filter_keys(keys: List[str], before: Optional[RunnableConfig], limit: Optional[int]) -> list:
|
|
70
|
+
if before:
|
|
71
|
+
keys = [
|
|
72
|
+
k
|
|
73
|
+
for k in keys
|
|
74
|
+
if _parse_cosmosdb_checkpoint_key(k)["checkpoint_id"]
|
|
75
|
+
< before["configurable"]["checkpoint_id"]
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
keys = sorted(
|
|
79
|
+
keys,
|
|
80
|
+
key=lambda k: _parse_cosmosdb_checkpoint_key(k)["checkpoint_id"],
|
|
81
|
+
reverse=True,
|
|
82
|
+
)
|
|
83
|
+
if limit:
|
|
84
|
+
keys = keys[:limit]
|
|
85
|
+
return keys
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _load_writes(serde: CosmosSerializer, task_id_to_data: dict[tuple[str, str], dict]) -> list[PendingWrite]:
|
|
89
|
+
writes = [
|
|
90
|
+
(
|
|
91
|
+
task_id,
|
|
92
|
+
data["channel"],
|
|
93
|
+
serde.loads_typed((data["type"], data["value"])),
|
|
94
|
+
)
|
|
95
|
+
for (task_id, _), data in task_id_to_data.items()
|
|
96
|
+
]
|
|
97
|
+
return writes
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _parse_cosmosdb_checkpoint_data(serde: CosmosSerializer, key: str, data: dict, pending_writes: Optional[List[PendingWrite]] = None) -> Optional[CheckpointTuple]:
|
|
101
|
+
if not data:
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
parsed_key = _parse_cosmosdb_checkpoint_key(key)
|
|
105
|
+
thread_id = parsed_key["thread_id"]
|
|
106
|
+
checkpoint_ns = parsed_key["checkpoint_ns"]
|
|
107
|
+
checkpoint_id = parsed_key["checkpoint_id"]
|
|
108
|
+
config = {
|
|
109
|
+
"configurable": {
|
|
110
|
+
"thread_id": thread_id,
|
|
111
|
+
"checkpoint_ns": checkpoint_ns,
|
|
112
|
+
"checkpoint_id": checkpoint_id,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
checkpoint = serde.loads_typed((data["type"], data["checkpoint"]))
|
|
117
|
+
metadata = serde.loads_typed(data["metadata"])
|
|
118
|
+
parent_checkpoint_id = data.get("parent_checkpoint_id", "")
|
|
119
|
+
parent_config = (
|
|
120
|
+
{
|
|
121
|
+
"configurable": {
|
|
122
|
+
"thread_id": thread_id,
|
|
123
|
+
"checkpoint_ns": checkpoint_ns,
|
|
124
|
+
"checkpoint_id": parent_checkpoint_id,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if parent_checkpoint_id
|
|
128
|
+
else None
|
|
129
|
+
)
|
|
130
|
+
return CheckpointTuple(
|
|
131
|
+
config=config,
|
|
132
|
+
checkpoint=checkpoint,
|
|
133
|
+
metadata=metadata,
|
|
134
|
+
parent_config=parent_config,
|
|
135
|
+
pending_writes=pending_writes,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class CosmosDBSaver(BaseCheckpointSaver):
|
|
140
|
+
container: Any
|
|
141
|
+
|
|
142
|
+
def __init__(
|
|
143
|
+
self,
|
|
144
|
+
database_name: str,
|
|
145
|
+
container_name: str,
|
|
146
|
+
reducer=None,
|
|
147
|
+
messages_key: str = "messages",
|
|
148
|
+
):
|
|
149
|
+
"""
|
|
150
|
+
Args:
|
|
151
|
+
database_name: CosmosDB database name.
|
|
152
|
+
container_name: CosmosDB container name.
|
|
153
|
+
reducer: Optional ``MessageReducer`` from ``agentstate-reducer``.
|
|
154
|
+
When set, the reducer is applied to the ``messages_key``
|
|
155
|
+
channel of every checkpoint before it is persisted.
|
|
156
|
+
messages_key: Name of the state channel that holds the message list.
|
|
157
|
+
Defaults to ``"messages"``.
|
|
158
|
+
"""
|
|
159
|
+
super().__init__()
|
|
160
|
+
self.reducer = reducer
|
|
161
|
+
self.messages_key = messages_key
|
|
162
|
+
endpoint = os.getenv("COSMOSDB_ENDPOINT")
|
|
163
|
+
if not endpoint:
|
|
164
|
+
raise ValueError("COSMOSDB_ENDPOINT environment variable is not set")
|
|
165
|
+
key = os.getenv("COSMOSDB_KEY")
|
|
166
|
+
|
|
167
|
+
try:
|
|
168
|
+
if key:
|
|
169
|
+
# Use key-based authentication
|
|
170
|
+
self.client = CosmosClient(endpoint, key)
|
|
171
|
+
self.database = self.client.create_database_if_not_exists(database_name)
|
|
172
|
+
self.container = self.database.create_container_if_not_exists(
|
|
173
|
+
id=container_name,
|
|
174
|
+
partition_key=PartitionKey(path="/partition_key")
|
|
175
|
+
)
|
|
176
|
+
else:
|
|
177
|
+
# Use default credentials (e.g., Azure Managed Identity)
|
|
178
|
+
credential = DefaultAzureCredential()
|
|
179
|
+
self.client = CosmosClient(endpoint, credential=credential)
|
|
180
|
+
self.database = self.client.get_database_client(database_name)
|
|
181
|
+
self.container = self.database.get_container_client(container_name)
|
|
182
|
+
except CredentialUnavailableError as e:
|
|
183
|
+
raise RuntimeError(
|
|
184
|
+
"Failed to obtain default credentials. Ensure the environment is correctly configured for DefaultAzureCredential."
|
|
185
|
+
) from e
|
|
186
|
+
except Exception as e:
|
|
187
|
+
raise RuntimeError("An unexpected error occurred during CosmosClient initialization.") from e
|
|
188
|
+
|
|
189
|
+
self.cosmos_serde = CosmosSerializer(self.serde)
|
|
190
|
+
self.database = self.client.get_database_client(database_name)
|
|
191
|
+
self.container = self.database.create_container_if_not_exists(
|
|
192
|
+
id=container_name,
|
|
193
|
+
partition_key=PartitionKey(path="/partition_key")
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
@classmethod
|
|
197
|
+
@contextmanager
|
|
198
|
+
def from_conn_info(cls, *, endpoint: str, key: str, database_name: str, container_name: str) -> Iterator["CosmosDBSaver"]:
|
|
199
|
+
saver = None
|
|
200
|
+
try:
|
|
201
|
+
saver = CosmosDBSaver(endpoint, key, database_name, container_name)
|
|
202
|
+
yield saver
|
|
203
|
+
finally:
|
|
204
|
+
pass
|
|
205
|
+
|
|
206
|
+
def _apply_reducer(self, checkpoint: Checkpoint) -> Checkpoint:
|
|
207
|
+
"""Return a checkpoint with the messages channel reduced (non-mutating)."""
|
|
208
|
+
if self.reducer is None:
|
|
209
|
+
return checkpoint
|
|
210
|
+
channel_values = checkpoint.get("channel_values", {})
|
|
211
|
+
messages = channel_values.get(self.messages_key)
|
|
212
|
+
if not messages:
|
|
213
|
+
return checkpoint
|
|
214
|
+
result = self.reducer.reduce(existing=messages, new=[])
|
|
215
|
+
new_channel_values = dict(channel_values)
|
|
216
|
+
new_channel_values[self.messages_key] = result.surviving
|
|
217
|
+
new_checkpoint = copy.copy(checkpoint)
|
|
218
|
+
new_checkpoint["channel_values"] = new_channel_values
|
|
219
|
+
return new_checkpoint
|
|
220
|
+
|
|
221
|
+
def put(self, config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig:
|
|
222
|
+
checkpoint = self._apply_reducer(checkpoint)
|
|
223
|
+
thread_id = config["configurable"]["thread_id"]
|
|
224
|
+
checkpoint_ns = config["configurable"]["checkpoint_ns"]
|
|
225
|
+
checkpoint_id = checkpoint["id"]
|
|
226
|
+
parent_checkpoint_id = config["configurable"].get("checkpoint_id")
|
|
227
|
+
key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, checkpoint_id)
|
|
228
|
+
partition_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, '')
|
|
229
|
+
|
|
230
|
+
type_, serialized_checkpoint = self.cosmos_serde.dumps_typed(checkpoint)
|
|
231
|
+
serialized_metadata = self.cosmos_serde.dumps_typed(metadata)
|
|
232
|
+
data = {
|
|
233
|
+
"partition_key": partition_key,
|
|
234
|
+
"id": key,
|
|
235
|
+
"checkpoint": serialized_checkpoint,
|
|
236
|
+
"type": type_,
|
|
237
|
+
"metadata": serialized_metadata,
|
|
238
|
+
"parent_checkpoint_id": parent_checkpoint_id
|
|
239
|
+
if parent_checkpoint_id
|
|
240
|
+
else "",
|
|
241
|
+
}
|
|
242
|
+
try:
|
|
243
|
+
self.container.create_item(data)
|
|
244
|
+
except CosmosHttpResponseError as e:
|
|
245
|
+
print(f"Unexpected error ({e.status_code}): {e.message}")
|
|
246
|
+
raise
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
"configurable": {
|
|
250
|
+
"thread_id": thread_id,
|
|
251
|
+
"checkpoint_ns": checkpoint_ns,
|
|
252
|
+
"checkpoint_id": checkpoint_id,
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
def put_writes(self, config: RunnableConfig, writes: List[Tuple[str, Any]], task_id: str) -> None:
|
|
257
|
+
|
|
258
|
+
thread_id = config["configurable"]["thread_id"]
|
|
259
|
+
checkpoint_ns = config["configurable"].get("checkpoint_ns", "")
|
|
260
|
+
checkpoint_id = config["configurable"]["checkpoint_id"]
|
|
261
|
+
|
|
262
|
+
# Decide on the operation: upsert or insert
|
|
263
|
+
is_upsert = all(w[0] in WRITES_IDX_MAP for w in writes)
|
|
264
|
+
|
|
265
|
+
for idx, (channel, value) in enumerate(writes):
|
|
266
|
+
key = _make_cosmosdb_checkpoint_writes_key(
|
|
267
|
+
thread_id,
|
|
268
|
+
checkpoint_ns,
|
|
269
|
+
checkpoint_id,
|
|
270
|
+
task_id,
|
|
271
|
+
WRITES_IDX_MAP.get(channel, idx),
|
|
272
|
+
)
|
|
273
|
+
partition_key = _make_cosmosdb_checkpoint_writes_key(
|
|
274
|
+
thread_id,
|
|
275
|
+
checkpoint_ns,
|
|
276
|
+
checkpoint_id,
|
|
277
|
+
'',
|
|
278
|
+
''
|
|
279
|
+
)
|
|
280
|
+
type_, serialized_value = self.cosmos_serde.dumps_typed(value)
|
|
281
|
+
|
|
282
|
+
data = {"partition_key": partition_key,"id": key, "channel": channel, "type": type_, "value": serialized_value}
|
|
283
|
+
|
|
284
|
+
if is_upsert:
|
|
285
|
+
self.container.upsert_item(data)
|
|
286
|
+
else:
|
|
287
|
+
try:
|
|
288
|
+
self.container.create_item(data)
|
|
289
|
+
except CosmosHttpResponseError as e:
|
|
290
|
+
if e.status_code != 409: # Conflict: Item already exists
|
|
291
|
+
print(f"Unexpected error ({e.status_code}): {e.message}")
|
|
292
|
+
raise
|
|
293
|
+
|
|
294
|
+
def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
|
|
295
|
+
|
|
296
|
+
thread_id = config["configurable"]["thread_id"]
|
|
297
|
+
checkpoint_id = get_checkpoint_id(config)
|
|
298
|
+
checkpoint_ns = config["configurable"].get("checkpoint_ns", "")
|
|
299
|
+
|
|
300
|
+
partition_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, '')
|
|
301
|
+
checkpoint_key = self._get_checkpoint_key(
|
|
302
|
+
self.container, thread_id, checkpoint_ns, checkpoint_id
|
|
303
|
+
)
|
|
304
|
+
if not checkpoint_key:
|
|
305
|
+
return None
|
|
306
|
+
|
|
307
|
+
checkpoint_id = _parse_cosmosdb_checkpoint_key(checkpoint_key)["checkpoint_id"]
|
|
308
|
+
|
|
309
|
+
query = "SELECT * FROM c WHERE c.partition_key=@partition_key AND c.id=@checkpoint_key"
|
|
310
|
+
parameters = [
|
|
311
|
+
{"name": "@partition_key", "value": partition_key},
|
|
312
|
+
{"name": "@checkpoint_key", "value": checkpoint_key}
|
|
313
|
+
]
|
|
314
|
+
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
|
315
|
+
checkpoint_data = items[0] if items else {}
|
|
316
|
+
|
|
317
|
+
pending_writes = self._load_pending_writes(
|
|
318
|
+
thread_id, checkpoint_ns, checkpoint_id
|
|
319
|
+
)
|
|
320
|
+
return _parse_cosmosdb_checkpoint_data(
|
|
321
|
+
self.cosmos_serde, checkpoint_key, checkpoint_data, pending_writes=pending_writes
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
def list(self, config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]:
|
|
325
|
+
thread_id = config["configurable"]["thread_id"]
|
|
326
|
+
checkpoint_ns = config["configurable"].get("checkpoint_ns", "")
|
|
327
|
+
|
|
328
|
+
partition_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, '')
|
|
329
|
+
|
|
330
|
+
query = "SELECT * FROM c WHERE c.partition_key=@partition_key"
|
|
331
|
+
parameters = [
|
|
332
|
+
{"name": "@partition_key", "value": partition_key}
|
|
333
|
+
]
|
|
334
|
+
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
|
335
|
+
|
|
336
|
+
for data in items:
|
|
337
|
+
if data and "checkpoint" in data and "metadata" in data:
|
|
338
|
+
key = data["id"]
|
|
339
|
+
checkpoint_id = _parse_cosmosdb_checkpoint_key(key)[
|
|
340
|
+
"checkpoint_id"
|
|
341
|
+
]
|
|
342
|
+
pending_writes = self._load_pending_writes(
|
|
343
|
+
thread_id, checkpoint_ns, checkpoint_id
|
|
344
|
+
)
|
|
345
|
+
yield _parse_cosmosdb_checkpoint_data(
|
|
346
|
+
self.cosmos_serde, key, data, pending_writes=pending_writes
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
def _load_pending_writes(self, thread_id: str, checkpoint_ns: str, checkpoint_id: str) -> List[PendingWrite]:
|
|
350
|
+
|
|
351
|
+
partition_key = _make_cosmosdb_checkpoint_writes_key(
|
|
352
|
+
thread_id,
|
|
353
|
+
checkpoint_ns,
|
|
354
|
+
checkpoint_id,
|
|
355
|
+
'',
|
|
356
|
+
''
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
query = "SELECT * FROM c WHERE c.partition_key=@partition_key"
|
|
361
|
+
parameters = [
|
|
362
|
+
{"name": "@partition_key", "value": partition_key}
|
|
363
|
+
]
|
|
364
|
+
writes = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
|
365
|
+
|
|
366
|
+
parsed_keys = [
|
|
367
|
+
_parse_cosmosdb_checkpoint_writes_key(write["id"]) for write in writes
|
|
368
|
+
]
|
|
369
|
+
pending_writes = _load_writes(
|
|
370
|
+
self.cosmos_serde,
|
|
371
|
+
{
|
|
372
|
+
(parsed_key["task_id"], parsed_key["idx"]): write
|
|
373
|
+
for write, parsed_key in sorted(
|
|
374
|
+
zip(writes, parsed_keys), key=lambda x: x[1]["idx"]
|
|
375
|
+
)
|
|
376
|
+
},
|
|
377
|
+
)
|
|
378
|
+
return pending_writes
|
|
379
|
+
|
|
380
|
+
def _get_checkpoint_key(self, container, thread_id: str, checkpoint_ns: str, checkpoint_id: Optional[str]) -> Optional[str]:
|
|
381
|
+
if checkpoint_id:
|
|
382
|
+
return _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, checkpoint_id)
|
|
383
|
+
|
|
384
|
+
partition_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_ns, '')
|
|
385
|
+
|
|
386
|
+
query = "SELECT * FROM c WHERE c.partition_key=@partition_key"
|
|
387
|
+
parameters = [
|
|
388
|
+
{"name": "@partition_key", "value": partition_key}
|
|
389
|
+
]
|
|
390
|
+
all_keys = list(container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
|
391
|
+
|
|
392
|
+
if not all_keys:
|
|
393
|
+
return None
|
|
394
|
+
latest_key = max(
|
|
395
|
+
all_keys,
|
|
396
|
+
key=lambda k: _parse_cosmosdb_checkpoint_key(k["id"])["checkpoint_id"],
|
|
397
|
+
)
|
|
398
|
+
return latest_key["id"]
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
|
|
402
|
+
return await asyncio.to_thread(self.get_tuple, config)
|
|
403
|
+
|
|
404
|
+
async def asnapshot(self, checkpoint: Checkpoint, config: RunnableConfig) -> None:
|
|
405
|
+
await asyncio.to_thread(
|
|
406
|
+
self.put,
|
|
407
|
+
config,
|
|
408
|
+
checkpoint,
|
|
409
|
+
CheckpointMetadata(),
|
|
410
|
+
ChannelVersions()
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
async def awrite(self, write: PendingWrite, config: RunnableConfig) -> None:
|
|
414
|
+
await asyncio.to_thread(
|
|
415
|
+
self.put_writes,
|
|
416
|
+
config,
|
|
417
|
+
[(write.channel, write.value)],
|
|
418
|
+
write.task_id
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
async def alist(self, config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]:
|
|
422
|
+
return await asyncio.to_thread(
|
|
423
|
+
lambda: list(self.list(config=config, filter=filter, before=before, limit=limit))
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
async def aput(self, config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig:
|
|
427
|
+
return await asyncio.to_thread(self.put, config, checkpoint, metadata, new_versions)
|
|
428
|
+
|
|
429
|
+
async def aput_writes(self, config: RunnableConfig, writes: Union[List[PendingWrite], List[Tuple[str, Any]]], task_id: Optional[str] = None) -> None:
|
|
430
|
+
await asyncio.to_thread(self.put_writes, config, writes, task_id)
|
|
431
|
+
|
|
432
|
+
async def adelete(self, thread_id: str, checkpoint_namespace: str, checkpoint_id: str) -> None:
|
|
433
|
+
checkpoint_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_namespace, checkpoint_id)
|
|
434
|
+
writes_key_prefix = _make_cosmosdb_checkpoint_writes_key(thread_id, checkpoint_namespace, checkpoint_id, "", "")
|
|
435
|
+
partition_key = _make_cosmosdb_checkpoint_key(thread_id, checkpoint_namespace, '')
|
|
436
|
+
try:
|
|
437
|
+
await asyncio.to_thread(self.container.delete_item, item=checkpoint_key, partition_key=partition_key)
|
|
438
|
+
except CosmosHttpResponseError as e:
|
|
439
|
+
if e.status_code != 404:
|
|
440
|
+
raise
|
|
441
|
+
query = "SELECT c.id FROM c WHERE STARTSWITH(c.id, @prefix)"
|
|
442
|
+
parameters = [{"name": "@prefix", "value": writes_key_prefix}]
|
|
443
|
+
items = await asyncio.to_thread(
|
|
444
|
+
lambda: list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
|
445
|
+
)
|
|
446
|
+
for item in items:
|
|
447
|
+
try:
|
|
448
|
+
await asyncio.to_thread(self.container.delete_item, item=item["id"], partition_key=writes_key_prefix)
|
|
449
|
+
except CosmosHttpResponseError as e:
|
|
450
|
+
if e.status_code != 404:
|
|
451
|
+
raise
|
|
452
|
+
|
|
453
|
+
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langgraph_checkpoint_cosmosdb
|
|
3
|
+
Version: 0.2.6
|
|
4
|
+
Summary: Azure CosmosDB checkpoint saver implementation for LangGraph
|
|
5
|
+
Author-email: Kamal <skamalj@github.com>
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Requires-Dist: azure-cosmos
|
|
11
|
+
Requires-Dist: azure-identity
|
|
12
|
+
Requires-Dist: langchain-core
|
|
13
|
+
Requires-Dist: langgraph
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: black; extra == 'dev'
|
|
16
|
+
Requires-Dist: isort; extra == 'dev'
|
|
17
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
18
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
20
|
+
Provides-Extra: reducer
|
|
21
|
+
Requires-Dist: agentstate-reducer>=0.1.1; extra == 'reducer'
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# LangGraph Checkpoint CosmosDB
|
|
25
|
+
|
|
26
|
+
This project provides an implementation of a checkpoint saver for LangGraph using Azure CosmosDB.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
- Save and retrieve langgraph checkpoints in Azure CosmosDB.
|
|
30
|
+
- Suppports both sync and async saver functionality
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
To install the package, ensure you have Python 3.9 or higher, and run:
|
|
35
|
+
|
|
36
|
+
```pip install langgraph-checkpoint-cosmosdb```
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### Setting Up Environment
|
|
41
|
+
|
|
42
|
+
To use the `CosmosDBSaver`
|
|
43
|
+
- You need to set CosmosDB endpoint and key if you want it to create your specified database and container.
|
|
44
|
+
```
|
|
45
|
+
export COSMOSDB_ENDPOINT='your_cosmosdb_endpoint'
|
|
46
|
+
export COSMOSDB_KEY='your_cosmosdb_key'
|
|
47
|
+
```
|
|
48
|
+
- If database and container already exists then this can work via default RBAC credentials. Ex. az login or by setting TENANT_ID, CLIENT_ID and CLIENT_SECRET in environment.
|
|
49
|
+
- For user-assigned managed identity, ensure AZURE_CLIENT_ID environment variable is set to the identity's client ID.
|
|
50
|
+
- Note that in this case error will be thrown if database and container do not exist.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## Import
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
from langgraph_checkpoint_cosmosdb import CosmosDBSaver
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Initialize the saver
|
|
61
|
+
Database and Container is created if it does not exists
|
|
62
|
+
```
|
|
63
|
+
saver = CosmosDBSaver(database_name='your_database', container_name='your_container')
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Limitations
|
|
67
|
+
List function does not support filters. You can only pass config on thread id to get the list.
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
print(list(memory.list(config=config)))
|
|
71
|
+
```
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
langgraph_checkpoint_cosmosdb/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
|
|
2
|
+
langgraph_checkpoint_cosmosdb/__init__.py,sha256=G3i7a_V2qq_cHqTX5o0qJgvoom_Iv0zIedUn5iiqiOk,176
|
|
3
|
+
langgraph_checkpoint_cosmosdb/cosmosSerializer.py,sha256=m9XQ45IaQ02xGmQ-TT0Wxj4kG5NMlDmQ5rFJEMXg5gs,829
|
|
4
|
+
langgraph_checkpoint_cosmosdb/cosmosdbSaver.py,sha256=eV_n2SuX_BCUoc_lfqqfK1OGUt9dq0kXZK02_99dJ0I,18902
|
|
5
|
+
langgraph_checkpoint_cosmosdb-0.2.6.dist-info/METADATA,sha256=8HSE7d9-zzGmpbhKAr9Jcw-AEF1NgJuMyTiCpD9XqP8,2338
|
|
6
|
+
langgraph_checkpoint_cosmosdb-0.2.6.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
7
|
+
langgraph_checkpoint_cosmosdb-0.2.6.dist-info/RECORD,,
|