crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] ChromaDB ingestion fails for batches mixing documents with and without metadata

Open MoAftab-01 opened this issue 3 weeks ago • 0 comments

Describe the bug

ChromaDB ingestion fails when a batch mixes documents without metadata and documents with populated metadata.

In lib/crewai/src/crewai/rag/chromadb/utils.py, _create_batch_slice() produces empty dictionaries for documents without metadata. For a mixed batch, the metadata payload is:

[{}, {"source": "local"}]

ChromaDB 1.5.9 rejects an empty metadata dictionary:

ValueError: Expected metadata to be a non-empty dict,
got 0 metadata attributes in upsert.

The failure was reproduced through both ChromaDBClient.add_documents() and ChromaDBClient.aadd_documents() using real Chroma collection validation, fixed local embeddings, and mocked transport. It occurs before transport writes.

Steps to reproduce

from chromadb.api.types import validate_metadatas
from crewai.rag.chromadb.utils import (
    _prepare_documents_for_chromadb,
    _create_batch_slice,
)

documents = [
    {"doc_id": "1", "content": "alpha"},
    {
        "doc_id": "2",
        "content": "beta",
        "metadata": {"source": "local"},
    },
]

prepared = _prepare_documents_for_chromadb(documents)
_, _, metadata = _create_batch_slice(prepared, 0, 100)

validate_metadatas(metadata)  # raises ValueError

Expected behavior

Mixed batches should ingest successfully when metadata is optional, without losing document-to-metadata alignment. ChromaDB accepts the corresponding representation:

[None, {"source": "local"}]

Actual behavior

CrewAI passes {} for a document without metadata, and ChromaDB rejects the entire mixed batch.

Environment

  • Python 3.12.10
  • CrewAI 1.15.35
  • ChromaDB 1.5.9

Additional context

  • All-missing-metadata and all-populated-metadata control cases succeeded.
  • Existing test lib/crewai/tests/rag/chromadb/test_utils.py::test_create_batch_slice_some_empty_metadatas currently expects the empty-dictionary payload.
  • The relevant existing RAG test files passed (96 tests); this mixed-metadata compatibility case is not covered correctly.
  • I searched public issues and PRs for this specific behavior and did not find a matching report. Related ChromaDB metadata work appears to concern different cases.

I'd be happy to add focused synchronous/asynchronous regression tests and a minimal fix if maintainers agree with the expected behavior.

MoAftab-01 avatar Nov 04 '26 23:11 MoAftab-01