How to create a Chroma collection in python
Direct answer
Use the
chromadb Python client to create a Chroma collection by initializing a client and calling client.create_collection(name="your_collection_name").Setup
Install
pip install chromadb Imports
import chromadb
from chromadb.config import Settings Examples
inCreate a collection named 'my_docs'
outCollection 'my_docs' created successfully.
inCreate a collection named 'research_papers'
outCollection 'research_papers' created successfully.
inCreate a collection with an existing name 'my_docs'
outCollection 'my_docs' already exists or retrieved existing collection.
Integration steps
- Install the chromadb Python package.
- Import chromadb and configure the client.
- Initialize the Chroma client with optional settings.
- Call
create_collectionwith your desired collection name. - Use the returned collection object to add or query documents.
Full code
import chromadb
from chromadb.config import Settings
# Initialize Chroma client with default settings
client = chromadb.Client(Settings())
# Create a collection named 'my_collection'
collection = client.create_collection(name="my_collection")
print(f"Collection '{collection.name}' created successfully.") output
Collection 'my_collection' created successfully.
API trace
Request
{"method": "POST", "endpoint": "/collections", "body": {"name": "my_collection"}} Response
{"id": "collection_id", "name": "my_collection", "status": "created"} Extract
collection = client.create_collection(name="my_collection")Variants
Retrieve existing collection if it exists ›
Use when you want to avoid errors if the collection already exists and want to reuse it.
import chromadb
from chromadb.config import Settings
client = chromadb.Client(Settings())
# This will create or return existing collection
collection = client.get_or_create_collection(name="my_collection")
print(f"Using collection: {collection.name}") Initialize client with persistent local storage ›
Use when you want your Chroma database to persist data locally across sessions.
import chromadb
from chromadb.config import Settings
settings = Settings(chroma_db_impl="duckdb+parquet", persist_directory="./chroma_db")
client = chromadb.Client(settings)
collection = client.create_collection(name="persistent_collection")
print(f"Persistent collection '{collection.name}' created.") Performance
Latency~100-300ms for collection creation depending on storage backend
CostFree for local and open-source usage; cloud-hosted options may vary
Rate limitsNo enforced rate limits on local usage; cloud services may impose limits
- Keep collection names short and descriptive to reduce metadata overhead.
- Batch inserts into collections to improve throughput.
- Use persistent storage settings to avoid recreating collections repeatedly.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Default in-memory client | ~100ms | Free | Quick prototyping |
| Persistent local storage | ~200-300ms | Free | Production with data persistence |
| Cloud-hosted Chroma | Varies | Depends on provider | Scalable multi-user deployments |
Quick tip
Always initialize the Chroma client with appropriate settings to control persistence and performance.
Common mistake
Trying to create a collection with a name that already exists without handling the exception or retrieving the existing collection.