Skip to content

fix(pgvector): use type check for numeric filter values - #22367

Open
pctablet505 wants to merge 2 commits into
run-llama:mainfrom
pctablet505:fix-15962-pgvector-numeric-filter
Open

pctablet505 wants to merge 2 commits into
run-llama:mainfrom
pctablet505:fix-15962-pgvector-numeric-filter

Conversation

@pctablet505

@pctablet505 pctablet505 commented Jul 15, 2026 •

Copy link
Copy Markdown

Fixes #15962

Replaces the float(value) try/cast in PGVectorStore._build_filter_clause with an explicit isinstance(filter_.value, (int, float)) check (excluding bool, which subclasses int). This keeps string metadata such as "2024_123" in text comparisons and avoids a PostgreSQL DataError from the ::float cast, while still using numeric comparison for actual int/float filter values. MetadataFilter.value is a Pydantic StrictInt/StrictFloat/StrictStr, so the Python type is exact and reliable to check.

Adds unit tests for underscore-separated strings, plain numeric strings, int, and float filter values. The tests call _build_filter_clause directly with a store built via __new__ (no DB connection needed), asserting the generated SQL uses ::float only for real numeric types.

AI assistance disclosure: this change was drafted with AI assistance. I reviewed the fix and tests line by line, confirmed the root cause (Python's float() accepts underscore separators like "2024_123" but PostgreSQL's ::float cast rejects them), verified that MetadataFilter.value's Pydantic Strict typing makes the isinstance check reliable, and confirmed the tests exercise the real code path (the only self reference in the tested branch is the pure _to_postgres_operator).

Avoid casting string metadata values to float by checking the Python type
of the filter value instead of calling float(). This prevents PostgreSQL
DataError for strings like '2024_123' that Python float() accepts but
PostgreSQL ::float does not.

Fixes run-llama#15962
@pctablet505
pctablet505 marked this pull request as ready for review October 21, 2026 16:34
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.

[Bug]: PGVectorStore incorrectly handles string metadata that resembles numbers

1 participant