ValueError
chromadb.errors.ValueError
Stack trace
ValueError: Collection with name 'my_collection' already exists.
File "app.py", line 42, in create_collection
collection = client.create_collection(name="my_collection")
File "chromadb/client.py", line 120, in create_collection
raise ValueError(f"Collection with name '{name}' already exists.") Why it happens
ChromaDB enforces unique collection names to prevent data conflicts. When your code tries to create a collection with a name that is already registered, it raises this ValueError to avoid overwriting or duplicating collections.
Detection
Before creating a collection, check if the collection name already exists by querying the client.list_collections() method and handle the existence case gracefully.
Causes & fixes
Code attempts to create a collection without checking if it already exists
Use client.list_collections() to check for existing collections before calling create_collection()
Multiple parts of the code or concurrent processes try to create the same collection simultaneously
Implement synchronization or locking to ensure only one creation attempt occurs, or catch the ValueError and handle it by retrieving the existing collection
Re-running initialization scripts without cleanup causes repeated create_collection calls
Add logic to reuse existing collections or delete collections before re-creating them during development
Code: broken vs fixed
from chromadb import Client
client = Client()
# This line raises ValueError if collection exists
collection = client.create_collection(name="my_collection") import os
from chromadb import Client
client = Client()
existing_collections = [col.name for col in client.list_collections()]
if "my_collection" in existing_collections:
collection = client.get_collection(name="my_collection") # Reuse existing collection
else:
collection = client.create_collection(name="my_collection") # Create new collection
print(f"Using collection: {collection.name}") # Confirm success Workaround
Wrap create_collection in try/except ValueError, and if caught, call get_collection to retrieve the existing collection instead of failing.
Prevention
Design your application to query existing collections before creation and handle concurrency with locks or retries to avoid duplicate collection creation attempts.