diff --git a/libs/checkpoint-postgres/langgraph/store/postgres/aio.py b/libs/checkpoint-postgres/langgraph/store/postgres/aio.py index 2354b3a8f..a8d434360 100644 --- a/libs/checkpoint-postgres/langgraph/store/postgres/aio.py +++ b/libs/checkpoint-postgres/langgraph/store/postgres/aio.py @@ -39,14 +39,14 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con """Asynchronous Postgres-backed store with optional vector search using pgvector. !!! example "Examples" - Basic setup and key-value storage: + Basic setup and usage: ```python from langgraph.store.postgres import AsyncPostgresStore - async with AsyncPostgresStore.from_conn_string( - "postgresql://user:pass@localhost:5432/dbname" - ) as store: - await store.setup() + conn_string = "postgresql://user:pass@localhost:5432/dbname" + + async with AsyncPostgresStore.from_conn_string(conn_string) as store: + await store.setup() # Run migrations. Done once # Store and retrieve data await store.aput(("users", "123"), "prefs", {"theme": "dark"}) @@ -58,38 +58,41 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con from langchain.embeddings import init_embeddings from langgraph.store.postgres import AsyncPostgresStore + conn_string = "postgresql://user:pass@localhost:5432/dbname" + async with AsyncPostgresStore.from_conn_string( - "postgresql://user:pass@localhost:5432/dbname", + conn_string, index={ "dims": 1536, "embed": init_embeddings("openai:text-embedding-3-small"), "fields": ["text"] # specify which fields to embed. Default is the whole serialized value } ) as store: - await store.setup() # Do this once to run migrations + await store.setup() # Run migrations. Done once # Store documents await store.aput(("docs",), "doc1", {"text": "Python tutorial"}) await store.aput(("docs",), "doc2", {"text": "TypeScript guide"}) - # Don't index the following - await store.aput(("docs",), "doc3", {"text": "Other guide"}, index=False) + await store.aput(("docs",), "doc3", {"text": "Other guide"}, index=False) # don't index # Search by similarity - results = await store.asearch(("docs",), query="python programming") + results = await store.asearch(("docs",), "programming guides", limit=2) ``` Using connection pooling for better performance: ```python from langgraph.store.postgres import AsyncPostgresStore, PoolConfig + conn_string = "postgresql://user:pass@localhost:5432/dbname" + async with AsyncPostgresStore.from_conn_string( - "postgresql://user:pass@localhost:5432/dbname", + conn_string, pool_config=PoolConfig( min_size=5, max_size=20 ) ) as store: - await store.setup() + await store.setup() # Run migrations. Done once # Use store with connection pooling... ``` @@ -102,7 +105,7 @@ class AsyncPostgresStore(AsyncBatchedBaseStore, BasePostgresStore[_ainternal.Con Note: Semantic search is disabled by default. You can enable it by providing an `index` configuration when creating the store. Without this configuration, all `index` arguments passed to - `put` or `aput`will have no effect. + `put` or `aput` will have no effect. """ __slots__ = ( diff --git a/libs/checkpoint-postgres/langgraph/store/postgres/base.py b/libs/checkpoint-postgres/langgraph/store/postgres/base.py index d47a357d2..a3acc4744 100644 --- a/libs/checkpoint-postgres/langgraph/store/postgres/base.py +++ b/libs/checkpoint-postgres/langgraph/store/postgres/base.py @@ -536,18 +536,35 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]): """Postgres-backed store with optional vector search using pgvector. !!! example "Examples" - Basic setup and key-value storage: + Basic setup and usage: + ```python + from langgraph.store.postgres import PostgresStore + from psycopg import Connection + + conn_string = "postgresql://user:pass@localhost:5432/dbname" + + # Using direct connection + with Connection.connect(conn_string) as conn: + store = PostgresStore(conn) + store.setup() # Run migrations. Done once + + # Store and retrieve data + store.put(("users", "123"), "prefs", {"theme": "dark"}) + item = store.get(("users", "123"), "prefs") + ``` + + Or using the convenient from_conn_string helper: ```python from langgraph.store.postgres import PostgresStore - store = PostgresStore( - connection_string="postgresql://user:pass@localhost:5432/dbname" - ) - store.setup() + conn_string = "postgresql://user:pass@localhost:5432/dbname" - # Store and retrieve data - store.put(("users", "123"), "prefs", {"theme": "dark"}) - item = store.get(("users", "123"), "prefs") + with PostgresStore.from_conn_string(conn_string) as store: + store.setup() + + # Store and retrieve data + store.put(("users", "123"), "prefs", {"theme": "dark"}) + item = store.get(("users", "123"), "prefs") ``` Vector search using LangChain embeddings: @@ -555,23 +572,25 @@ class PostgresStore(BaseStore, BasePostgresStore[_pg_internal.Conn]): from langchain.embeddings import init_embeddings from langgraph.store.postgres import PostgresStore - store = PostgresStore( - connection_string="postgresql://user:pass@localhost:5432/dbname", + conn_string = "postgresql://user:pass@localhost:5432/dbname" + + with PostgresStore.from_conn_string( + conn_string, index={ "dims": 1536, "embed": init_embeddings("openai:text-embedding-3-small"), "fields": ["text"] # specify which fields to embed. Default is the whole serialized value } - ) - store.setup() # Do this once to run migrations + ) as store: + store.setup() # Do this once to run migrations - # Store documents - store.put(("docs",), "doc1", {"text": "Python tutorial"}) - store.put(("docs",), "doc2", {"text": "TypeScript guide"}) - store.put(("docs",), "doc2", {"text": "Other guide"}, index=False) # don't index + # Store documents + store.put(("docs",), "doc1", {"text": "Python tutorial"}) + store.put(("docs",), "doc2", {"text": "TypeScript guide"}) + store.put(("docs",), "doc2", {"text": "Other guide"}, index=False) # don't index - # Search by similarity - results = store.search(("docs",), query="python programming") + # Search by similarity + results = store.search(("docs",), "programming guides", limit=2) ``` Note: