langchain-polardb-pg
LangChain integration for Alibaba Cloud PolarDB for PostgreSQL — providing in-database embeddings, vector store, and model management powered by the polar_ai extension.
Features
- In-Database Embeddings — compute text embeddings directly inside PolarDB, eliminating external API calls and network round-trips.
- Dual Embedding Modes — automatically selects the optimal path based on your cluster edition:
ai_text_embeddingmode (Standard edition): inline embedding in a single SQL statementcall_modelmode (Enterprise edition + AI node): embedding viaai_callmodel
- Vector Store — full-featured similarity search, MMR, filtering, built on community
langchain-postgres. - Model Management — register, configure, and manage AI models through Python APIs.
- Auto-Discovery —
from_instance()resolves cluster endpoints and capabilities via Alibaba Cloud OpenAPI.
Installation
pip install langchain-polardb-pg
With cloud auto-discovery support (recommended):
pip install langchain-polardb-pg[cloud]
Quick Start
from langchain_polardb_pg import PolarDBPGEngine, PolarDBPGEmbeddings, PolarDBPGVector
# 1. Connect to your PolarDB instance
engine = PolarDBPGEngine.from_instance(
cluster_id="pc-xxxxx",
database="mydb",
user="user",
password="password",
access_key_id="your-ak",
access_key_secret="your-sk",
)
# 2. Create in-database embeddings (no external API needed!)
embeddings = PolarDBPGEmbeddings.create_sync(engine)
# 3. Initialize vector store table
engine.init_vectorstore_table("my_docs", vector_size=1536)
# 4. Create vector store
store = PolarDBPGVector.create_sync(engine, embeddings, table_name="my_docs")
# 5. Add documents & search
store.add_texts(["PolarDB is a cloud-native database", "LangChain is an AI framework"])
results = store.similarity_search("cloud database", k=2)
Embedding Modes
| Mode | Edition | How it works | Vector Dim |
|---|---|---|---|
ai_text_embedding |
Standard (Beijing) | Inline SQL — embedding computed inside INSERT/SELECT | 1536 |
call_model |
Enterprise + AI Node | Two-step — ai_callmodel then INSERT/SELECT |
1024 |
The mode is auto-resolved from your cluster attributes. You can also specify it explicitly:
from langchain_polardb_pg.embeddings import EmbeddingMode
embeddings = PolarDBPGEmbeddings.create_sync(engine, mode=EmbeddingMode.AI_TEXT_EMBEDDING)
Components
PolarDBPGEngine
Extends the community PGEngine with:
from_instance()— auto-discover endpoints via Alibaba Cloud OpenAPIcluster_attribute— cached cluster metadata (region, edition, AI capabilities)get_ai_api_key()— unified AI key retrieval (from cluster or environment)
PolarDBPGEmbeddings
Implements LangChain Embeddings interface:
embed_query(text)/embed_documents(texts)— standard embedding APIsembed_query_inline(text)— returns a SQL expression for inline embedding (ai_text_embedding mode only)
PolarDBPGVector
Inherits all capabilities from community PGVectorStore:
add_documents()/add_texts()— with in-database embeddingsimilarity_search()/similarity_search_with_score()max_marginal_relevance_search()— diversity-aware retrievaldelete()— by IDs or metadata filter
PolarDBPGModelManager
Model lifecycle management:
create_model()/alter_model()/drop_model()set_model_token()/call_model()list_models()/get_model()
Connection Methods
# Method 1: Auto-discovery (recommended)
engine = PolarDBPGEngine.from_instance(
cluster_id="pc-xxxxx",
database="mydb", user="user", password="pass",
access_key_id="ak", access_key_secret="sk",
)
# Method 2: Direct connection string
engine = PolarDBPGEngine.from_connection_string(
"postgresql+asyncpg://user:pass@host:5432/mydb"
)
# Method 3: From existing SQLAlchemy engine
engine = PolarDBPGEngine.from_engine(existing_async_engine)
Requirements
- Python >= 3.9
- PolarDB for PostgreSQL with
polar_aiextension - Dependencies:
langchain-postgres,langchain-core,sqlalchemy[asyncio],asyncpg
Development
Install local development dependencies:
python -m pip install -U pip
python -m pip install -e ".[dev,cloud]"
Run unit tests:
make test
Run LangChain standard vector store integration tests with a live PolarDB instance:
make integration-test
Build and validate distribution metadata:
make build
make check-dist
License
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_polardb_pg-0.1.0.tar.gz.
File metadata
- Download URL: langchain_polardb_pg-0.1.0.tar.gz
- Upload date:
- Size: 30.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1e5cd1acb4ed3502d197dc063c2d23a7559a3fbefecca6d91658d585cfc127a
|
|
| MD5 |
36c5380a0ec00d0ea9071a7995964627
|
|
| BLAKE2b-256 |
9721666b2cebc20aaf65e3185874fd1fe708cf680b62cc1cd3981d2687304354
|
Provenance
The following attestation bundles were made for langchain_polardb_pg-0.1.0.tar.gz:
Publisher:
publish.yml on polardb/langchain-polardb-pg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_polardb_pg-0.1.0.tar.gz -
Subject digest:
c1e5cd1acb4ed3502d197dc063c2d23a7559a3fbefecca6d91658d585cfc127a - Sigstore transparency entry: 2447535160
- Sigstore integration time:
-
Permalink:
polardb/langchain-polardb-pg@5d77cf0c91d880daaa14792b20a508795132a348 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/polardb
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5d77cf0c91d880daaa14792b20a508795132a348 -
Trigger Event:
push
-
Statement type:
File details
Details for the file langchain_polardb_pg-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_polardb_pg-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e664a3b3d2029f698f4dd7058323e152e1bc2ea96955af05902deba2f8e4409d
|
|
| MD5 |
a92959792994fa87296d3cc6e6b07207
|
|
| BLAKE2b-256 |
2d9da50da670df9cd646f53b7c90025cadd736dbaf28c72eae288496670dc767
|
Provenance
The following attestation bundles were made for langchain_polardb_pg-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on polardb/langchain-polardb-pg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_polardb_pg-0.1.0-py3-none-any.whl -
Subject digest:
e664a3b3d2029f698f4dd7058323e152e1bc2ea96955af05902deba2f8e4409d - Sigstore transparency entry: 2447535679
- Sigstore integration time:
-
Permalink:
polardb/langchain-polardb-pg@5d77cf0c91d880daaa14792b20a508795132a348 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/polardb
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5d77cf0c91d880daaa14792b20a508795132a348 -
Trigger Event:
push
-
Statement type: