Repository navigation
fix(vector_store): return stored metadata in MilvusCollection.search() (#1330) - #1343
Conversation
|
ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing |
PR Summary by QodoReturn stored metadata from Milvus collection searches
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1.
|
447430c to
a64bc80
Compare
|
Fixed both issues flagged by the Qodo review: 1. Duplicate output_fields crash — Instead of passing 2. Canonical schema test — Added All 16 tests pass (4 new focused tests + 12 existing schema tests). |
There was a problem hiding this comment.
The core fix looks right.
There's one thing this broke though. TestVectorStoreDeepDive::test_milvus_store in tests/vector_store/test_vector_store_deepdive.py builds a Milvus mock hit with only .id and .distance set, no entity.get stub, same pattern that broke TestMilvusSearchSchema in test_search_result_schema.py. That one got the mock_hit.entity.get.return_value = None fix but this one didn't. Ran it: passes on main, fails on this branch with
AssertionError: <MagicMock name='mock.entity.get()' id=...> is not an instance of <class 'dict'>
Since MagicMock auto-attributes are truthy, hit.entity.get("metadata") or {} returns the mock itself instead of falling through to {}..
zeekay
left a comment
There was a problem hiding this comment.
The premise checks out, and the comment this replaces was the actually-wrong part. MilvusStore.create_collection defines the schema at milvus_store.py:388-394:
FieldSchema(name="id", dtype=DataType.VARCHAR, is_primary=True, ...),
FieldSchema(name="vector", dtype=DataType.FLOAT_VECTOR, dim=dimension),
FieldSchema(name="metadata", dtype=DataType.JSON),So the comment at line 211 — "Milvus collection schema stores only id+vector; no metadata field is defined in create_collection()" — has been false for as long as that schema has existed, and search() was returning {} on the strength of it. The rest of the file already reads the field (output_fields at lines 656, 715, 773), which is what makes search() the odd one out: get_metadata() and filter_by_metadata() returned real values while search() returned nothing.
Popping output_fields out of **options before merging is the right call rather than an afterthought — collection.search(output_fields=..., **options) with a caller-supplied output_fields would have been a TypeError, and that's exactly the sort of thing that only shows up for the one user who passes it.
Test runs against current main (merged, the branch is behind): pytest tests/vector_store/ -m "not integration" — 345 passed, 84 skipped, 7 xfailed, against 341 passed on main. The four new tests are the difference.
Two small things I checked rather than assumed:
list({"metadata", *caller_fields})has nondeterministic order, but the test asserts"metadata" in output_fieldsrather than list equality, so it won't flake. Worth keeping that assertion as-is if anyone edits it later.- The
or {}fallback and the corresponding one-line update totest_milvus_search_canonical_schemakeep the "legacy record with no metadata" path returning{}rather thanNone, so the result schema stays stable for existing callers.
I don't have write access on this repo, so I can't merge it — approving so a maintainer can.
zeekay
left a comment
There was a problem hiding this comment.
Correcting my own approval above — @ZohaibHassan16 is right, and I missed it because of how I ran the suite. Reproduced on this branch merged onto current main:
tests/vector_store/test_vector_store_deepdive.py::TestVectorStoreDeepDive::test_milvus_store
> self.assertIsInstance(results[0]["metadata"], dict)
E AssertionError: <MagicMock name='mock.entity.get()' ...> is not an instance of <class 'dict'>
1 failed, 10 deselected
Passes on main, fails here. The cause is the one they gave: the mock hit in that test sets only .id and .distance, so hit.entity.get("metadata") returns an auto-created MagicMock, which is truthy, so or {} never fires and the mock itself lands in the result dict. It's the same underspecified mock you already fixed in test_search_result_schema.py — this file just needs the same mock_hit.entity.get.return_value = None.
Worth knowing why I didn't catch it: tests/vector_store/test_vector_store_deepdive.py:19 sets pytestmark = pytest.mark.integration for the whole module, so pytest -m "not integration" skips it — that's the "11 deselected" in the 345-passed run I quoted. The file only uses mocks and needs no Milvus server, so the marker is wrong, but until someone fixes that this failure is invisible to the standard local command and CI doesn't run pytest at all. If you're touching it anyway, dropping the module-level marker would stop the next person from hitting the same blind spot.
Everything else in my earlier review stands: the schema really does define FieldSchema(name="metadata", dtype=DataType.JSON), the comment you're replacing was false, and popping output_fields out of **options is the right way to avoid the duplicate-keyword TypeError. This is a one-line test fix away from being ready.
Closes #1330.
Summary
MilvusCollection.search()contained a stale comment claiming the collection schema only storesidandvectorwith no metadata field. That description was accurate once, butcreate_collection()has long defined aFieldSchema(name="metadata", dtype=DataType.JSON)at line 393.As a result of the stale comment,
search()hardcoded"metadata": {}in every result, silently discarding metadata that was actually stored — making it inconsistent withget_metadata()andfilter_by_metadata(), which both request and return metadata correctly.Changes
semantica/vector_store/milvus_store.pyMilvusCollection.search()output_fields=["metadata"]to theself.collection.search()call so the Milvus SDK returns the stored metadata field alongside each hit."metadata": {}→"metadata": hit.entity.get("metadata") or {}so callers receive the actual stored value. Falls back to{}for legacy records where the field is absent.tests/vector_store/test_milvus_store_search_metadata.py[new]Four focused unit tests (no real Milvus connection needed — all collection calls are mocked):
test_search_returns_stored_metadatatest_search_requests_metadata_output_fieldoutput_fields=["metadata"]is passed to the SDKtest_search_falls_back_to_empty_dict_when_metadata_is_noneNoneentity value yields{}, notNonetest_search_score_calculationVerification