langgraph-checkpoint-postgres 1.0.4__tar.gz → 1.0.6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langgraph-checkpoint-postgres
3
- Version: 1.0.4
3
+ Version: 1.0.6
4
4
  Summary: Library with a Postgres implementation of LangGraph checkpoint saver.
5
5
  Home-page: https://www.github.com/langchain-ai/langgraph
6
6
  License: MIT
@@ -11,7 +11,7 @@ Classifier: Programming Language :: Python :: 3.9
11
11
  Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
- Requires-Dist: langgraph-checkpoint (>=1.0.1,<2.0.0)
14
+ Requires-Dist: langgraph-checkpoint (>=1.0.8,<2.0.0)
15
15
  Requires-Dist: orjson (>=3.10.1)
16
16
  Requires-Dist: psycopg (>=3.0.0,<4.0.0)
17
17
  Requires-Dist: psycopg-pool (>=3.0.0,<4.0.0)
@@ -154,10 +154,11 @@ class PostgresSaver(BasePostgresSaver):
154
154
  "checkpoint_id": value["checkpoint_id"],
155
155
  }
156
156
  },
157
- {
158
- **self._load_checkpoint(value["checkpoint"]),
159
- "channel_values": self._load_blobs(value["channel_values"]),
160
- },
157
+ self._load_checkpoint(
158
+ value["checkpoint"],
159
+ value["channel_values"],
160
+ value["pending_sends"],
161
+ ),
161
162
  self._load_metadata(value["metadata"]),
162
163
  {
163
164
  "configurable": {
@@ -232,10 +233,11 @@ class PostgresSaver(BasePostgresSaver):
232
233
  "checkpoint_id": value["checkpoint_id"],
233
234
  }
234
235
  },
235
- {
236
- **self._load_checkpoint(value["checkpoint"]),
237
- "channel_values": self._load_blobs(value["channel_values"]),
238
- },
236
+ self._load_checkpoint(
237
+ value["checkpoint"],
238
+ value["channel_values"],
239
+ value["pending_sends"],
240
+ ),
239
241
  self._load_metadata(value["metadata"]),
240
242
  {
241
243
  "configurable": {
@@ -1,6 +1,6 @@
1
1
  import asyncio
2
2
  from contextlib import asynccontextmanager
3
- from typing import Any, AsyncIterator, Optional, Union
3
+ from typing import Any, AsyncIterator, Iterator, List, Optional, Union
4
4
 
5
5
  from langchain_core.runnables import RunnableConfig
6
6
  from psycopg import AsyncConnection, AsyncCursor, AsyncPipeline
@@ -51,6 +51,7 @@ class AsyncPostgresSaver(BasePostgresSaver):
51
51
  self.conn = conn
52
52
  self.pipe = pipe
53
53
  self.lock = asyncio.Lock()
54
+ self.loop = asyncio.get_running_loop()
54
55
 
55
56
  @classmethod
56
57
  @asynccontextmanager
@@ -64,7 +65,7 @@ class AsyncPostgresSaver(BasePostgresSaver):
64
65
  pipeline (bool): whether to use AsyncPipeline
65
66
 
66
67
  Returns:
67
- PostgresSaver: A new PostgresSaver instance.
68
+ AsyncPostgresSaver: A new AsyncPostgresSaver instance.
68
69
  """
69
70
  async with await AsyncConnection.connect(
70
71
  conn_string, autocommit=True, prepare_threshold=0, row_factory=dict_row
@@ -137,12 +138,12 @@ class AsyncPostgresSaver(BasePostgresSaver):
137
138
  "checkpoint_id": value["checkpoint_id"],
138
139
  }
139
140
  },
140
- {
141
- **self._load_checkpoint(value["checkpoint"]),
142
- "channel_values": await asyncio.to_thread(
143
- self._load_blobs, value["channel_values"]
144
- ),
145
- },
141
+ await asyncio.to_thread(
142
+ self._load_checkpoint,
143
+ value["checkpoint"],
144
+ value["channel_values"],
145
+ value["pending_sends"],
146
+ ),
146
147
  self._load_metadata(value["metadata"]),
147
148
  {
148
149
  "configurable": {
@@ -196,12 +197,12 @@ class AsyncPostgresSaver(BasePostgresSaver):
196
197
  "checkpoint_id": value["checkpoint_id"],
197
198
  }
198
199
  },
199
- {
200
- **self._load_checkpoint(value["checkpoint"]),
201
- "channel_values": await asyncio.to_thread(
202
- self._load_blobs, value["channel_values"]
203
- ),
204
- },
200
+ await asyncio.to_thread(
201
+ self._load_checkpoint,
202
+ value["checkpoint"],
203
+ value["channel_values"],
204
+ value["pending_sends"],
205
+ ),
205
206
  self._load_metadata(value["metadata"]),
206
207
  {
207
208
  "configurable": {
@@ -329,3 +330,96 @@ class AsyncPostgresSaver(BasePostgresSaver):
329
330
  binary=True, row_factory=dict_row
330
331
  ) as cur:
331
332
  yield cur
333
+
334
+ def list(
335
+ self,
336
+ config: Optional[RunnableConfig],
337
+ *,
338
+ filter: Optional[dict[str, Any]] = None,
339
+ before: Optional[RunnableConfig] = None,
340
+ limit: Optional[int] = None,
341
+ ) -> Iterator[CheckpointTuple]:
342
+ """List checkpoints from the database.
343
+
344
+ This method retrieves a list of checkpoint tuples from the Postgres database based
345
+ on the provided config. The checkpoints are ordered by checkpoint ID in descending order (newest first).
346
+
347
+ Args:
348
+ config (Optional[RunnableConfig]): Base configuration for filtering checkpoints.
349
+ filter (Optional[Dict[str, Any]]): Additional filtering criteria for metadata.
350
+ before (Optional[RunnableConfig]): If provided, only checkpoints before the specified checkpoint ID are returned. Defaults to None.
351
+ limit (Optional[int]): Maximum number of checkpoints to return.
352
+
353
+ Yields:
354
+ Iterator[CheckpointTuple]: An iterator of matching checkpoint tuples.
355
+ """
356
+ aiter_ = self.alist(config, filter=filter, before=before, limit=limit)
357
+ while True:
358
+ try:
359
+ yield asyncio.run_coroutine_threadsafe(
360
+ anext(aiter_), self.loop
361
+ ).result()
362
+ except StopAsyncIteration:
363
+ break
364
+
365
+ def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
366
+ """Get a checkpoint tuple from the database.
367
+
368
+ This method retrieves a checkpoint tuple from the Postgres database based on the
369
+ provided config. If the config contains a "checkpoint_id" key, the checkpoint with
370
+ the matching thread ID and "checkpoint_id" is retrieved. Otherwise, the latest checkpoint
371
+ for the given thread ID is retrieved.
372
+
373
+ Args:
374
+ config (RunnableConfig): The config to use for retrieving the checkpoint.
375
+
376
+ Returns:
377
+ Optional[CheckpointTuple]: The retrieved checkpoint tuple, or None if no matching checkpoint was found.
378
+ """
379
+ return asyncio.run_coroutine_threadsafe(
380
+ self.aget_tuple(config), self.loop
381
+ ).result()
382
+
383
+ def put(
384
+ self,
385
+ config: RunnableConfig,
386
+ checkpoint: Checkpoint,
387
+ metadata: CheckpointMetadata,
388
+ new_versions: ChannelVersions,
389
+ ) -> RunnableConfig:
390
+ """Save a checkpoint to the database.
391
+
392
+ This method saves a checkpoint to the Postgres database. The checkpoint is associated
393
+ with the provided config and its parent config (if any).
394
+
395
+ Args:
396
+ config (RunnableConfig): The config to associate with the checkpoint.
397
+ checkpoint (Checkpoint): The checkpoint to save.
398
+ metadata (CheckpointMetadata): Additional metadata to save with the checkpoint.
399
+ new_versions (ChannelVersions): New channel versions as of this write.
400
+
401
+ Returns:
402
+ RunnableConfig: Updated configuration after storing the checkpoint.
403
+ """
404
+ return asyncio.run_coroutine_threadsafe(
405
+ self.aput(config, checkpoint, metadata, new_versions), self.loop
406
+ ).result()
407
+
408
+ def put_writes(
409
+ self,
410
+ config: RunnableConfig,
411
+ writes: List[tuple[str, Any]],
412
+ task_id: str,
413
+ ) -> None:
414
+ """Store intermediate writes linked to a checkpoint.
415
+
416
+ This method saves intermediate writes associated with a checkpoint to the database.
417
+
418
+ Args:
419
+ config (RunnableConfig): Configuration of the related checkpoint.
420
+ writes (Sequence[Tuple[str, Any]]): List of writes to store, each as (channel, value) pair.
421
+ task_id (str): Identifier for the task creating the writes.
422
+ """
423
+ return asyncio.run_coroutine_threadsafe(
424
+ self.aput_writes(config, writes, task_id), self.loop
425
+ ).result()
@@ -1,4 +1,3 @@
1
- from base64 import b64decode, b64encode
2
1
  from hashlib import md5
3
2
  from typing import Any, List, Optional, Tuple
4
3
 
@@ -13,7 +12,7 @@ from langgraph.checkpoint.base import (
13
12
  get_checkpoint_id,
14
13
  )
15
14
  from langgraph.checkpoint.serde.jsonplus import JsonPlusSerializer
16
- from langgraph.checkpoint.serde.types import ChannelProtocol
15
+ from langgraph.checkpoint.serde.types import TASKS, ChannelProtocol
17
16
 
18
17
  MetadataInput = Optional[dict[str, Any]]
19
18
 
@@ -58,7 +57,7 @@ MIGRATIONS = [
58
57
  "ALTER TABLE checkpoint_blobs ALTER COLUMN blob DROP not null;",
59
58
  ]
60
59
 
61
- SELECT_SQL = """
60
+ SELECT_SQL = f"""
62
61
  select
63
62
  thread_id,
64
63
  checkpoint,
@@ -77,12 +76,20 @@ select
77
76
  ) as channel_values,
78
77
  (
79
78
  select
80
- array_agg(array[cw.task_id::text::bytea, cw.channel::bytea, cw.type::bytea, cw.blob])
79
+ array_agg(array[cw.task_id::text::bytea, cw.channel::bytea, cw.type::bytea, cw.blob] order by cw.task_id, cw.idx)
81
80
  from checkpoint_writes cw
82
81
  where cw.thread_id = checkpoints.thread_id
83
82
  and cw.checkpoint_ns = checkpoints.checkpoint_ns
84
83
  and cw.checkpoint_id = checkpoints.checkpoint_id
85
- ) as pending_writes
84
+ ) as pending_writes,
85
+ (
86
+ select array_agg(array[cw.type::bytea, cw.blob] order by cw.idx)
87
+ from checkpoint_writes cw
88
+ where cw.thread_id = checkpoints.thread_id
89
+ and cw.checkpoint_ns = checkpoints.checkpoint_ns
90
+ and cw.checkpoint_id = checkpoints.parent_checkpoint_id
91
+ and cw.channel = '{TASKS}'
92
+ ) as pending_sends
86
93
  from checkpoints """
87
94
 
88
95
  UPSERT_CHECKPOINT_BLOBS_SQL = """
@@ -116,25 +123,23 @@ class BasePostgresSaver(BaseCheckpointSaver):
116
123
 
117
124
  jsonplus_serde = JsonPlusSerializer()
118
125
 
119
- def _load_checkpoint(self, checkpoint: dict[str, Any]) -> Checkpoint:
120
- if len(checkpoint["pending_sends"]) == 2 and all(
121
- isinstance(a, str) for a in checkpoint["pending_sends"]
122
- ):
123
- type, bs = checkpoint["pending_sends"]
124
- return {
125
- **checkpoint,
126
- "pending_sends": self.serde.loads_typed((type, b64decode(bs))),
127
- }
128
-
129
- return checkpoint
130
-
131
- def _dump_checkpoint(self, checkpoint: Checkpoint) -> dict[str, Any]:
132
- type, bs = self.serde.dumps_typed(checkpoint["pending_sends"])
126
+ def _load_checkpoint(
127
+ self,
128
+ checkpoint: dict[str, Any],
129
+ channel_values: list[tuple[bytes, bytes, bytes]],
130
+ pending_sends: list[tuple[bytes, bytes]],
131
+ ) -> Checkpoint:
133
132
  return {
134
133
  **checkpoint,
135
- "pending_sends": (type, b64encode(bs).decode()),
134
+ "pending_sends": [
135
+ self.serde.loads_typed((c.decode(), b)) for c, b in pending_sends or []
136
+ ],
137
+ "channel_values": self._load_blobs(channel_values),
136
138
  }
137
139
 
140
+ def _dump_checkpoint(self, checkpoint: Checkpoint) -> dict[str, Any]:
141
+ return {**checkpoint, "pending_sends": []}
142
+
138
143
  def _load_blobs(
139
144
  self, blob_values: list[tuple[bytes, bytes, bytes]]
140
145
  ) -> dict[str, Any]:
@@ -255,7 +260,8 @@ class BasePostgresSaver(BaseCheckpointSaver):
255
260
  if config:
256
261
  wheres.append("thread_id = %s ")
257
262
  param_values.append(config["configurable"]["thread_id"])
258
- if checkpoint_ns := config["configurable"].get("checkpoint_ns"):
263
+ checkpoint_ns = config["configurable"].get("checkpoint_ns")
264
+ if checkpoint_ns is not None:
259
265
  wheres.append("checkpoint_ns = %s")
260
266
  param_values.append(checkpoint_ns)
261
267
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "langgraph-checkpoint-postgres"
3
- version = "1.0.4"
3
+ version = "1.0.6"
4
4
  description = "Library with a Postgres implementation of LangGraph checkpoint saver."
5
5
  authors = []
6
6
  license = "MIT"
@@ -10,13 +10,13 @@ packages = [{ include = "langgraph" }]
10
10
 
11
11
  [tool.poetry.dependencies]
12
12
  python = "^3.9.0,<4.0"
13
- langgraph-checkpoint = "^1.0.1"
13
+ langgraph-checkpoint = "^1.0.8"
14
14
  orjson = ">=3.10.1"
15
15
  psycopg = "^3.0.0"
16
16
  psycopg-pool = "^3.0.0"
17
17
 
18
18
  [tool.poetry.group.dev.dependencies]
19
- ruff = "^0.1.4"
19
+ ruff = "^0.6.2"
20
20
  codespell = "^2.2.0"
21
21
  pytest = "^7.2.1"
22
22
  anyio = "^4.4.0"