Skip to content

Conversation

@DavidHein96
Copy link

This addresses #61

The default format is a a numpy float 32 to match pgvector

numpy was chosen over pytorch tensor for the lighter dependencies

This might need a special section in the docs though that to actually be able to do this you may need to coerce first to actually ensure the correct form.

def _coerce_embedding(x: NDArray[np.floating]) -> NDArray[np.float32]:
    # pgvector elements are single-precision floats; enforce dtype + 1D
    arr = np.asarray(x, dtype=np.float32)
    if arr.ndim != 1:
        raise ValueError(f"embedding must be 1-D, got shape {arr.shape}")
    return arr

As well as some kind of way of registering pgvector

from sqlalchemy import event
from pgvector.psycopg import register_vector

@event.listens_for(engine, "connect")
def _pgvector_connect(dbapi_connection, _):
    register_vector(dbapi_connection)

devstein and others added 2 commits December 16, 2025 18:31
  Add support for PostgreSQL vector type with proper numpy typing:
  - Map 'vector' type to NDArray[numpy.float32]
  - Auto-import numpy and numpy.typing.NDArray
  - Add test case for vector type generation
  - Add .DS_Store to .gitignore

  This enables type-safe pgvector usage in generated Python code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants