mirror of
https://github.com/langchain-ai/langchain-postgres.git
synced 2026-07-16 01:33:18 -04:00
[PR #272] [MERGED] Feature: Support metadata based filtering for delete operations #273
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/langchain-ai/langchain-postgres/pull/272
Author: @yukiharada1228
Created: 12/17/2025
Status: ✅ Merged
Merged: 1/21/2026
Merged by: @dishaprakash
Base:
main← Head:support-metadata-based-filtering-for-delete-operations📝 Commits (9)
c2c8addUpdate README.md711714dMerge branch 'langchain-ai:main' into main09a38deAdd metadata-based filtering support for delete operations1e076deUpdate documentation and tests for metadata-based filtering in delete operationsec11d4bAdd filter parameter and documentation to delete method in AsyncPGVectorStore882c0d7Add metadata filter examples to delete operations in notebookb0b5e20Format code: adjust line breaks and simplify notebook examplese6ac1beMerge branch 'main' into support-metadata-based-filtering-for-delete-operations8d1aa1eMerge branch 'main' into support-metadata-based-filtering-for-delete-operations📊 Changes
6 files changed (+479 additions, -18 deletions)
View changed files
📝
examples/pg_vectorstore_how_to.ipynb(+62 -11)📝
langchain_postgres/v2/async_vectorstore.py(+72 -4)📝
langchain_postgres/v2/vectorstores.py(+50 -2)📝
tests/unit_tests/v2/test_async_pg_vectorstore.py(+204 -0)📝
tests/unit_tests/v2/test_pg_vectorstore.py(+78 -0)📝
uv.lock(+13 -1)📄 Description
Add metadata-based filtering support for delete operations
Summary
This PR adds support for metadata-based filtering in
delete()andadelete()methods, enabling bulk deletion of documents based on metadata criteria rather than just by IDs.Motivation
Currently, the delete methods only support deletion by document IDs, which is limiting for common use cases:
Other vector stores (Chroma, Pinecone, Weaviate) already support metadata-based deletion, and the infrastructure for metadata filtering already exists in this codebase via the
_create_filter_clause()method.Changes
Modified Files
langchain_postgres/v2/async_vectorstore.pyadelete()method to accept optionalfilterparameter_create_filter_clause()for consistent filter syntaxlangchain_postgres/v2/vectorstores.pyadelete()anddelete()methods to acceptfilterparameterTest files
metadata_columnsto ensure proper filtering behaviorUsage Examples
Setup: Define metadata columns
Delete by metadata filter only
Delete by IDs only (existing behavior)
Delete by both IDs and filter (must match both criteria)
Sync methods work identically
Filter Syntax
The
filterparameter supports the same rich filtering syntax assimilarity_search():{"field": "value"}{"field": {"$lt": 100}}($eq, $ne, $lt, $lte, $gt, $gte){"field": {"$in": [1, 2, 3]}}($in, $nin){"field": {"$like": "pattern%"}}($like, $ilike){"$and": [...]},{"$or": [...]},{"$not": {...}}{"field": {"$exists": True}}{"field": {"$between": [10, 20]}}⚠️ Important Limitation
Filters only work on fields defined in
metadata_columns, not on fields stored inmetadata_json_column.This is consistent with how
similarity_search()filtering works. To use metadata-based deletion, you must define the metadata fields as actual database columns when creating the vectorstore:Fields stored only in
metadata_json_columncannot be used in filters. This design choice provides better query performance and leverages PostgreSQL's native indexing capabilities.Implementation Details
adelete(ids=[...])continues to work unchanged_create_filter_clause()methodsimilarity_search()for consistencyTest Coverage
Added comprehensive test coverage (all tests passing):
test_adelete_with_filter: Basic metadata filter deletiontest_adelete_with_filter_and_operator: Deletion with comparison operatorstest_adelete_with_complex_filter: Complex filters with logical operatorstest_adelete_with_filter_and_ids: Combined ID and filter deletiontest_adelete_with_filter_no_matches: Graceful handling of no matchestest_adelete_with_filter(sync): Async method in sync wrappertest_delete_with_filter(sync): Sync method filteringtest_adelete: Existing tests continue to passAll tests follow existing patterns and integrate with the current test suite.
Breaking Changes
None. This is a backward-compatible enhancement:
filterparameter is optionalChecklist
rufflintingmypytype checkingRelated Issues
Closes #271
Additional Notes
Why metadata_columns are required for filtering
This implementation reuses the robust
_create_filter_clause()method that's already extensively tested for search operations. The method generates SQL WHERE clauses that operate on actual database columns, which provides:similarity_search()filteringThis design is consistent with the existing filtering implementation and aligns with how other parts of the codebase handle metadata filtering.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.