Adding or querying my Chroma collection fails with a dimension mismatch, even though my code didn't change.
Why this happens
A Chroma collection records the dimension of the first embeddings added to it, and every later embedding must match. The exact wording of the error differs between Chroma versions, but the cause is the same. Typical ways it happens:
-
The collection was created with Chroma's default embedding function (all-MiniLM-L6-v2, 384 dimensions), and you now pass OpenAI embeddings (1536 dimensions), or the other way round.
-
You added precomputed embeddings, but query with
query_texts=, which makes Chroma embed the query with the collection's default function instead of your model. -
A persistent collection from an earlier run, created with a different model, is still on disk, and
get_or_create_collectionreopened it.
Answer 1: Attach the same embedding function every time you open the collection
Pass your embedding function both when creating and when reopening the collection, so documents and queries are embedded the same way:
import chromadb
from chromadb.utils import embedding_functions
ef = embedding_functions.OpenAIEmbeddingFunction(
api_key="...", model_name="text-embedding-3-small"
)
client = chromadb.PersistentClient(path="./chroma_db")
collection = client.get_or_create_collection("docs",
embedding_function=ef)
collection.add(ids=ids, documents=documents)
results = collection.query(query_texts=["my question"],
n_results=5)
