ValueError
ValueError: embedding_function not set in ChromaDB client
Stack trace
ValueError: embedding_function not set
File "/usr/local/lib/python3.9/site-packages/chromadb/api/client.py", line 123, in add
raise ValueError("embedding_function not set")
File "app.py", line 45, in main
collection.add(documents=docs) # triggers error
Why it happens
ChromaDB requires an embedding function to convert text into vector embeddings before storing or querying. If you create a collection without setting this embedding_function, operations that depend on embeddings will fail with this error.
Detection
Check if your ChromaDB collection instance has an embedding_function attribute set before calling add or query methods; log or assert this during initialization.
Causes & fixes
Embedding function was never assigned to the ChromaDB collection instance.
Assign a valid embedding function (e.g., from OpenAI or HuggingFace) to the collection's embedding_function attribute before adding or querying.
Using ChromaDB client without passing embedding_function parameter during collection creation.
Pass embedding_function explicitly when creating or loading the collection, e.g., collection = client.get_collection(name='mycol', embedding_function=my_embed_func).
Embedding function variable is None or improperly initialized.
Ensure the embedding function is properly instantiated and not None before assigning it to the collection.
Code: broken vs fixed
from chromadb import Client
client = Client()
collection = client.get_collection(name="test_collection")
collection.add(documents=["Hello world"]) # triggers ValueError: embedding_function not set import os
from chromadb import Client
from openai import OpenAI
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "your_api_key_here")
client = Client()
# Define embedding function using OpenAI embeddings
embedding_function = lambda texts: OpenAI(api_key=os.environ["OPENAI_API_KEY"]).embeddings.create(input=texts).data
collection = client.get_collection(name="test_collection", embedding_function=embedding_function)
collection.add(documents=["Hello world"]) # fixed: embedding_function set
print("Added documents successfully.") Workaround
Wrap calls to add or query in try/except catching ValueError; if embedding_function not set, manually generate embeddings externally and add vectors directly.
Prevention
Always initialize ChromaDB collections with a valid embedding_function before any vector operations to avoid runtime errors.