🦜️🔗 LangChain AgensGraph
LangChain integration for AgensGraph, Skai's PostgreSQL-based multi-model graph database. Ships a GraphStore, a pgvector-backed VectorStore, chat-message history, a LangGraph checkpointer, an LLM graph transformer, and a connection-pooling engine — with async variants throughout.
What's new in 0.2.0
A ground-up modernization for LangChain 1.x and AgensGraph 2.17, with a full set of production components.
Compatibility & packaging
- Targets
langchain-core1.x; no dependency on the archivedlangchain-community— theGraphStore,GraphDocument, andDistanceStrategytypes are vendored locally. - Python 3.10–3.14;
uv+hatchlingbuild (PEP 621).
Graph + vector store
AgensGraphandAgensgraphVectorwith full sync and async surfaces (aquery,asimilarity_search,aadd_texts,adelete,aget_by_ids,aclose, …).delete,get_by_ids,effective_search_ratioover-fetch, andbatch_size/embed_batch_sizefor production ingest.- The internal system id is stored under
__id__, so user metadata"id"round-trips intact andDocument.idis populated on retrieval. add_graph_documentsruns in a single transaction — partial failures roll back cleanly, no orphan nodes.- Passes LangChain's standard
langchain_tests.integration_tests.VectorStoreIntegrationTestsconformance suite.
New components
AgensEngine— a shareablepsycopgconnection pool (sync + async). Passengine=to share one pool across anAgensGraphand multipleAgensgraphVectorstores, so concurrent requests stop serializing on one connection. Without it, behavior is unchanged.AgensChatMessageHistory—BaseChatMessageHistorystoring a session's messages as an ordered chain of graph vertices; sync + async; per-session isolation; optionalwindow.AgensSaver/AsyncAgensSaver— a LangGraphBaseCheckpointSaverthat persists agent state to the graph so threads resume across restarts. Drop-incheckpointer=AgensSaver(graph=...).LLMGraphTransformer— text→graph extraction via any chat model'swith_structured_output; feeds straight intoadd_graph_documents.
AgensGraph 2.17 & ergonomics
- Schema introspection uses the
metaextension when present (meta.vertex_labels,meta.edge_labels, …), falling back to catalog scans on older versions; multi-label nodes and NULL-safe type detection. - Connection lifecycle:
close()/aclose(), sync & async context managers, andapplication_nametagging forpg_stat_activity. - Query
timeout(per-instance and per-call) and asanitizeflag that strips oversized list properties from results. - Typed
IndexConfig(HNSWm/ef_construction, IVFFlatlists) andHybridSearchConfig(reciprocal rank fusionrank_constant+ per-modality weights). enhanced_schema=Truesamples example property values into the schema for better Text2Cypher prompting.- Bug fixes:
IVFFLATenum value was"IVFLLAT"(extra L → pgvector rejected the DDL); strayprint("DEBUG: ...")calls replaced withlogger.debug;_format_propertiesnow escapes apostrophes/backslashes.
Installation
pip install -U langchain-agensgraph
AgensGraph requirements
AgensGraph 2.17+ is recommended. AgensGraph does not bundle the pgvector or meta extensions; build and install them against your AgensGraph install's pg_config:
# pgvector
git clone https://github.com/pgvector/pgvector.git
cd pgvector && PG_CONFIG=/path/to/agens/bin/pg_config make && make install
# meta extension (ships in AgensGraph's contrib/)
cd /path/to/agensgraph/contrib/meta
PG_CONFIG=/path/to/agens/bin/pg_config make USE_PGXS=1 install
# in your AgensGraph database:
CREATE EXTENSION vector;
CREATE EXTENSION meta;
The integration works without meta (falls back to ag_label catalog scans) but refresh_schema is much faster with it.
Usage
AgensGraph (graph store)
from langchain_agensgraph import AgensGraph
conf = {
"dbname": "...",
"user": "...",
"password": "...",
"host": "...",
"port": 5432,
}
graph = AgensGraph(graph_name="my_graph", conf=conf, create=True)
graph.query("MATCH (n) RETURN n LIMIT 1")
# Optional: cache the schema between refreshes (seconds)
graph = AgensGraph(graph_name="my_graph", conf=conf, schema_cache_ttl=60)
# Async
results = await graph.aquery("MATCH (n) RETURN count(n) AS c")
await graph.aclose()
AgensgraphVector (vector store)
from langchain_agensgraph import AgensgraphVector
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
db = AgensgraphVector.from_documents(
docs,
embeddings,
url="postgresql://user:pwd@host:5432/db",
)
# Search
docs_with_score = db.similarity_search_with_score("What is LangChain?", k=4)
# Higher recall — fetch 3× candidates from the ANN index, then trim to k
hits = db.similarity_search("...", k=10, effective_search_ratio=3.0)
# Mutation
db.add_texts(["...", "..."], ids=["a", "b"], batch_size=500)
db.delete(["a"])
got = db.get_by_ids(["b"])
# Async
hits = await db.asimilarity_search("...", k=10)
await db.aadd_texts(["..."], batch_size=500)
await db.aclose()
Shared connection pool
from langchain_agensgraph import AgensEngine, AgensGraph, AgensgraphVector
engine = AgensEngine.from_url("postgresql://user:pwd@host:5432/db", min_size=2, max_size=20)
graph = AgensGraph("my_graph", conf={...}, engine=engine, create=True)
store = AgensgraphVector(embeddings, graph_name="my_graph", engine=engine)
# ... concurrent requests each borrow their own pooled connection ...
engine.close()
Production tips
- Connection pooling: use
AgensEngine(backed bypsycopg-pool) and share it across your graph and vector stores so concurrent requests don't serialize on a single connection. - PgBouncer transaction mode: AgensGraph speaks the standard PG wire protocol, so PgBouncer works unchanged. In transaction-pool mode, disable psycopg's server-side prepared-statement cache (
prepare_threshold=None). - HNSW + AgensGraph 2.17: two June-2026 commits (
e7e1be9,47b38ed) finally makeCREATE PROPERTY INDEX ... USING HNSW (((embedding)::vector(N)) vector_cosine_ops)use anIndex Scanplan instead of falling back to seq-scan. If you see seq-scan on v2.17 with a small table, that's expected — the planner picks seq-scan when it's cheaper. auto_gather_graphmeta: enable on the database (ALTER DATABASE x SET auto_gather_graphmeta = on) for ~30× fasterDETACH DELETEon large graphs.
Compatibility
Old (0.1.0) |
New (0.2.0) |
|
|---|---|---|
langchain-core |
>=0.3.34,<1.0.0 |
>=1.0.0,<2.0.0 |
langchain-community |
required | not used |
langgraph |
— | >=1.0.0,<2.0.0 (checkpointer) |
| Python | 3.9–3.12 | 3.10–3.14 |
| Build system | Poetry | hatchling (PEP 621) |
Document.id after retrieval |
unset | set to internal __id__ |
User metadata key "id" |
clobbered our system id | round-trips intact |
add_graph_documents |
per-statement commit | single transaction |
| Connection model | one connection | optional pooled AgensEngine |
| Components | graph + vector | + chat history, checkpointer, transformer |
License
Apache-2.0.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_agensgraph-0.2.0.tar.gz.
File metadata
- Download URL: langchain_agensgraph-0.2.0.tar.gz
- Upload date:
- Size: 71.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
024f6fa1d50f0ffb628a8d795afbdaf7a517880961cc6a1122a9edeea6a1f788
|
|
| MD5 |
70ebcf6ec1be19e920e580c08fcfd48f
|
|
| BLAKE2b-256 |
6015d575bba41af442dd15f9d9cc8f31c93931d0287f9df9f9808df840295fb6
|
File details
Details for the file langchain_agensgraph-0.2.0-py3-none-any.whl.
File metadata
- Download URL: langchain_agensgraph-0.2.0-py3-none-any.whl
- Upload date:
- Size: 56.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c04c96a186f761a56ec11bc311eda03ea6589b0d3d15298d3097e4fe90ae5ff
|
|
| MD5 |
e2e22664e55994c59756e53e0baee38c
|
|
| BLAKE2b-256 |
68ce32ad1d02e4f279f77f5f4f5a18d08a4eb8171c4ca5608305a9b6502347e7
|