Skip to content

Commit 295d8b9

Browse files
committed
refactor(client): replace all AsyncMilvusClient usage of has_collection() with list_collections()
Signed-off-by: Mustafa Elbehery <[email protected]>
1 parent 5482396 commit 295d8b9

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

llama_stack/providers/remote/vector_io/milvus/milvus.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ async def initialize(self):
6767

6868
async def delete(self):
6969
try:
70-
if await self.client.has_collection(self.collection_name):
70+
collections = await self.client.list_collections()
71+
if self.collection_name in collections:
7172
await self.client.drop_collection(collection_name=self.collection_name)
7273
except Exception as e:
7374
logger.warning(f"Failed to check or delete collection {self.collection_name}: {e}")
@@ -78,7 +79,8 @@ async def add_chunks(self, chunks: list[Chunk], embeddings: NDArray):
7879
)
7980

8081
try:
81-
collection_exists = await self.client.has_collection(self.collection_name)
82+
collections = await self.client.list_collections()
83+
collection_exists = self.collection_name in collections
8284
except Exception as e:
8385
logger.error(f"Failed to check collection existence: {self.collection_name} ({e})")
8486
# If it's an event loop issue, try to recreate the client
@@ -87,7 +89,8 @@ async def add_chunks(self, chunks: list[Chunk], embeddings: NDArray):
8789

8890
if hasattr(self, "_parent_adapter"):
8991
await self._parent_adapter._recreate_client()
90-
collection_exists = await self.client.has_collection(self.collection_name)
92+
collections = await self.client.list_collections()
93+
collection_exists = self.collection_name in collections
9194
else:
9295
# Assume collection doesn't exist if we can't check
9396
collection_exists = False

tests/unit/providers/vector_io/remote/test_milvus.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def mock_milvus_client() -> MagicMock:
4040
"""Create a mock Milvus client with common method behaviors."""
4141
client = MagicMock()
4242

43-
client.has_collection = AsyncMock(return_value=False) # Initially no collection
43+
client.list_collections = AsyncMock(return_value=[]) # Initially no collections
4444
client.create_collection = AsyncMock(return_value=None)
4545
client.drop_collection = AsyncMock(return_value=None)
4646

@@ -103,7 +103,7 @@ async def milvus_index(mock_milvus_client):
103103

104104
async def test_add_chunks(milvus_index, sample_chunks, sample_embeddings, mock_milvus_client):
105105
# Setup: collection doesn't exist initially, then exists after creation
106-
mock_milvus_client.has_collection.side_effect = [False, True]
106+
mock_milvus_client.list_collections.side_effect = [[], ["test_collection"]]
107107

108108
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
109109

@@ -120,7 +120,7 @@ async def test_query_chunks_vector(
120120
milvus_index, sample_chunks, sample_embeddings, embedding_dimension, mock_milvus_client
121121
):
122122
# Setup: Add chunks first
123-
mock_milvus_client.has_collection.return_value = True
123+
mock_milvus_client.list_collections.return_value = ["test_collection"]
124124
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
125125

126126
# Test vector search
@@ -133,7 +133,7 @@ async def test_query_chunks_vector(
133133

134134

135135
async def test_query_chunks_keyword_search(milvus_index, sample_chunks, sample_embeddings, mock_milvus_client):
136-
mock_milvus_client.has_collection.return_value = True
136+
mock_milvus_client.list_collections.return_value = ["test_collection"]
137137
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
138138

139139
# Test keyword search
@@ -146,7 +146,7 @@ async def test_query_chunks_keyword_search(milvus_index, sample_chunks, sample_e
146146

147147
async def test_bm25_fallback_to_simple_search(milvus_index, sample_chunks, sample_embeddings, mock_milvus_client):
148148
"""Test that when BM25 search fails, the system falls back to simple text search."""
149-
mock_milvus_client.has_collection.return_value = True
149+
mock_milvus_client.list_collections.return_value = ["test_collection"]
150150
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
151151

152152
# Force BM25 search to fail
@@ -188,7 +188,7 @@ async def test_bm25_fallback_to_simple_search(milvus_index, sample_chunks, sampl
188188

189189
async def test_delete_collection(milvus_index, mock_milvus_client):
190190
# Test collection deletion
191-
mock_milvus_client.has_collection.return_value = True
191+
mock_milvus_client.list_collections.return_value = ["test_collection"]
192192

193193
await milvus_index.delete()
194194

@@ -199,7 +199,7 @@ async def test_query_hybrid_search_rrf(
199199
milvus_index, sample_chunks, sample_embeddings, embedding_dimension, mock_milvus_client
200200
):
201201
"""Test hybrid search with RRF reranker."""
202-
mock_milvus_client.has_collection.return_value = True
202+
mock_milvus_client.list_collections.return_value = ["test_collection"]
203203
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
204204

205205
# Mock hybrid search results
@@ -251,7 +251,7 @@ async def test_query_hybrid_search_weighted(
251251
milvus_index, sample_chunks, sample_embeddings, embedding_dimension, mock_milvus_client
252252
):
253253
"""Test hybrid search with weighted reranker."""
254-
mock_milvus_client.has_collection.return_value = True
254+
mock_milvus_client.list_collections.return_value = ["test_collection"]
255255
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
256256

257257
# Mock hybrid search results
@@ -297,7 +297,7 @@ async def test_query_hybrid_search_default_rrf(
297297
milvus_index, sample_chunks, sample_embeddings, embedding_dimension, mock_milvus_client
298298
):
299299
"""Test hybrid search with default RRF reranker (no reranker_type specified)."""
300-
mock_milvus_client.has_collection.return_value = True
300+
mock_milvus_client.list_collections.return_value = ["test_collection"]
301301
await milvus_index.add_chunks(sample_chunks, sample_embeddings)
302302

303303
# Mock hybrid search results

0 commit comments

Comments
 (0)