Skip to content

Commit fed21c6

Browse files
bsboddenabrookins
andauthored
refactor: use ULIDs instead of uuids (#4)
Co-authored-by: Andrew Brookins <[email protected]>
1 parent 192b872 commit fed21c6

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

langgraph/store/redis/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import asyncio
66
import json
77
import math
8-
import uuid
98
from contextlib import contextmanager
109
from datetime import datetime, timezone
1110
from typing import Any, Iterable, Iterator, Optional, Sequence, cast
11+
from ulid import ULID
1212

1313
from langgraph.store.base import (
1414
BaseStore,
@@ -223,7 +223,7 @@ def _batch_put_ops(
223223
# Generate IDs for PUT operations
224224
for _, op in put_ops:
225225
if op.value is not None:
226-
generated_doc_id = uuid.uuid4().hex
226+
generated_doc_id = str(ULID())
227227
namespace = _namespace_to_text(op.namespace)
228228
doc_ids[(namespace, op.key)] = generated_doc_id
229229

langgraph/store/redis/aio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import asyncio
44
import json
5-
import uuid
65
import weakref
76
from contextlib import asynccontextmanager
87
from datetime import datetime, timezone
98
from types import TracebackType
109
from typing import Any, AsyncIterator, Iterable, Optional, Sequence, cast
10+
from ulid import ULID
1111

1212
from langgraph.store.base import (
1313
BaseStore,
@@ -398,7 +398,7 @@ async def _batch_put_ops(
398398
# Generate IDs for PUT operations
399399
for _, op in put_ops:
400400
if op.value is not None:
401-
generated_doc_id = uuid.uuid4().hex
401+
generated_doc_id = str(ULID())
402402
namespace = _namespace_to_text(op.namespace)
403403
doc_ids[(namespace, op.key)] = generated_doc_id
404404

poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ python = ">=3.9,<3.13"
2121
langgraph-checkpoint = "^2.0.10"
2222
redisvl = "^0.3.9"
2323
redis = "^5.2.1"
24+
python-ulid = "^3.0.0"
2425
langgraph = "^0.2.70"
2526

2627
[tool.poetry.group.dev.dependencies]

tests/test_async_store.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"""Tests for AsyncRedisStore."""
22

3-
import uuid
43
from typing import Any, AsyncGenerator, Dict, Sequence, cast
54

65
import pytest
76
from langchain_anthropic import ChatAnthropic
87
from langchain_core.messages import BaseMessage, HumanMessage
98
from langchain_core.runnables import RunnableConfig
109
from langchain_openai import OpenAIEmbeddings
10+
from redis.asyncio import Redis
11+
from ulid import ULID
12+
13+
from langgraph.checkpoint.redis import AsyncRedisSaver
1114
from langgraph.constants import START
1215
from langgraph.graph import MessagesState, StateGraph
1316
from langgraph.store.base import (
@@ -286,6 +289,7 @@ async def test_list_namespaces(store: AsyncRedisStore) -> None:
286289
for namespace in test_namespaces:
287290
await store.adelete(namespace, "dummy")
288291

292+
289293
# TODO
290294
@pytest.mark.skip(reason="Skipping for v0.0.1 release")
291295
@pytest.mark.asyncio
@@ -527,7 +531,7 @@ def call_model(
527531
# Store new memories if the user asks the model to remember
528532
if "remember" in last_message.content.lower(): # type:ignore[union-attr]
529533
memory = "User name is Bob"
530-
store.put(namespace, str(uuid.uuid4()), {"data": memory})
534+
store.put(namespace, str(ULID()), {"data": memory})
531535

532536
messages = [{"role": "system", "content": system_msg}]
533537
messages.extend([msg.model_dump() for msg in state["messages"]])

tests/test_store.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import uuid
21
from typing import Any, Dict, Sequence, cast
32

43
import pytest
54
from langchain_anthropic import ChatAnthropic
65
from langchain_core.messages import BaseMessage, HumanMessage
76
from langchain_core.runnables import RunnableConfig
87
from langchain_openai import OpenAIEmbeddings
8+
from redis import Redis
9+
from ulid import ULID
10+
11+
from langgraph.checkpoint.redis import RedisSaver
912
from langgraph.graph import START, MessagesState, StateGraph
1013
from langgraph.store.base import (
1114
BaseStore,
@@ -19,9 +22,6 @@
1922
SearchItem,
2023
SearchOp,
2124
)
22-
from redis import Redis
23-
24-
from langgraph.checkpoint.redis import RedisSaver
2525
from langgraph.store.redis import RedisStore
2626
from tests.conftest import VECTOR_TYPES
2727
from tests.embed_test_utils import CharacterEmbeddings
@@ -464,7 +464,7 @@ def call_model(
464464
# Store new memories if the user asks the model to remember
465465
if "remember" in last_message.content.lower(): # type:ignore[union-attr]
466466
memory = "User name is Bob"
467-
store.put(namespace, str(uuid.uuid4()), {"data": memory})
467+
store.put(namespace, str(ULID()), {"data": memory})
468468

469469
messages = [{"role": "system", "content": system_msg}]
470470
messages.extend([msg.model_dump() for msg in state["messages"]])

tests/test_sync.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
from langchain_core.tools import tool
99
from langchain_core.tools.base import BaseTool
1010
from langchain_openai import ChatOpenAI
11+
from redis import Redis
12+
from redis.exceptions import ConnectionError as RedisConnectionError
13+
1114
from langgraph.checkpoint.base import (
1215
WRITES_IDX_MAP,
1316
Checkpoint,
1417
CheckpointMetadata,
1518
create_checkpoint,
1619
empty_checkpoint,
1720
)
18-
from langgraph.prebuilt import create_react_agent
19-
from redis import Redis
20-
from redis.exceptions import ConnectionError as RedisConnectionError
21-
2221
from langgraph.checkpoint.redis import BaseRedisSaver, RedisSaver
22+
from langgraph.prebuilt import create_react_agent
2323

2424

2525
@pytest.fixture

0 commit comments

Comments
 (0)