wmilvus
wmilvus is an enterprise-grade, type-safe Milvus Vector Database client wrapper for Python designed with high-level Pydantic ORM model mapping, vector search, multi-collection routing, async support, and audit trail features.
Key Features
- Pydantic Model Declarations — Define collections as standard Pydantic models with vector annotations (
FieldVector,MetricType,IndexType).FieldVectorautomatically defaults todefault_factory=listto prevent missing field validation errors during deserialization. - Explicit Schema Generation & Auto-Migration — Every attribute defined in your Pydantic model (
str,int,float,bool,list[str],list[int],dict,datetime) is automatically mapped to dedicated Milvus columns (VARCHAR,INT64,DOUBLE,BOOL,ARRAY,JSON) rather than falling into generic dynamic fields ($meta). Existing collections automatically evolve by auto-adding missing columns (add_collection_field) upon client initialization. - Collection Lifecycle & Dropping (
drop,drop_collection) — Conveniently drop collections from Milvus viadb[Model].drop()ordb.drop_collection(Model)across synchronous and asynchronous clients. - Automatic Vector Retrieval & Preservation — Query methods (
get_by_field,filter_by) automatically include vector fields by default (include_vector=True). Callingupdate()with an empty vector automatically preserves the pre-existing vector from Milvus, eliminating dimension mismatch crashes on partial or metadata-only updates. - Dynamic Vector Fields & Auto-ID — Native payload generation supporting custom vector attribute names (
embedding,face_vec, etc.) and automatedINT64primary key generation without requiring manual IDs. - Universal Primary Key Support — Full compatibility with both auto-increment integer (
INT64) and string (VARCHAR) primary keys across all CRUD, filtering, and search routines. - Collection Partitioning & Multi-Tenancy — Route insertions, queries, and searches to specific Milvus partitions (
partition_name/partition_names). - High-Scale Streaming Iterators (
iter_all) — Memory-efficient batch streaming across millions of vectors using native Milvus query iterators. - Typed Filter Query Builder (
filter_by) — Declarative keyword querying with comparison operators (age__gte=18,status__in=[1, 2],frames_ids__contains='f1',dni='00000000P'). - Vector Similarity & Range Search — KNN similarity search (
search_similar), similarity distance scores (search_similar_with_scores), and similarity range threshold search (search_range) with customizable search index parameters (search_params). - Batch & Expression Deletion — Flexible record removal via single ID (
delete), batch IDs (delete_batch), or scalar filter expressions (delete_by_expr). - Multi-Vector & Hybrid Search Rankers — Dense + scalar hybrid search with direct support for Milvus rerankers (
RRFRanker,WeightedRanker). - High-Performance Non-Blocking Writes & Explicit Flushing — In adherence to official Milvus architecture best practices, automatic forced
flush()calls after everyinsert,insert_batch, andupdatehave been removed (auto_flush=Falseby default). This eliminates severe I/O latency bottlenecks, query node memory saturation, and tiny segment fragmentation caused by sealing growing segments prematurely. If immediate read-after-write consistency is needed, Milvus's nativeconsistency_level="Strong"or"Session"is recommended. Manual flushing is available viadb.flush()ordb[Model].flush()when required administratively. - Asynchronous Forensic Audit Dispatcher (
ForensicAuditDispatcher) — Decouples audit trail writes (_forensic_audit_log) into a background daemon queue with micro-batching. This reduces single-item write latency onForensicModelby 100-220ms per operation without sacrificing audit guarantees (synchronized on process termination, client close, or manual flush). - Transparent Write-Behind Buffer (
write_buffer=True) — High-performance write buffer for microservices that issue single-record inserts or updates. Operations return immediately (<10ms) while a background daemon coalesces writes into sub-second Milvus upsert batches (e.g. 25ms window or up to 50 items), cutting round-trip latency by over 80%. - High-Speed Batch Updates (
update_batch) — Update multiple records in a single round-trip viadb.update_batch([records])across synchronous and asynchronous clients, with vector preservation and batched audit trails. - In-Memory Ghost Collection Caching — Verifications of collection existence for
_forensic_audit_logare memoized in memory, eliminating redundant gRPChas_collectioncalls on every write. - Selective Scalar Querying for Updates — Pre-update queries for
ForensicModelaudit snapshots automatically exclude 512-dimension vectors when the incoming record already supplies the vector, saving bandwidth and network overhead. - Bulk Vector Ingestion — High-performance chunked bulk insertion (
insert_batch) with automatic payload chunking and ID propagation. - Async Client Support (
AsyncWMilvus) — Full non-blocking async mirror withasync withcontext manager for FastAPI and asyncio applications. - Schema Verification & Dimension Mismatch Detection — Verification of Pydantic model vector dimensions against active Milvus collection schemas with explicit warning logging when existing server collections deviate from declared dimensions.
- Optional Vector Fields & Scalar-Only Collections — Collections do not strictly require vector fields. Pure scalar or metadata models (e.g., users, categories, configurations, relational entities) can be defined without any vector fields. WMilvus transparently satisfies Milvus's internal vector requirement under the hood with lightweight synthetic dummy vectors, enabling full type-safe CRUD, filtering, streaming, and multi-table querying without imposing vectors on your models.
- Single & Multi-Collection ORM Routing — Automatic collection creation and full multi-table query support: call
db.get_all()across all registered collections simultaneously, scope queries by model class (db.get_all(Model)ordb[Model].get_all()), or access repositories directly by attribute (db.modelname). - Enterprise Ghost Audit Log (
_forensic_audit_log) — Global audit trail trackingINSERT,UPDATE,SOFT_DELETE, andHARD_DELETEoperations for models inheriting fromForensicModelwith safe JSON ISO-8601 datetime serialization. - Automated Collection Memory Loading — Transparent auto-loading (
load_collection) into memory for pre-existing or offline Milvus collections upon connection. - Flexible Client Instantiation — Versatile initialization supporting
WMilvus(uri, models=[...]),WMilvus(models=[...], uri=...), and config dictionaries without rigid positional restrictions. - SQLite Vector Backup & Import (
wsqlite) — Native ORM-based backup export (export_to_sqlite) and restoration (import_from_sqlite) powered bywsqlite. - Dockerized Test Runner & Coverage — Out-of-the-box support for isolated container testing (
run_tests_docker.sh) and HTML coverage reports (run_coverage.sh).
Key Technologies and Libraries
| Component | Technology |
|---|---|
| Language | Python 3.9+ |
| Vector Engine | Milvus 2.3+ |
| SDK Core | pymilvus 2.3+ |
| Backup Storage | wsqlite 1.5+ |
| Data Validation | Pydantic 2.x |
| Numerical Core | NumPy |
| Logging | Loguru |
| CLI Framework | Click |
| Packaging & Build | setuptools>=61.0, wheel |
| Testing Framework | pytest, pytest-cov |
Quick Start
from typing import List
from pydantic import BaseModel
from wmilvus import FieldVector, MetricType, WMilvus
class UserFace(BaseModel):
id: str
name: str
face_embedding: List[float] = FieldVector(dim=128, metric_type=MetricType.COSINE)
# Context Manager Usage
with WMilvus(UserFace, uri="http://localhost:19530") as db:
# 1. Insert Pydantic record
user = UserFace(id="u_001", name="William Rodriguez", face_embedding=[0.1] * 128)
db.insert(user)
# 2. Vector Similarity Search returning typed Pydantic instances
matches = db.search_similar(vector=[0.1] * 128, top_k=5)
print(f"Top match: {matches[0].name}")
# 3. Vector Search with Similarity Scores
matches_with_scores = db.search_similar_with_scores(vector=[0.1] * 128, top_k=5)
print(f"Score: {matches_with_scores[0].distance:.4f}")
Async Quick Start
import asyncio
from wmilvus import AsyncWMilvus, FieldVector, MetricType
from pydantic import BaseModel
class Telemetry(BaseModel):
id: str
vector: List[float] = FieldVector(dim=4, metric_type=MetricType.COSINE)
async def main():
async with AsyncWMilvus(Telemetry, uri="http://localhost:19530") as db:
await db.insert(Telemetry(id="t1", vector=[0.5, 0.5, 0.0, 0.0]))
results = await db.search_similar(vector=[0.5, 0.5, 0.1, 0.0], top_k=1)
print(results[0].id)
asyncio.run(main())
Write Performance & Consistency
By default in wmilvus>=0.4.6, automatic flush() calls during insert(), insert_batch(), and update() are disabled (auto_flush=False). Calling flush() after every write is strongly discouraged by official Milvus documentation because it forcibly seals growing segments in memory, writes tiny files to storage, and triggers expensive background compactions.
High-Throughput Write-Behind Buffer (write_buffer=True)
When microservices are architecturally constrained to call insert() or update() one record at a time, each standalone network call against Milvus can incur significant network and proxy overhead (typically ~600ms per update due to Milvus's tombstone deletion and indexing mechanism).
To solve this without modifying calling microservices:
# Enable transparent write-behind micro-batching
with WMilvus(
UserFace,
uri="http://localhost:19530",
write_buffer=True,
buffer_max_size=50,
buffer_flush_interval=0.025,
) as db:
# Individual calls return immediately (<10ms)
db.insert(user1)
db.update("u_001", user1_updated)
# Writes are seamlessly batched and flushed in the background
High-Speed Batch Updates (update_batch)
To update multiple entities in a single round-trip:
updated_users = [user1_mod, user2_mod, user3_mod]
db.update_batch(updated_users)
# Async support
await async_db.update_batch(updated_users)
Non-Blocking Forensic Audit Dispatcher
When using ForensicModel, WMilvus automatically offloads audit trail persistence to an asynchronous background worker (ForensicAuditDispatcher) with in-memory caching. This removes over 150-200ms of latency per write while guaranteeing all audit entries are safely flushed on process termination (atexit), close(), or flush().
Read-After-Write Consistency & Manual Flushing
- Immediate Visibility / Read-After-Write Consistency: If your application or tests require immediate visibility of newly inserted entities without incurring I/O penalties, use Milvus's native
consistency_level="Strong"or"Session":db = WMilvus(UserFace, uri="http://localhost:19530", consistency_level="Strong")
- Manual Administrative Flush: When necessary (e.g., before stopping an instance or following an offline migration), trigger a flush explicitly:
# Flush all registered collections db.flush() # Flush a single collection db[UserFace].flush() # Asynchronous flush await async_db.flush()
- Per-operation Auto-Flush: If you explicitly want a specific write operation to trigger a flush:
db.insert(user, auto_flush=True) db.insert_batch(users, auto_flush=True) db.update("u_001", user, auto_flush=True)
Organized Examples
The repository includes organized example scripts in examples/:
examples/01_crud/example.py— Single-collection CRUD operations.examples/02_vector_search/example.py— Vector similarity KNN search, distance scores, and scalar metadata filtering.examples/03_range_search/example.py— Similarity range threshold search (radius filtering).examples/04_batch_ingestion/example.py— Bulk vector ingestion with automatic payload chunking (insert_batch).examples/05_async_client/example.py— Async non-blocking client (AsyncWMilvus) for asyncio/FastAPI.examples/06_schema_verification/example.py— Milvus collection schema verification (verify_schema).examples/07_hybrid_search/example.py— Hybrid similarity search (search_hybrid).examples/08_sqlite_backup/example.py— SQLite vector backup export and import powered bywsqlite.examples/16_forensic_fields/example.py— Forensic field tracking (create_by,create_in,update_by).examples/17_multi_table/example.py— Multi-collection management & repository routing.examples/18_ghost_table_audit/example.py— Enterprise global audit trail (_forensic_audit_log).examples/19_auto_id_and_int64_pk/example.py— Auto-incrementINT64primary key handling and model back-population.examples/20_query_builder_filter_by/example.py— Fluent query builder with comparison operators (__gte,__in,__lt) and chained pagination.examples/21_streaming_iter_all/example.py— Memory-efficient streaming iterators (iter_all) across large collections.examples/22_partition_routing/example.py— Creating collection partitions, targeted partition ingestion, and partition-scoped search.examples/23_batch_and_expr_deletion/example.py— High-performance batch ID deletion and conditional deletion via Milvus boolean expressions.examples/24_scalar_models/example.py— Purely scalar / metadata models without vector fields operating alongside vector collections.examples/25_high_performance_writes/example.py— High-throughput writes via transparentWriteBuffermicro-batching andupdate_batch.
Running Tests
Execute unit tests inside an isolated Docker container:
./run_tests_docker.sh
Calculate code coverage locally:
./run_coverage.sh
Metadata
Release files for wmilvus 0.4.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wmilvus-0.4.7.tar.gz | 46.5 kB | Without provenance | Details for wmilvus-0.4.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wmilvus-0.4.7-py3-none-any.whl | Python 3 | none | any | Details for wmilvus-0.4.7-py3-none-any.whl |
Total release size: 77.0 kB
Release files / wmilvus-0.4.7.tar.gz
| Download URL | wmilvus-0.4.7.tar.gz |
|---|---|
| Size | 46.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ceb7ba0a23a3b804fd3e55fcdae44305f1638175062249e298de74c2a3f6940
|
|
BLAKE2b-256 checksum How to use checksums |
8145dcaea99125916c93f4dd1d86a79abaeec9661d36a0e1c2f6aa74e661e0bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / wmilvus-0.4.7-py3-none-any.whl
| Download URL | wmilvus-0.4.7-py3-none-any.whl |
|---|---|
| Size | 30.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
837c618da63f705bfb8b71d08d5b86578edf3e5372d09bbabab9e2650e45b4cd
|
|
BLAKE2b-256 checksum How to use checksums |
0a352a474a6b26ea5348af14279b53adf45107d8dc27f1d35457867cb2f27d61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Security for wmilvus 0.4.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.