Repository navigation
fix(mcp): allow locked exists on vector fields with index_missing #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Purv-Kabaria
wants to merge
2
commits into
redis:main
Choose a base branch
from
Purv-Kabaria:fix/mcp-locked-exists-vector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
118 changes: 118 additions & 0 deletions
118
tests/integration/test_mcp/test_locked_exists_vector.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """A locked ``exists`` filter on a vector field, checked against live Redis. | ||
|
|
||
| ``validate_profile_against_schema`` accepts ``exists`` on a vector field only | ||
| when the field declares ``index_missing``, on the grounds that Redis then answers | ||
| ``ismissing()`` for it. These tests pin that assumption: if ``FT.CREATE`` ever | ||
| stopped carrying ``INDEXMISSING`` for a vector field, the validator would still | ||
| pass the profile and every call would fail as a retryable backend error. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from redisvl.index import SearchIndex | ||
| from redisvl.mcp.filters import parse_filter | ||
| from redisvl.query import FilterQuery, VectorQuery | ||
| from redisvl.redis.connection import supports_svs | ||
| from redisvl.redis.utils import array_to_buffer | ||
| from redisvl.schema import IndexSchema | ||
|
|
||
| DIMS = 4 | ||
| EMBEDDING = [0.1, 0.2, 0.3, 0.4] | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| params=[ | ||
| pytest.param({"algorithm": "flat"}, id="flat"), | ||
| pytest.param({"algorithm": "hnsw"}, id="hnsw"), | ||
| pytest.param({"algorithm": "svs-vamana"}, id="svs-vamana"), | ||
| ] | ||
| ) | ||
| def vector_attrs(request, client): | ||
| if request.param["algorithm"] == "svs-vamana" and not supports_svs(client): | ||
| pytest.skip("SVS-VAMANA requires Redis 8.2+ with SVS support") | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture(params=["hash", "json"]) | ||
| def storage_type(request): | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def index_with_missing_embedding( | ||
| redis_url, redis_test_name, vector_attrs, storage_type | ||
| ): | ||
| name = redis_test_name("locked_exists_vector") | ||
| schema = IndexSchema.from_dict( | ||
| { | ||
| "index": { | ||
| "name": name, | ||
| "prefix": name, | ||
| "storage_type": storage_type, | ||
| }, | ||
| "fields": [ | ||
| {"name": "doc_id", "type": "tag"}, | ||
| { | ||
| "name": "embedding", | ||
| "type": "vector", | ||
| "attrs": { | ||
| **vector_attrs, | ||
| "dims": DIMS, | ||
| "distance_metric": "cosine", | ||
| "datatype": "float32", | ||
| "index_missing": True, | ||
| }, | ||
| }, | ||
| ], | ||
| } | ||
| ) | ||
| index = SearchIndex(schema=schema, redis_url=redis_url) | ||
| index.create(overwrite=True, drop=True) | ||
|
|
||
| embedding = ( | ||
| array_to_buffer(EMBEDDING, "float32") if storage_type == "hash" else EMBEDDING | ||
| ) | ||
| index.load( | ||
| [ | ||
| {"doc_id": "with-embedding", "embedding": embedding}, | ||
| {"doc_id": "without-embedding"}, | ||
| ], | ||
| id_field="doc_id", | ||
| ) | ||
|
|
||
| yield index | ||
|
|
||
| index.delete(drop=True) | ||
|
|
||
|
|
||
| def _locked_exists_filter(index): | ||
| # The same parser a profile's lock.filter goes through at startup. | ||
| return parse_filter({"field": "embedding", "op": "exists"}, index.schema) | ||
|
|
||
|
|
||
| def test_locked_exists_on_vector_field_as_filter(index_with_missing_embedding): | ||
| index = index_with_missing_embedding | ||
| locked = _locked_exists_filter(index) | ||
| assert str(locked) == "(-ismissing(@embedding))" | ||
|
|
||
| results = index.query( | ||
| FilterQuery(filter_expression=locked, return_fields=["doc_id"]) | ||
| ) | ||
|
|
||
| assert [doc["doc_id"] for doc in results] == ["with-embedding"] | ||
|
|
||
|
|
||
| def test_locked_exists_on_vector_field_as_knn_prefilter(index_with_missing_embedding): | ||
| index = index_with_missing_embedding | ||
| query = VectorQuery( | ||
| vector=np.array(EMBEDDING, dtype=np.float32).tolist(), | ||
| vector_field_name="embedding", | ||
| return_fields=["doc_id"], | ||
| filter_expression=_locked_exists_filter(index), | ||
| num_results=10, | ||
| ) | ||
|
|
||
| results = index.query(query) | ||
|
|
||
| assert [doc["doc_id"] for doc in results] == ["with-embedding"] |
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
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.
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.
This test lets the profile through on the grounds that Redis answers
ismissing()on the vector field. Nothing intests/integrationchecks that, though:ismissingdoesn't appear there at all. IfFT.CREATEever stopped carryingINDEXMISSINGfor a vector field, this validator would still pass the profile, and every call would fail as a retryable backend error. That's the failure mode the validator exists to prevent.Could you add one integration test? It would create a vector field with
index_missing: True, load one document with an embedding and one without, and assert that(-ismissing(@embedding))returns only the first, both as a filter and as a KNN prefilter. I checked that behaviour by hand on 8.2, 8.4 and 8.10, for FLAT, HNSW and SVS-VAMANA, on HASH and JSON, so the test should pass on every CI version.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.
@Purv-Kabaria once this change is in, I can merge the PR 👍
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.
Fixed in 9620555. Added integration coverage using RedisVL's own
SearchIndex.create: one document with an embedding and one without, asserting that(-ismissing(@embedding))selects only the first, both as a filter and as a KNN prefilter. The matrix covers HASH/JSON with FLAT, HNSW and SVS-VAMANA. All 12 cases and the 598 MCP unit tests pass against redis:8.10.3; formatting and mypy pass. The Reviewer Brief is updated. Thanks for spelling out the backend failure this needs to catch.