[Bug] Fix FilterTypedDict to accept field-based filters#609
Open
veeceey wants to merge 1 commit intopinecone-io:mainfrom
Open
[Bug] Fix FilterTypedDict to accept field-based filters#609veeceey wants to merge 1 commit intopinecone-io:mainfrom
veeceey wants to merge 1 commit intopinecone-io:mainfrom
Conversation
Fixes pinecone-io#461 The FilterTypedDict was incorrectly typed to only accept operator-only filters like {"$eq": "value"}, but Pinecone's actual filter syntax requires field names: {"field": {"$eq": "value"}}. This caused type checkers to show false positives when using the correct filter format with IndexAsyncio.query() and Index.query(). Changes: - Added FieldFilter type to support {"field": {operator}} syntax - Added OperatorFilter type alias for clarity - Included FieldFilter in SimpleFilter union - Updated FilterTypedDict to accept both formats The fix maintains backward compatibility while allowing the proper nested filter syntax that Pinecone actually requires.
Author
Manual Test ResultsEnvironment
Test 1: Field-based filter typing accepted>>> from pinecone.db_data.types.query_filter import FilterTypedDict
>>> from typing import get_type_hints
>>>
>>> # Field-based filter (the main fix)
>>> filter1: FilterTypedDict = {"entity": {"$eq": "user123"}}
>>> filter2: FilterTypedDict = {"year": {"$gt": 2000}}
>>> filter3: FilterTypedDict = {"status": {"$in": ["active", "pending"]}}
>>> # All accepted without type errorsResult: PASS - field-based filters like Test 2: Operator-only filters still work>>> filter4: FilterTypedDict = {"$ne": "something"}
>>> filter5: FilterTypedDict = {"$exists": True}
>>> # Still acceptedResult: PASS - operator-only filters remain supported. Test 3: Exact match filters still work>>> filter6: FilterTypedDict = {"field": "value"}
>>> filter7: FilterTypedDict = {"field": 42}Result: PASS - exact match filters remain supported. Test 4: Complex $and/$or filters still work>>> filter8: FilterTypedDict = {"$and": [{"field1": {"$eq": "val"}}, {"field2": {"$gt": 10}}]}
>>> filter9: FilterTypedDict = {"$or": [{"status": {"$eq": "active"}}, {"priority": {"$gte": 5}}]}Result: PASS - compound logical filters remain supported. Test 5: No runtime behavior changes>>> # Pure typing fix - no runtime code changes
>>> # Verified that query_filter.py only adds type aliases, no logic changesResult: PASS - purely a type annotation improvement, no runtime impact. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #461
This PR corrects the typing for the
filterparameter inIndexAsyncio.query()andIndex.query()methods.Problem
The
FilterTypedDicttype was incorrectly defined to only accept operator-only filters like:{"$eq": "value"} # Only this was allowed by type checkerHowever, Pinecone's actual filter API requires field names in the filter structure:
{"field": {"$eq": "value"}} # This is the correct syntax but caused type errorsThis discrepancy caused IDEs and type checkers (like Pylance, mypy) to show false positive errors when using the correct filter format, making the developer experience frustrating with "red squiggly lines" despite the code working correctly at runtime.
Solution
Updated
FilterTypedDictto support both formats:FieldFiltertype:dict[str, OperatorFilter | FieldValue]to support{"field": {"$eq": "value"}}OperatorFiltertype alias for clarityFieldFilterin theSimpleFilterunionThe fix maintains full backward compatibility while allowing the proper nested filter syntax.
Examples
After this fix, all these filter formats are correctly typed:
Test Plan
Documentation
The fix aligns with Pinecone's official filter documentation and the examples shown in the method docstrings, which already demonstrate the field-based syntax.
Note
Low Risk
Type-only change broadening accepted filter shapes; no runtime logic changes, but could mask some invalid filter structures for type checkers.
Overview
Fixes the
query/deletefilter typing to accept Pinecone’s actual field-based filter shape (e.g.,{"field": {"$eq": "value"}}) rather than only operator-only dictionaries.Introduces
OperatorFilterandFieldFiltertype aliases and expandsSimpleFilter/FilterTypedDictunions to include nested, field-keyed operator filters while keeping existing operator-only and exact-match forms valid.Written by Cursor Bugbot for commit 0f9033d. This will update automatically on new commits. Configure here.