Skip to main content

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_embedding mode (Standard edition): inline embedding in a single SQL statement
    • call_model mode (Enterprise edition + AI node): embedding via ai_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-Discoveryfrom_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 OpenAPI
  • cluster_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 APIs
  • embed_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 embedding
  • similarity_search() / similarity_search_with_score()
  • max_marginal_relevance_search() — diversity-aware retrieval
  • delete() — 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_ai extension
  • 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

Apache License 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

langchain_polardb_pg-0.1.0.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_polardb_pg-0.1.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

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

Hashes for langchain_polardb_pg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c1e5cd1acb4ed3502d197dc063c2d23a7559a3fbefecca6d91658d585cfc127a
MD5 36c5380a0ec00d0ea9071a7995964627
BLAKE2b-256 9721666b2cebc20aaf65e3185874fd1fe708cf680b62cc1cd3981d2687304354

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_polardb_pg-0.1.0.tar.gz:

Publisher: publish.yml on polardb/langchain-polardb-pg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_polardb_pg-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_polardb_pg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e664a3b3d2029f698f4dd7058323e152e1bc2ea96955af05902deba2f8e4409d
MD5 a92959792994fa87296d3cc6e6b07207
BLAKE2b-256 2d9da50da670df9cd646f53b7c90025cadd736dbaf28c72eae288496670dc767

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page