PostgreSQL vector support for asyncpg
Adds PostgreSQL vector support for Python.
Registers data types vector and halfvec from the PostgreSQL extension vector to the asynchronous PostgreSQL client asyncpg, and marshals vector data to and from PostgreSQL database tables.
Internally, the data is packed into a Python bytes object, with single-precision float vectors stored on 4 bytes per item (for class Vector) and half-precision float vectors stored on 2 bytes per item (for class HalfVector). Data is (un)packed using struct from the standard library as necessary.
This module provides functionality similar to pgvector-python but imports minimum dependencies (e.g. no dependency on numpy).
Setup
Install the package
pip install asyncpg_vector
Initialize
Register vector types with your database connection or connection pool:
Connection:
from asyncpg_vector import register_vector
async def main() -> None:
...
await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
await register_vector(conn)
Pool:
from asyncpg_vector import register_vector
async def init_connection(conn: asyncpg.Connection) -> None:
await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
await register_vector(conn)
async def main() -> None:
...
pool = await asyncpg.create_pool(..., init=init_connection)
Perform similarity search
First, create a table and an index:
async def create(conn: asyncpg.Connection) -> None:
await conn.execute("""
CREATE TABLE IF NOT EXISTS items
(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
content text NOT NULL,
embedding halfvec(1536) NOT NULL,
CONSTRAINT pk_items PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS embedding_index ON items
USING hnsw (embedding halfvec_cosine_ops);
""")
Next, find documents in a knowledge base that match a search phrase using vector similarity with approximate nearest neighbor semantics:
from asyncpg_vector import HalfVector
async def search(conn: asyncpg.Connection, phrase: str) -> list[str]:
...
embedding_response = await ai_client.embeddings.create(
input=phrase,
model="text-embedding-3-small", # aligned with `halfvec` size
encoding_format="base64", # transmits data efficiently
)
embedding = HalfVector.from_float_base64(embedding_response.data[0].embedding)
query = """
SELECT
id,
content,
embedding <=> $1 AS distance
FROM items
ORDER BY distance
LIMIT 5
"""
rows = await conn.fetch(query, embedding)
...
Metadata
Release files for asyncpg-vector 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asyncpg_vector-0.1.2.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asyncpg_vector-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.6 kB
Release files / asyncpg_vector-0.1.2.tar.gz
| Download URL | asyncpg_vector-0.1.2.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a986c835ceec0e45d672ff8288d608f954e147f15b819e84d1a86d86ca7f92bc
|
|
BLAKE2b-256 checksum How to use checksums |
a8030e382fb2ac551097803eea9eab8b99866adfa2d9c8490140c10865eb7a1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency logRelease files / asyncpg_vector-0.1.2-py3-none-any.whl
| Download URL | asyncpg_vector-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1dee02b59f4d308cf90c21a25593befc565e341866c00359a2ec53dc908441e
|
|
BLAKE2b-256 checksum How to use checksums |
8842c2a0c094ebb9887642ce993c49929548c217d88057ff20c347ab96f05d34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency log