Skip to content

Commit 425cdda

Browse files
committed
fix(memory): decode pgvector values at store boundary
1 parent 8eb9f92 commit 425cdda

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎amadeus/memory/postgres.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,15 @@ def _iso(value: Any) -> str | None:
595595
def _coerce_embedding(value: Any) -> list[float]:
596596
"""Normalize a pgvector column value into a plain ``list[float]``.
597597
598-
The pgvector psycopg adapter decodes columns into ``numpy.ndarray``; the
599-
ranking layer and existing tests expect ``list[float]``. ``None`` (a null
600-
embedding) decodes to an empty list, matching the SQLite store shape.
598+
Depending on adapter configuration and version, pgvector columns decode to
599+
``Vector`` or ``numpy.ndarray``. The memory layer consistently consumes
600+
``list[float]``. ``None`` (a null embedding) decodes to an empty list,
601+
matching the SQLite store shape.
601602
"""
602603
if value is None:
603604
return []
605+
if isinstance(value, Vector):
606+
return [float(v) for v in value.to_list()]
604607
if isinstance(value, list):
605608
return [float(v) for v in value]
606609
# numpy ndarray or other sequence-like container

‎tests/memory/test_postgres_memory_store.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55

66
import psycopg
77
import pytest
8+
from pgvector import Vector
89
from amadeus.db import PostgresDatabase
9-
from amadeus.memory.postgres import PostgresMemoryStore
10+
from amadeus.memory.postgres import PostgresMemoryStore, _coerce_embedding
1011

1112
from tests.db.postgres_helpers import clean_postgres
1213

1314
EMBEDDING_DIM = 1024
1415

1516

17+
def test_coerce_embedding_accepts_pgvector_value() -> None:
18+
assert _coerce_embedding(Vector([0.25, 0.5, 0.75])) == [0.25, 0.5, 0.75]
19+
20+
1621
def _pad(values: list[float], dim: int = EMBEDDING_DIM) -> list[float]:
1722
"""Pad a small signal vector to the schema's pgvector dimension.
1823

0 commit comments

Comments
 (0)