# chromadb.errors.DimensionError: Inserted embedding dimension (512) does not match collection dimension (768)

- **ID:** `llm/embedding-dimension-mismatch-insert`
- **Domain:** llm
- **Category:** data_error
- **Error Code:** `DimensionError`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The embedding model used for insertion produces vectors of a different dimensionality than the model used when creating the collection, often due to switching embedding models or using a model with a different output dimension.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| chromadb==0.4.22 | active | — | — |
| chromadb==0.5.0 | active | — | — |
| sentence-transformers==2.2.2 | active | — | — |
| openai==1.12.0 | active | — | — |

## Workarounds

1. **Delete the existing collection and recreate it using the same embedding model that you will use for all inserts. For example, in ChromaDB: `client.delete_collection("my_collection"); collection = client.create_collection(name="my_collection", embedding_function=current_embedding_function)`** (90% success)
   ```
   Delete the existing collection and recreate it using the same embedding model that you will use for all inserts. For example, in ChromaDB: `client.delete_collection("my_collection"); collection = client.create_collection(name="my_collection", embedding_function=current_embedding_function)`
   ```
2. **If you must keep the existing collection, re-embed all existing documents using the new model and re-insert them. Use a script to iterate over all documents, call `embedding_model.encode(doc)`, and upsert into the collection.** (85% success)
   ```
   If you must keep the existing collection, re-embed all existing documents using the new model and re-insert them. Use a script to iterate over all documents, call `embedding_model.encode(doc)`, and upsert into the collection.
   ```
3. **Explicitly set the embedding function dimension when creating the collection: `collection = client.create_collection(name="my_collection", metadata={"hnsw:space": "cosine"}, embedding_function=current_embedding_function)` and verify `collection.metadata['dimension']` matches your model output.** (95% success)
   ```
   Explicitly set the embedding function dimension when creating the collection: `collection = client.create_collection(name="my_collection", metadata={"hnsw:space": "cosine"}, embedding_function=current_embedding_function)` and verify `collection.metadata['dimension']` matches your model output.
   ```

## Dead Ends

- **** — Padding distorts the vector space and ruins retrieval accuracy; the collection's distance metrics expect all vectors to have the same semantic dimensionality. (95% fail)
- **** — ChromaDB infers dimension from the first inserted embedding; if the first insert uses the wrong model, the same mismatch occurs again. (80% fail)
- **** — Reshaping changes the data layout but not the semantic content; distance computations become meaningless and retrieval degrades to random. (90% fail)
