Skip to content

feat(rag): add Milvus provider#6503

Open
zc277584121 wants to merge 3 commits into
crewAIInc:mainfrom
zc277584121:add-milvus-rag-client
Open

feat(rag): add Milvus provider#6503
zc277584121 wants to merge 3 commits into
crewAIInc:mainfrom
zc277584121:add-milvus-rag-client

Conversation

@zc277584121

Copy link
Copy Markdown

Summary

  • Add a Milvus RAG provider backed by pymilvus.MilvusClient.
  • Wire MilvusConfig into the RAG config/factory path and add a milvus optional extra.
  • Cover real Milvus Lite behavior, schema validation, metadata filtering, score ordering, and provider docs.

Tests

  • gh pr list --repo crewAIInc/crewAI --state all --search milvus
  • gh issue list --repo crewAIInc/crewAI --state all --search milvus
  • uv lock
  • uv run --extra milvus ruff check lib/crewai/src/crewai/rag/milvus lib/crewai/src/crewai/rag/config lib/crewai/src/crewai/rag/factory.py lib/crewai/tests/rag/milvus lib/crewai/tests/rag/config/test_factory.py lib/crewai/tests/rag/config/test_optional_imports.py
  • uv run --extra milvus mypy lib/crewai/src/crewai/rag/milvus lib/crewai/src/crewai/rag/config lib/crewai/src/crewai/rag/factory.py
  • uv run --extra milvus pytest lib/crewai/tests/rag/milvus lib/crewai/tests/rag/config/test_factory.py lib/crewai/tests/rag/config/test_optional_imports.py lib/crewai/tests/rag/test_client_factory_registry.py -q
  • uv run --extra milvus pytest lib/crewai/tests/knowledge/test_knowledge_storage_integration.py lib/crewai/tests/rag/test_error_handling.py -q
  • git diff --check

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c4b2625c-9cb4-47fe-a96e-ae1ca471a80e

📥 Commits

Reviewing files that changed from the base of the PR and between 07148f8 and 34f85be.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/rag/milvus/utils.py
  • lib/crewai/tests/rag/milvus/test_utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai/src/crewai/rag/milvus/utils.py

📝 Walkthrough

Walkthrough

Added Milvus as a RAG vector-store provider, including configuration, optional dependency handling, factory dispatch, synchronous and asynchronous client operations, schema and filtering utilities, tests, and English/Arabic documentation.

Changes

Milvus RAG integration

Layer / File(s) Summary
Provider contracts and configuration
lib/crewai/pyproject.toml, lib/crewai/src/crewai/rag/config/..., lib/crewai/src/crewai/rag/milvus/types.py, lib/crewai/src/crewai/rag/milvus/config.py, lib/crewai/src/crewai/rag/milvus/constants.py
Adds the Milvus optional dependency, provider types, missing-provider handling, configuration defaults, embedding protocols, and connection parameters.
Milvus client and data operations
lib/crewai/src/crewai/rag/milvus/client.py, lib/crewai/src/crewai/rag/milvus/utils.py
Implements collection lifecycle, document upserts, synchronous and asynchronous search, schema validation, metadata filters, score normalization, result conversion, deletion, and reset operations.
Factory dispatch and provider loading
lib/crewai/src/crewai/rag/factory.py, lib/crewai/src/crewai/rag/milvus/factory.py
Routes Milvus configurations to a factory that creates the PyMilvus client wrapper.
Milvus integration and configuration tests
lib/crewai/tests/rag/config/..., lib/crewai/tests/rag/milvus/...
Tests provider dispatch, missing-dependency errors, default and custom configuration, Milvus Lite operations, async behavior, schemas, filtering, and scoring.
RAG provider documentation
docs/edge/en/concepts/knowledge.mdx, docs/edge/ar/concepts/knowledge.mdx
Documents Milvus support, Milvus Lite defaults, remote connection options, and Milvus operation examples.

Sequence Diagram(s)

sequenceDiagram
  participant RAGConfig
  participant RAGFactory
  participant MilvusFactory
  participant MilvusClient
  participant Milvus

  RAGConfig->>RAGFactory: select provider "milvus"
  RAGFactory->>MilvusFactory: require Milvus factory
  MilvusFactory->>Milvus: create pymilvus client
  MilvusFactory->>MilvusClient: wrap client and embedding function
  MilvusClient->>Milvus: create collection and upsert documents
  MilvusClient->>Milvus: search vectors with filters
  Milvus-->>MilvusClient: return search hits
  MilvusClient-->>RAGFactory: return SearchResult values
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a Milvus RAG provider.
Description check ✅ Passed The description is directly related to the changeset and accurately summarizes the Milvus provider work and tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zc277584121
zc277584121 force-pushed the add-milvus-rag-client branch from 8c3c2e6 to 4b2b4c3 Compare July 10, 2026 01:02

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/crewai/src/crewai/rag/milvus/types.py (1)

35-43: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

any_schema() accepts non-callable values for the embedding function field.

MilvusEmbeddingFunctionWrapper.__get_pydantic_core_schema__ returns core_schema.any_schema(), which means Pydantic will accept any value — including non-callables like 42 or None — for the embedding_function config field. This creates a gap between the type annotation (which expects an EmbeddingFunction) and runtime validation. If config is loaded from a file (YAML/JSON), invalid values would pass validation and only fail later with a confusing TypeError when the client invokes the function.

Consider using core_schema.callable_schema() or a custom validator to at least ensure the value is callable.

♻️ Proposed refactor
 class MilvusEmbeddingFunctionWrapper(EmbeddingFunction):
     """Base class for Milvus embedding functions used by Pydantic validation."""

     `@classmethod`
     def __get_pydantic_core_schema__(
         cls, _source_type: Any, _handler: GetCoreSchemaHandler
     ) -> CoreSchema:
         """Generate Pydantic core schema for Milvus embedding functions."""
-        return core_schema.any_schema()
+        return core_schema.callable_schema()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/rag/milvus/types.py` around lines 35 - 43, Update
MilvusEmbeddingFunctionWrapper.__get_pydantic_core_schema__ to return a schema
that validates values are callable, such as core_schema.callable_schema(), or
add an equivalent custom validator; ensure invalid values like None or numeric
values are rejected during Pydantic validation while valid embedding functions
remain accepted.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/crewai/src/crewai/rag/milvus/types.py`:
- Around line 35-43: Update
MilvusEmbeddingFunctionWrapper.__get_pydantic_core_schema__ to return a schema
that validates values are callable, such as core_schema.callable_schema(), or
add an equivalent custom validator; ensure invalid values like None or numeric
values are rejected during Pydantic validation while valid embedding functions
remain accepted.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c65fbc91-0076-4b2b-a7e6-686a7b87cb95

📥 Commits

Reviewing files that changed from the base of the PR and between 7baf8f9 and 8c3c2e6.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing (21)
  • docs/edge/ar/concepts/knowledge.mdx
  • docs/edge/en/concepts/knowledge.mdx
  • lib/crewai/pyproject.toml
  • lib/crewai/src/crewai/rag/config/optional_imports/base.py
  • lib/crewai/src/crewai/rag/config/optional_imports/protocols.py
  • lib/crewai/src/crewai/rag/config/optional_imports/providers.py
  • lib/crewai/src/crewai/rag/config/optional_imports/types.py
  • lib/crewai/src/crewai/rag/config/types.py
  • lib/crewai/src/crewai/rag/factory.py
  • lib/crewai/src/crewai/rag/milvus/__init__.py
  • lib/crewai/src/crewai/rag/milvus/client.py
  • lib/crewai/src/crewai/rag/milvus/config.py
  • lib/crewai/src/crewai/rag/milvus/constants.py
  • lib/crewai/src/crewai/rag/milvus/factory.py
  • lib/crewai/src/crewai/rag/milvus/types.py
  • lib/crewai/src/crewai/rag/milvus/utils.py
  • lib/crewai/tests/rag/config/test_factory.py
  • lib/crewai/tests/rag/config/test_optional_imports.py
  • lib/crewai/tests/rag/milvus/__init__.py
  • lib/crewai/tests/rag/milvus/test_client.py
  • lib/crewai/tests/rag/milvus/test_config.py

@zc277584121
zc277584121 force-pushed the add-milvus-rag-client branch from 4b2b4c3 to 07148f8 Compare July 15, 2026 00:32

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
lib/crewai/src/crewai/rag/milvus/utils.py (1)

351-358: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Cache the Milvus Lite version lookup instead of resolving it per search hit.

_normalize_milvus_score (called once per hit in _process_search_results's loop) invokes this function on every COSINE-metric result, and each call re-runs importlib.metadata.version("milvus-lite"), which scans installed package metadata. For searches returning many hits this adds avoidable per-result I/O; the result is invariant for the process lifetime and should be memoized.

♻️ Proposed fix: memoize the version check
+import functools
+
+
+@functools.lru_cache(maxsize=1)
 def _milvus_lite_uses_cosine_distance() -> bool:
     """Return whether the installed Milvus Lite version reports cosine distance."""
     try:
         version = importlib.metadata.version("milvus-lite")
     except importlib.metadata.PackageNotFoundError:
         return False
     base_version = version.split("+", maxsplit=1)[0]
     return base_version in MILVUS_LITE_COSINE_DISTANCE_VERSIONS
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/rag/milvus/utils.py` around lines 351 - 358, Memoize
the result of _milvus_lite_uses_cosine_distance so
importlib.metadata.version("milvus-lite") is resolved only once per process.
Preserve the existing PackageNotFoundError handling and version normalization,
using the project’s established caching approach or a suitable function-level
memoization mechanism.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/crewai/src/crewai/rag/milvus/utils.py`:
- Around line 361-374: Update _normalize_milvus_score to handle the
VALID_METRIC_TYPES entry IP explicitly instead of using the cosine-style
fallback mapping. Normalize inner-product embeddings before scoring, or define
and document an IP-specific normalization that preserves meaningful score
ordering and threshold behavior, while keeping COSINE and L2 handling unchanged.

---

Nitpick comments:
In `@lib/crewai/src/crewai/rag/milvus/utils.py`:
- Around line 351-358: Memoize the result of _milvus_lite_uses_cosine_distance
so importlib.metadata.version("milvus-lite") is resolved only once per process.
Preserve the existing PackageNotFoundError handling and version normalization,
using the project’s established caching approach or a suitable function-level
memoization mechanism.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2abebe57-53ef-41ef-be12-e1b78b6c31a1

📥 Commits

Reviewing files that changed from the base of the PR and between 4b2b4c3 and 07148f8.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing (21)
  • docs/edge/ar/concepts/knowledge.mdx
  • docs/edge/en/concepts/knowledge.mdx
  • lib/crewai/pyproject.toml
  • lib/crewai/src/crewai/rag/config/optional_imports/base.py
  • lib/crewai/src/crewai/rag/config/optional_imports/protocols.py
  • lib/crewai/src/crewai/rag/config/optional_imports/providers.py
  • lib/crewai/src/crewai/rag/config/optional_imports/types.py
  • lib/crewai/src/crewai/rag/config/types.py
  • lib/crewai/src/crewai/rag/factory.py
  • lib/crewai/src/crewai/rag/milvus/__init__.py
  • lib/crewai/src/crewai/rag/milvus/client.py
  • lib/crewai/src/crewai/rag/milvus/config.py
  • lib/crewai/src/crewai/rag/milvus/constants.py
  • lib/crewai/src/crewai/rag/milvus/factory.py
  • lib/crewai/src/crewai/rag/milvus/types.py
  • lib/crewai/src/crewai/rag/milvus/utils.py
  • lib/crewai/tests/rag/config/test_factory.py
  • lib/crewai/tests/rag/config/test_optional_imports.py
  • lib/crewai/tests/rag/milvus/__init__.py
  • lib/crewai/tests/rag/milvus/test_client.py
  • lib/crewai/tests/rag/milvus/test_config.py
🚧 Files skipped from review as they are similar to previous changes (18)
  • lib/crewai/pyproject.toml
  • lib/crewai/src/crewai/rag/config/optional_imports/types.py
  • lib/crewai/src/crewai/rag/milvus/factory.py
  • lib/crewai/tests/rag/milvus/init.py
  • lib/crewai/src/crewai/rag/milvus/init.py
  • lib/crewai/tests/rag/milvus/test_config.py
  • lib/crewai/src/crewai/rag/milvus/constants.py
  • lib/crewai/src/crewai/rag/milvus/config.py
  • lib/crewai/src/crewai/rag/factory.py
  • lib/crewai/tests/rag/config/test_optional_imports.py
  • lib/crewai/src/crewai/rag/config/optional_imports/providers.py
  • lib/crewai/tests/rag/config/test_factory.py
  • docs/edge/en/concepts/knowledge.mdx
  • lib/crewai/src/crewai/rag/config/types.py
  • lib/crewai/src/crewai/rag/config/optional_imports/protocols.py
  • lib/crewai/src/crewai/rag/milvus/types.py
  • lib/crewai/src/crewai/rag/milvus/client.py
  • lib/crewai/tests/rag/milvus/test_client.py

Comment thread lib/crewai/src/crewai/rag/milvus/utils.py
@zc277584121

Copy link
Copy Markdown
Author

Hi @lorenzejay @joaomdmoura, this Milvus RAG provider PR is ready for maintainer review when you have a chance.

It adds an optional Milvus provider following the existing RAG provider pattern, with Milvus Lite defaults, remote Milvus / Zilliz Cloud configuration, docs, and focused tests. Happy to adjust the scope or API shape to better match the project conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant