Skip to content

Commit 33dee60

Browse files
ankanejackrua
andcommitted
Added support for str objects for bit type with SQLAlchemy - #137
Co-authored-by: Giacomo rua <giacomo.rua@outlook.com>
1 parent ee3e71c commit 33dee60

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.2 (unreleased)
2+
3+
- Added support for `str` objects for `bit` type with SQLAlchemy
4+
15
## 0.4.1 (2025-04-26)
26

37
- Fixed `SparseVector` constructor for SciPy sparse matrices

pgvector/sqlalchemy/bit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ def get_col_spec(self, **kw):
1414
return 'BIT'
1515
return 'BIT(%d)' % self.length
1616

17+
def bind_processor(self, dialect):
18+
if dialect.__class__.__name__ == 'PGDialect_asyncpg':
19+
import asyncpg
20+
21+
def process(value):
22+
if isinstance(value, str):
23+
return asyncpg.BitString(value)
24+
return value
25+
return process
26+
else:
27+
return super().bind_processor(dialect)
28+
1729
class comparator_factory(UserDefinedType.Comparator):
1830
def hamming_distance(self, other):
1931
return self.op('<~>', return_type=Float)(other)

tests/test_sqlalchemy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ async def test_bit(self, engine):
596596
item = await session.get(Item, 1)
597597
assert item.binary_embedding == embedding
598598

599+
if engine == asyncpg_engine:
600+
session.add(Item(id=2, binary_embedding='101'))
601+
item = await session.get(Item, 2)
602+
assert item.binary_embedding == embedding
603+
599604
await engine.dispose()
600605

601606
@pytest.mark.asyncio

0 commit comments

Comments
 (0)