GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added support for Bit objects with Psycopg 2 and pg8000 by astroknot-sheep · Pull Request #170 · pgvector/pgvector-python

Conversation

@astroknot-sheep

@astroknot-sheep astroknot-sheep commented Oct 21, 2026

Copy link
Copy Markdown

Fixes #169

Bit objects can be passed as query parameters with Psycopg 3, but not with Psycopg 2 or pg8000. This adds:

  • pgvector/psycopg2/bit.py with a BitAdapter, matching HalfvecAdapter / SparsevecAdapter, registered in register_vector
  • a Bit out adapter for pg8000 using to_text(), alongside the existing Vector one

asyncpg is unchanged (asyncpg.BitString per #110).

Tests: test_bit_class for Psycopg 2 and pg8000 (both fail on master). Full suite passes locally (527 passed, Postgres 17), plus mypy and ty check.

Written with AI assistance; I have reviewed and tested the changes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@astroknot-sheep
Added support for Bit objects with Psycopg 2 and pg8000
4764b5b
chrikrah
chrikrah approved these changes Oct 28, 2026
View reviewed changes

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astroknot-sheep this works on both drivers and brings them in line with Psycopg 3, which already adapts Bit. Reverting both register.py files fails the new tests with the #169 errors, and <~>, = and & resolve against a Bit on both drivers.

# Postgres 17 + pgvector (docker.io/pgvector/pgvector:pg17), CPython 3.12, pgvector imported from the exported tree
$ python -m pytest tests -q -p no:cacheprovider        # head 4764b5b
519 passed, 8 skipped in 29.96s

$ python -m pytest tests/test_psycopg2.py tests/test_pg8000.py -q -p no:cacheprovider -k bit_class   # head, both register.py from e0f3ac3
E       psycopg2.ProgrammingError: can't adapt type 'Bit'
E           pg8000.exceptions.DatabaseError: {... 'M': '"i" is not a valid binary digit', ...}
2 failed, 15 deselected in 0.27s

# probe at head: SELECT '111'::bit(3) <~> p1, '1010'::varbit = p2, p3::bit(3) & '011'::bit(3)
# with p1..p3 = Bit("101"), Bit("1010"), Bit("110")
psycopg2 (1.0, True, '010')
pg8000   [[1.0, True, '010']]

# asyncpg 0.32.0 at head, after register_vector(conn)
SELECT $1::bit(3) -> DataError invalid input for query argument $1: Bit(101) (a bytes-like object is required, not 'Bit')

# not run: mypy, ty

non-blocking: asyncpg has the same gap, since its register_vector sets no codec for bit. I'd keep this PR to the two drivers #169 names and track asyncpg in its own issue. Shall I open it?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@chrikrah chrikrah chrikrah approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Bit objects not supported with Psycopg 2 and pg8000

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@astroknot-sheep @chrikrah
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.