Skip to content

Commit d4d7a2e

Browse files
committed
fix: harden RESP2 unified vector flag parsing per review
- Pair only known vector parameter keys from flags, so an unknown bare token no longer shifts the remaining key/value pairs. - Treat an unparseable dim/dims as missing, so the field is skipped with a warning instead of raising out of from_existing(). - Name the consequence in the skip warning. - Exercise protocol=2 + legacy_responses=False in the existing from_existing integration tests (sync and async). - Note why INDEXMISSING is not recovered on vector fields.
1 parent db9896f commit d4d7a2e

4 files changed

Lines changed: 116 additions & 10 deletions

File tree

‎redisvl/redis/connection.py‎

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,32 @@ def normalize_index_definition(index_info: dict[str, Any]) -> dict[str, Any]:
318318
return _normalize_index_info_mapping(index_info.get("index_definition"))
319319

320320

321-
# FT.INFO field modifiers that appear in "flags" without a value.
322-
_VECTOR_VALUELESS_FLAGS = {"INDEXMISSING", "INDEXEMPTY", "SORTABLE", "NOINDEX", "UNF"}
321+
# Vector parameter names that parse_vector_attrs reads from FT.INFO. In a
322+
# RESP2 unified ``flags`` list, only these are paired with the next token, so
323+
# an unknown bare token costs one position instead of shifting every pair.
324+
_VECTOR_PARAM_KEYS = frozenset(
325+
{
326+
"algorithm",
327+
"data_type",
328+
"datatype",
329+
"type",
330+
"dim",
331+
"dims",
332+
"distance_metric",
333+
"initial_cap",
334+
"block_size",
335+
"m",
336+
"ef_construction",
337+
"ef_runtime",
338+
"epsilon",
339+
"compression",
340+
"reduce",
341+
"graph_max_degree",
342+
"construction_window_size",
343+
"search_window_size",
344+
"training_threshold",
345+
}
346+
)
323347

324348

325349
def normalize_index_fields(index_info: dict[str, Any]) -> list[dict[str, Any]]:
@@ -383,11 +407,11 @@ def parse_vector_attrs(attrs):
383407
flags = attrs.get("flags") or []
384408
i = 0
385409
while i < len(flags):
386-
key = str(flags[i])
387-
if key.upper() in _VECTOR_VALUELESS_FLAGS or i + 1 >= len(flags):
410+
key = str(flags[i]).lower()
411+
if key not in _VECTOR_PARAM_KEYS or i + 1 >= len(flags):
388412
i += 1
389413
continue
390-
vector_attrs.setdefault(key.lower(), flags[i + 1])
414+
vector_attrs.setdefault(key, flags[i + 1])
391415
i += 2
392416
else:
393417
# Check if we have any attributes beyond the type declaration
@@ -424,10 +448,18 @@ def parse_vector_attrs(attrs):
424448
normalized = {}
425449

426450
# Handle dims/dim field - REQUIRED for vector fields
427-
if "dim" in vector_attrs:
428-
normalized["dims"] = int(vector_attrs.pop("dim"))
429-
elif "dims" in vector_attrs:
430-
normalized["dims"] = int(vector_attrs["dims"])
451+
if "dim" in vector_attrs or "dims" in vector_attrs:
452+
raw_dims = (
453+
vector_attrs.pop("dim")
454+
if "dim" in vector_attrs
455+
else vector_attrs["dims"]
456+
)
457+
try:
458+
normalized["dims"] = int(raw_dims)
459+
except (ValueError, TypeError):
460+
# Unparseable dims: fall through to the required-dims check
461+
# below, which skips the field with a warning.
462+
pass
431463
else:
432464
# If dims is missing from normal parsing, try scanning the raw attrs
433465
# This handles edge cases where the format is unexpected
@@ -622,7 +654,9 @@ def parse_attrs(attrs, field_type=None):
622654
# Skip this field - it cannot be properly reconstructed
623655
logger.warning(
624656
"Skipping vector field %r of index %r: could not parse its "
625-
"attributes from FT.INFO",
657+
"attributes from FT.INFO. The recovered schema is incomplete, "
658+
"and recreating the index from it (e.g. create(overwrite=True)) "
659+
"drops this field.",
626660
name,
627661
index_name,
628662
)

‎tests/integration/test_async_search_index.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ async def test_batch_search(async_index):
547547
reason="legacy_responses requires redis-py 8",
548548
),
549549
),
550+
pytest.param(
551+
{"protocol": 2, "legacy_responses": False},
552+
id="resp2-new-response-format",
553+
marks=pytest.mark.skipif(
554+
int(redis.__version__.split(".")[0]) < 8,
555+
reason="legacy_responses requires redis-py 8",
556+
),
557+
),
550558
],
551559
)
552560
async def test_client_from_existing_and_batch_search(

‎tests/integration/test_search_index.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,14 @@ def test_batch_search(index):
610610
reason="legacy_responses requires redis-py 8",
611611
),
612612
),
613+
pytest.param(
614+
{"protocol": 2, "legacy_responses": False},
615+
id="resp2-new-response-format",
616+
marks=pytest.mark.skipif(
617+
int(redis.__version__.split(".")[0]) < 8,
618+
reason="legacy_responses requires redis-py 8",
619+
),
620+
),
613621
],
614622
)
615623
def test_client_from_existing_and_batch_search(

‎tests/unit/test_convert_index_info.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ def test_convert_index_info_resp2_unified_vector_flags():
274274

275275
result = convert_index_info_to_schema(index_info)
276276

277+
# INDEXMISSING is deliberately not recovered as ``index_missing``: a vector
278+
# field can't be recreated with it yet, so restoring it here would turn a
279+
# quiet loss into a failure on ``create()``.
277280
assert result["fields"] == [
278281
{
279282
"name": "emb",
@@ -305,3 +308,56 @@ def test_convert_index_info_warns_on_unparseable_vector_field(caplog):
305308

306309
assert result["fields"] == []
307310
assert "'emb'" in caplog.text and "'test_index'" in caplog.text
311+
312+
313+
def test_convert_index_info_resp2_unified_flags_skip_unknown_tokens():
314+
"""An unknown bare token in ``flags`` does not shift the key/value pairs."""
315+
index_info = {
316+
"index_name": "test_index",
317+
"index_definition": {"key_type": "HASH", "prefixes": ["doc"]},
318+
"attributes": [
319+
{
320+
"identifier": "emb",
321+
"attribute": "emb",
322+
"type": "VECTOR",
323+
"flags": [
324+
"algorithm",
325+
"FLAT",
326+
"data_type",
327+
"FLOAT32",
328+
"dim",
329+
4,
330+
"NEWMOD",
331+
"distance_metric",
332+
"IP",
333+
],
334+
}
335+
],
336+
}
337+
338+
result = convert_index_info_to_schema(index_info)
339+
340+
assert result["fields"][0]["attrs"]["distance_metric"] == "ip"
341+
assert result["fields"][0]["attrs"]["dims"] == 4
342+
343+
344+
def test_convert_index_info_skips_vector_field_with_unparseable_dims(caplog):
345+
"""A non-numeric ``dim`` skips the field with a warning instead of raising."""
346+
index_info = {
347+
"index_name": "test_index",
348+
"index_definition": {"key_type": "HASH", "prefixes": ["doc"]},
349+
"attributes": [
350+
{
351+
"identifier": "emb",
352+
"attribute": "emb",
353+
"type": "VECTOR",
354+
"flags": ["algorithm", "FLAT", "dim", "FUTUREMOD"],
355+
}
356+
],
357+
}
358+
359+
with caplog.at_level("WARNING"):
360+
result = convert_index_info_to_schema(index_info)
361+
362+
assert result["fields"] == []
363+
assert "'emb'" in caplog.text

0 commit comments

Comments
 (0)