Vector Database Plugin Architecture#
Dify's vector database (VDB) system is fully pluggable: each backend is a standalone Python workspace package under api/providers/vdb/ that advertises itself via a dify.vector_backends importlib entry point. The core RAG layer discovers and loads backends at runtime β no import-time registration, no monolithic switch statement.
30 backends ship out-of-the-box : Milvus, Qdrant, pgvector, Weaviate, Chroma, Elasticsearch, and more. The VectorType StrEnum lists all recognized type strings .
Key Components#
| Layer | File | Role |
|---|---|---|
| Entry-point registry | vector_backend_registry.py | Discovers and caches AbstractVectorFactory classes via importlib.metadata.entry_points |
| Public facade | vector_factory.py | Vector class β wraps a BaseVector and handles batching, lazy embeddings, dedup, and Redis cache invalidation |
| Plugin contract | vector_base.py | BaseVector ABC |
| Type registry | vector_type.py | VectorType StrEnum mapping human names to entry-point keys |
How Discovery Works#
get_vector_factory_class(vector_type) is the single entry point for loading a backend:
- Cache check β returns the in-process cached class if already loaded .
- Entry-point scan β iterates
entry_points(group="dify.vector_backends")and matches byep.name == vector_type. - Builtin fallback β checks
_BUILTIN_VECTOR_FACTORY_TARGETS(currently empty dict; reserved for test stubs) . - Error β if nothing matched, raises
ValueErrorlisting all installed backends .
After uv sync, each installed package's dist-info contains the entry-point metadata. The registry never imports a backend package unless that backend type is actually requested.
Plugin Package Structure#
Each providers/vdb/vdb-<name>/ directory is a uv workspace member . Its pyproject.toml must declare:
- Dependencies β backend-specific client library (e.g.
weaviate-client==4.22.0,pymilvus~=2.6.12) - Entry point β one line mapping the
VectorTypestring to the factory class:
[project.entry-points."dify.vector_backends"]
weaviate = "dify_vdb_weaviate.weaviate_vector:WeaviateVectorFactory"
The factory class must implement AbstractVectorFactory.init_vector(dataset, attributes, embeddings) β BaseVector.
BaseVector Contract#
All backends implement BaseVector (abstract methods):
| Method | Purpose |
|---|---|
create() | Bulk-insert documents + pre-computed embeddings |
add_texts() | Incremental insert |
text_exists(id) | Duplicate check by doc_id |
delete_by_ids(ids) | Delete by object/vector IDs |
delete_by_metadata_field(key, value) | Metadata-filtered bulk delete |
search_by_vector(query_vector) | ANN search |
search_by_full_text(query) | BM25 / keyword search |
delete() | Drop entire collection |
Dependency Management#
api/pyproject.toml uses a uv workspace . The default-groups list includes vdb-all , so a bare uv sync installs all 30 backends. To install a single backend:
uv sync --group vdb-milvus # installs only dify-vdb-milvus
Individual groups are defined at . After adding or modifying a plugin package, run uv sync to regenerate dist-info so entry points match pyproject.toml .
Runtime Selection#
Vector._init_vector() determines which backend to use:
- If the dataset already has an
index_struct_dict, itstypefield is used (e.g., the collection was created with a specific backend). - Otherwise, falls back to
dify_config.VECTOR_STORE(theVECTOR_STOREenvironment variable).
The Vector class also uses a _LazyEmbeddings proxy so delete/cleanup paths never trigger billing-API calls just to instantiate the embedding model.
Adding a New Backend#
- Create
api/providers/vdb/vdb-<name>/with apyproject.tomldeclaring the entry point underdify.vector_backends. - Implement
AbstractVectorFactoryandBaseVectorsubclasses. - Add
dify-vdb-<name>to the workspace sources and dependency groups inapi/pyproject.toml. - Add the type string to
VectorType. - Run
uv sync.
Key reference files:
api/core/rag/datasource/vdb/vector_backend_registry.pyβ discovery logicapi/core/rag/datasource/vdb/vector_factory.pyβVectorfacade andAbstractVectorFactoryapi/core/rag/datasource/vdb/vector_base.pyβBaseVectorcontractapi/core/rag/datasource/vdb/vector_type.pyβVectorTypeenumapi/providers/vdb/vdb-weaviate/pyproject.tomlβ example plugin manifestapi/pyproject.tomlβ workspace and dependency group declarations