Chroma Tutorial: Search Your Shop by Meaning in Python

Quick answer

Chroma is an open source vector database you can run from Python without installing a server: you store text as embeddings, then search by meaning instead of matching keywords. This post builds a small catalogue for a Karachi clothing shop, embeds eight products, and queries it with a plain sentence like "warm shawl for winter evenings". You will see the real distances it returns, a price filter, and two errors that stopped the first run.

A small clothing shop in Karachi lists its stock on WhatsApp and in a spreadsheet. A customer asks for something warm for winter evenings, and the owner scrolls, guesses, and sends photos. Keyword search does not help: warm appears nowhere in the product names, and shawl matches everything from a lawn dupatta to a wool shawl. What the shop needs is search that understands meaning, so a customer sentence lands on the right shelf. That is what a vector database does, and Chroma is the lightest way I know to try one in Python: it runs in your script and stores its data in a folder.

What I would check before using it

My first check is how much data there is. Chroma inside your Python process, with a PersistentClient writing to a folder, handles hundreds or thousands of items without complaint. At millions of items, or with several services querying at once, I would run Chroma as a server or use a dedicated vector database such as Qdrant, and the Qdrant Tutorial: Search Your Shop by Meaning, Not Words post covers that route.

My second check is whether meaning is really the problem. Chroma ranks by similarity of meaning, exactly what the shawl question needs, but it is no keyword engine. If customers mostly type exact product names, or misspell them, typo-tolerant keyword search fits better, and the Typesense Tutorial: Search That Forgives Typos Fast post covers that side.

My third check is the embedding model. Chroma ships a default, all-MiniLM-L6-v2, downloaded on first use, 79.3 MB in this run. That is fine on a normal connection, but on a machine with no internet, or where downloads are blocked, the first add will fail. Plan for that: download the model once, or bring your own embedding function.

My fourth check is where the data lives and who can reach it. PersistentClient stores everything in a folder on disk, so back it up like any database. The embedded client has no login, so never share that folder or serve it from a public path.

My verdict, as judgement: adopt Chroma when a Python project needs meaning-based search over a modest collection with zero infrastructure. It flips when the data grows past a single process, when keyword search matters more, or when the machine cannot download the model.

A Karachi clothing shop, searched by meaning

The demo is a Karachi clothing shop: eight products, each with a short description and metadata for price and category, in a collection called products. Install Chroma first:

$ pip install chromadb
Successfully installed chromadb-1.5.9

The script opens a persistent client, which stores the database in the chroma_data folder. My first step asked for a collection that did not exist:

import chromadb

client = chromadb.PersistentClient(path="./chroma_data")

try:
    client.get_collection("winter_sale")
except Exception as e:
    print("ERROR:", type(e).__name__, "-", e)

collection = client.get_or_create_collection(name="products")
ERROR: NotFoundError - Collection [winter_sale] does not exist

The fix is get_or_create_collection, which creates the collection when missing. Then the eight products go in, each with an id, a description to embed, and metadata:

products = [
    ("p1", "White Cotton Kurta, breathable cotton, plain white, everyday wear",
     {"name": "White Cotton Kurta", "price_rs": 2450, "category": "kurta"}),
    ("p2", "Pastel Lawn Kurta, light lawn fabric, soft pastel print, summer wear",
     {"name": "Pastel Lawn Kurta", "price_rs": 1950, "category": "kurta"}),
    ("p3", "Embroidered Lawn Suit, lawn suit with thread embroidery, festive wear",
     {"name": "Embroidered Lawn Suit", "price_rs": 3200, "category": "suit"}),
    ("p4", "Wool Shawl, thick warm wool, plain maroon, for cold evenings",
     {"name": "Wool Shawl", "price_rs": 1800, "category": "shawl"}),
    ("p5", "Winter Pashmina Shawl, soft pashmina, warm and light, winter evenings",
     {"name": "Winter Pashmina Shawl", "price_rs": 4500, "category": "shawl"}),
    ("p6", "Linen Summer Kurta, airy linen, cool for hot days, casual wear",
     {"name": "Linen Summer Kurta", "price_rs": 2100, "category": "kurta"}),
    ("p7", "Silk Formal Kurta, smooth silk, formal occasions, elegant look",
     {"name": "Silk Formal Kurta", "price_rs": 5800, "category": "kurta"}),
    ("p8", "Cotton Lawn Dupatta, lightweight cotton dupatta, printed, daily wear",
     {"name": "Cotton Lawn Dupatta", "price_rs": 950, "category": "dupatta"}),
]

collection.add(
    ids=[p[0] for p in products],
    documents=[p[1] for p in products],
    metadatas=[p[2] for p in products],
)
print("Added", collection.count(), "products")
Added 8 products

Now the query. No keyword matches "warm shawl for winter evenings", but the embeddings know what it means. A smaller distance means closer in meaning:

results = collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=3,
)
for doc, meta, dist in zip(
    results["documents"][0], results["metadatas"][0], results["distances"][0]
):
    print(f"{meta['name']} - Rs {meta['price_rs']} - distance {dist:.4f}")
Wool Shawl - Rs 1800 - distance 0.4445
Winter Pashmina Shawl - Rs 4500 - distance 0.6763
Linen Summer Kurta - Rs 2100 - distance 1.2062

Both shawls come first, and the Wool Shawl wins because its description mentions thick warm wool for cold evenings. The Linen Summer Kurta trails at 1.2062: it shares the kurta theme, not the warmth.

A metadata filter narrows the same query by price and category, and only the Wool Shawl is a shawl under Rs 3,000:

filtered = collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=5,
    where={"$and": [{"category": "shawl"}, {"price_rs": {"$lt": 3000}}]},
)
print("Shawls under Rs 3,000:")
for meta in filtered["metadatas"][0]:
    print(f"  {meta['name']} - Rs {meta['price_rs']}")
Shawls under Rs 3,000:
  Wool Shawl - Rs 1800

My second error came from the filter itself. I passed the price as the string "3000" instead of the number 3000:

collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=3,
    where={"price_rs": {"$lt": "3000"}},
)
ERROR: ValueError - Expected operand value to be an int or a float for operator $lt, got 3000 in query.

The fix is to pass the number, not the string: metadata values keep their types, and the filter operators check them.

Terminal output showing a Chroma query for a warm shawl for winter evenings returning the Wool Shawl at Rs 1,800 with distance 0.4445.

Common mistakes and how to fix them

First, asking for a collection that was never created. The symptom is NotFoundError: "Collection [winter_sale] does not exist". The fix is get_or_create_collection, or create the collection first.

Second, passing a string where a number belongs in a filter. The symptom is ValueError with "Expected operand value to be an int or a float for operator $lt, got 3000 in query." The fix is to pass 3000 as a number, matching the type stored in the metadata.

Third, adding lists of different lengths. I added 2 ids with 1 document. The symptom is ValueError with "Unequal lengths for fields: ids: 2, documents: 1 in add." The fix is to keep ids, documents, and metadatas the same length, building them from one list.

Fourth, querying an empty collection and expecting an error. There is none: the query returns empty lists for ids, documents, metadatas, and distances. Check collection.count() first, or handle an empty result in your code.

Frequently Asked Questions

Does Chroma need a server? No. PersistentClient stores the database in a folder on your machine, and EphemeralClient keeps it in memory for a quick test.

Which embedding model does Chroma use? The ONNX version of all-MiniLM-L6-v2 from Hugging Face. Chroma downloads it, 79.3 MB here, the first time you add or query documents.

How is Chroma different from Qdrant? Chroma runs inside your Python process with no server, the quicker start for a small project. Qdrant runs as a separate service built for larger collections, and the Qdrant Tutorial: Search Your Shop by Meaning, Not Words post covers it.

Can I use Chroma with LangChain? Yes. LangChain has a Chroma integration that wraps a collection as a vector store, so these products can feed a retrieval chain. The LangChain Tutorial: Build Your First LLM App in Python post covers that side.

What does the distance number mean? It measures how far apart the query and the product sit in embedding space. A smaller distance means closer in meaning: the Wool Shawl at 0.4445 outranked the Linen Summer Kurta at 1.2062.

What to remember

  • Chroma is a vector database: search by meaning, not by keyword.
  • A collection holds documents to embed, plus metadata you can filter on.
  • The default embedding model downloads on first use, so plan for that.
  • Use get_or_create_collection; get_collection fails on a missing name.
  • Metadata filters check types: a price stored as a number needs a number.

Where this leaves you

Eight products, one folder, and a Karachi clothing shop can now answer "warm shawl for winter evenings" with the Wool Shawl at Rs 1,800 and a distance of 0.4445, no keyword tricks needed. The two errors along the way were both cheap: a missing collection and a string where a number belonged. If your own catalogue still answers customers by scrolling, try this on a copy of your real product list, and watch which items the distances put first before you build anything bigger around it.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Quick answer

Chroma is an open source vector database you can run from Python without installing a server: you store text as embeddings, then search by meaning instead of matching keywords. This post builds a small catalogue for a Karachi clothing shop, embeds eight products, and queries it with a plain sentence like "warm shawl for winter evenings". You will see the real distances it returns, a price filter, and two errors that stopped the first run.

A small clothing shop in Karachi lists its stock on WhatsApp and in a spreadsheet. A customer asks for something warm for winter evenings, and the owner scrolls, guesses, and sends photos. Keyword search does not help: warm appears nowhere in the product names, and shawl matches everything from a lawn dupatta to a wool shawl. What the shop needs is search that understands meaning, so a customer sentence lands on the right shelf. That is what a vector database does, and Chroma is the lightest way I know to try one in Python: it runs in your script and stores its data in a folder.

What I would check before using it

My first check is how much data there is. Chroma inside your Python process, with a PersistentClient writing to a folder, handles hundreds or thousands of items without complaint. At millions of items, or with several services querying at once, I would run Chroma as a server or use a dedicated vector database such as Qdrant, and the Qdrant Tutorial: Search Your Shop by Meaning, Not Words post covers that route.

My second check is whether meaning is really the problem. Chroma ranks by similarity of meaning, exactly what the shawl question needs, but it is no keyword engine. If customers mostly type exact product names, or misspell them, typo-tolerant keyword search fits better, and the Typesense Tutorial: Search That Forgives Typos Fast post covers that side.

My third check is the embedding model. Chroma ships a default, all-MiniLM-L6-v2, downloaded on first use, 79.3 MB in this run. That is fine on a normal connection, but on a machine with no internet, or where downloads are blocked, the first add will fail. Plan for that: download the model once, or bring your own embedding function.

My fourth check is where the data lives and who can reach it. PersistentClient stores everything in a folder on disk, so back it up like any database. The embedded client has no login, so never share that folder or serve it from a public path.

My verdict, as judgement: adopt Chroma when a Python project needs meaning-based search over a modest collection with zero infrastructure. It flips when the data grows past a single process, when keyword search matters more, or when the machine cannot download the model.

A Karachi clothing shop, searched by meaning

The demo is a Karachi clothing shop: eight products, each with a short description and metadata for price and category, in a collection called products. Install Chroma first:

$ pip install chromadb
Successfully installed chromadb-1.5.9

The script opens a persistent client, which stores the database in the chroma_data folder. My first step asked for a collection that did not exist:

import chromadb

client = chromadb.PersistentClient(path="./chroma_data")

try:
    client.get_collection("winter_sale")
except Exception as e:
    print("ERROR:", type(e).__name__, "-", e)

collection = client.get_or_create_collection(name="products")
ERROR: NotFoundError - Collection [winter_sale] does not exist

The fix is get_or_create_collection, which creates the collection when missing. Then the eight products go in, each with an id, a description to embed, and metadata:

products = [
    ("p1", "White Cotton Kurta, breathable cotton, plain white, everyday wear",
     {"name": "White Cotton Kurta", "price_rs": 2450, "category": "kurta"}),
    ("p2", "Pastel Lawn Kurta, light lawn fabric, soft pastel print, summer wear",
     {"name": "Pastel Lawn Kurta", "price_rs": 1950, "category": "kurta"}),
    ("p3", "Embroidered Lawn Suit, lawn suit with thread embroidery, festive wear",
     {"name": "Embroidered Lawn Suit", "price_rs": 3200, "category": "suit"}),
    ("p4", "Wool Shawl, thick warm wool, plain maroon, for cold evenings",
     {"name": "Wool Shawl", "price_rs": 1800, "category": "shawl"}),
    ("p5", "Winter Pashmina Shawl, soft pashmina, warm and light, winter evenings",
     {"name": "Winter Pashmina Shawl", "price_rs": 4500, "category": "shawl"}),
    ("p6", "Linen Summer Kurta, airy linen, cool for hot days, casual wear",
     {"name": "Linen Summer Kurta", "price_rs": 2100, "category": "kurta"}),
    ("p7", "Silk Formal Kurta, smooth silk, formal occasions, elegant look",
     {"name": "Silk Formal Kurta", "price_rs": 5800, "category": "kurta"}),
    ("p8", "Cotton Lawn Dupatta, lightweight cotton dupatta, printed, daily wear",
     {"name": "Cotton Lawn Dupatta", "price_rs": 950, "category": "dupatta"}),
]

collection.add(
    ids=[p[0] for p in products],
    documents=[p[1] for p in products],
    metadatas=[p[2] for p in products],
)
print("Added", collection.count(), "products")
Added 8 products

Now the query. No keyword matches "warm shawl for winter evenings", but the embeddings know what it means. A smaller distance means closer in meaning:

results = collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=3,
)
for doc, meta, dist in zip(
    results["documents"][0], results["metadatas"][0], results["distances"][0]
):
    print(f"{meta['name']} - Rs {meta['price_rs']} - distance {dist:.4f}")
Wool Shawl - Rs 1800 - distance 0.4445
Winter Pashmina Shawl - Rs 4500 - distance 0.6763
Linen Summer Kurta - Rs 2100 - distance 1.2062

Both shawls come first, and the Wool Shawl wins because its description mentions thick warm wool for cold evenings. The Linen Summer Kurta trails at 1.2062: it shares the kurta theme, not the warmth.

A metadata filter narrows the same query by price and category, and only the Wool Shawl is a shawl under Rs 3,000:

filtered = collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=5,
    where={"$and": [{"category": "shawl"}, {"price_rs": {"$lt": 3000}}]},
)
print("Shawls under Rs 3,000:")
for meta in filtered["metadatas"][0]:
    print(f"  {meta['name']} - Rs {meta['price_rs']}")
Shawls under Rs 3,000:
  Wool Shawl - Rs 1800

My second error came from the filter itself. I passed the price as the string "3000" instead of the number 3000:

collection.query(
    query_texts=["warm shawl for winter evenings"],
    n_results=3,
    where={"price_rs": {"$lt": "3000"}},
)
ERROR: ValueError - Expected operand value to be an int or a float for operator $lt, got 3000 in query.

The fix is to pass the number, not the string: metadata values keep their types, and the filter operators check them.

Common mistakes and how to fix them

First, asking for a collection that was never created. The symptom is NotFoundError: "Collection [winter_sale] does not exist". The fix is get_or_create_collection, or create the collection first.

Second, passing a string where a number belongs in a filter. The symptom is ValueError with "Expected operand value to be an int or a float for operator $lt, got 3000 in query." The fix is to pass 3000 as a number, matching the type stored in the metadata.

Third, adding lists of different lengths. I added 2 ids with 1 document. The symptom is ValueError with "Unequal lengths for fields: ids: 2, documents: 1 in add." The fix is to keep ids, documents, and metadatas the same length, building them from one list.

Fourth, querying an empty collection and expecting an error. There is none: the query returns empty lists for ids, documents, metadatas, and distances. Check collection.count() first, or handle an empty result in your code.

Frequently Asked Questions

Does Chroma need a server? No. PersistentClient stores the database in a folder on your machine, and EphemeralClient keeps it in memory for a quick test.

Which embedding model does Chroma use? The ONNX version of all-MiniLM-L6-v2 from Hugging Face. Chroma downloads it, 79.3 MB here, the first time you add or query documents.

How is Chroma different from Qdrant? Chroma runs inside your Python process with no server, the quicker start for a small project. Qdrant runs as a separate service built for larger collections, and the Qdrant Tutorial: Search Your Shop by Meaning, Not Words post covers it.

Can I use Chroma with LangChain? Yes. LangChain has a Chroma integration that wraps a collection as a vector store, so these products can feed a retrieval chain. The LangChain Tutorial: Build Your First LLM App in Python post covers that side.

What does the distance number mean? It measures how far apart the query and the product sit in embedding space. A smaller distance means closer in meaning: the Wool Shawl at 0.4445 outranked the Linen Summer Kurta at 1.2062.

What to remember

  • Chroma is a vector database: search by meaning, not by keyword.
  • A collection holds documents to embed, plus metadata you can filter on.
  • The default embedding model downloads on first use, so plan for that.
  • Use get_or_create_collection; get_collection fails on a missing name.
  • Metadata filters check types: a price stored as a number needs a number.

Where this leaves you

Eight products, one folder, and a Karachi clothing shop can now answer "warm shawl for winter evenings" with the Wool Shawl at Rs 1,800 and a distance of 0.4445, no keyword tricks needed. The two errors along the way were both cheap: a missing collection and a string where a number belonged. If your own catalogue still answers customers by scrolling, try this on a copy of your real product list, and watch which items the distances put first before you build anything bigger around it.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Comments

Popular posts from this blog

Swift Explained: Apple's Language for iPhone, Mac, and More

LangChain Tutorial: Build Your First LLM App in Python

Ollama Tutorial: Run Your First Local LLM in Python