Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion langchain_postgres/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ async def __afrom(
**kwargs: Any,
) -> PGVector:
if ids is None:
ids = [str(uuid.uuid1()) for _ in texts]
ids = [str(uuid.uuid4()) for _ in texts]

if not metadatas:
metadatas = [{} for _ in texts]
Expand Down
43 changes: 43 additions & 0 deletions tests/unit_tests/v1/test_afrom_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Auto-generated ids from the async constructors must be random UUIDv4.

These are pure-Python tests: constructing ``PGVector`` with ``async_mode=True``
defers ``__post_init__``, so no database connection is opened, and
``aadd_embeddings`` is stubbed out to capture the ids the constructor generates.
"""

import uuid
from typing import Any, List, Optional
from unittest.mock import patch

import pytest

from langchain_postgres.vectorstores import PGVector
from tests.unit_tests.fake_embeddings import FakeEmbeddings

# A well-formed connection string. ``create_async_engine`` is lazy, so this does
# not open a socket, and ``aadd_embeddings`` is patched before any I/O happens.
FAKE_ASYNC_CONNECTION = "postgresql+psycopg://user:pw@localhost:5432/db"


@pytest.mark.asyncio
async def test_afrom_texts_generates_uuid4_ids() -> None:
"""``afrom_texts`` should mint random (v4) ids, matching the sync path."""
captured: dict = {}

async def _capture(
*args: Any, ids: Optional[List[str]] = None, **kwargs: Any
) -> None:
captured["ids"] = ids

with patch.object(PGVector, "aadd_embeddings", _capture):
await PGVector.afrom_texts(
texts=["foo", "bar", "baz"],
embedding=FakeEmbeddings(),
connection=FAKE_ASYNC_CONNECTION,
)

ids = captured["ids"]
assert ids is not None and len(ids) == 3
# uuid1 embeds the host MAC address and a timestamp; the sync `from_texts`
# uses uuid4, so the async path must too.
assert all(uuid.UUID(i).version == 4 for i in ids)