PyMilvus
Official Python SDK for Milvus, the open-source vector database. Has two coexisting APIs: the modern MilvusClient (recommended, introduced in 2.3) and the legacy ORM API (connections.connect + Collection class). Both currently work but LLM-generated and tutorial code overwhelmingly uses the old ORM patterns. Milvus Lite (embedded SQLite-based local mode) is available via the milvus-lite extra on Linux/macOS only. Server version must be kept in sync with client minor version.
Warnings
- breaking PyMilvus client version must match Milvus server minor version. pymilvus 2.6.x requires Milvus server 2.6.x. Using pymilvus 2.6 against a Milvus 2.4 server causes gRPC proto mismatch errors and silent data corruption.
- breaking Milvus Lite (.db file mode) is not available on Windows. MilvusClient('local.db') on Windows raises ModuleNotFoundError: No module named 'milvus_lite'.
- breaking ORM API (Collection class) and MilvusClient are not fully interoperable. Collections created via Collection() may not appear in client.list_collections() due to internal state management differences. Mixing the two APIs in the same codebase causes hard-to-debug visibility issues.
- gotcha MilvusClient database= parameter does not reliably set the active database context in some versions (confirmed bug in 2.5.4). Collections may be created in the default database instead of the specified one.
- gotcha Collections must be loaded into memory before search or query. client.search() on an unloaded collection returns empty results or raises a not-loaded error. Loading is asynchronous by default.
- gotcha pymilvus[model] installs heavyweight ML dependencies (torch, transformers, sentence-transformers). pip install pymilvus[model] in a lean environment adds 1–2GB of packages.
- gotcha zsh users: pip install pymilvus[model] fails with 'zsh: no matches found'. zsh treats square brackets as glob patterns.
Install
-
pip install pymilvus -
pip install 'pymilvus[model]' -
pip install 'pymilvus[milvus-lite]'
Imports
- MilvusClient (recommended)
from pymilvus import MilvusClient client = MilvusClient(uri='http://localhost:19530', token='root:Milvus') # Milvus Lite (local, no server): # client = MilvusClient('local.db') - MilvusClient uri (not host/port)
client = MilvusClient(uri='http://localhost:19530')
Quickstart
from pymilvus import MilvusClient, DataType
# Connect to running Milvus server
client = MilvusClient(uri='http://localhost:19530', token='root:Milvus')
# Milvus Lite (embedded, no server, Linux/macOS only):
# client = MilvusClient('my_data.db')
# Create collection (simple API)
client.create_collection(
collection_name='docs',
dimension=768,
)
# Insert
data = [
{'id': 1, 'vector': [0.1] * 768, 'text': 'hello world'},
]
client.insert(collection_name='docs', data=data)
# Search
results = client.search(
collection_name='docs',
data=[[0.1] * 768],
limit=5,
output_fields=['text'],
)
print(results)