Chroma: collections use the L2 space but scores assume cosine distance, so scores and score thresholds are wrong

Open
#46,994 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #47084 by @laoyyu — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
60/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
ai, databases

Research direction

Start from ChromaVector.create_collection, add_texts and search_by_vector in the Chroma vector-store adapter: create_collection passes no distance space, and search_by_vector scores as 1 - distance. Reproduce with the snippet against ghcr.io/chroma-core/chroma:0.5.20 using unit-length embeddings, then make the score follow the collection's space so existing l2 collections report 1 - distance/2. Done when threshold 0.0 returns all five chunks, 0.7 returns only the 0.95 and 0.8 ones, and hit testing reports the same scores.

Written by the indexing model from the issue text.

Description

Self Checks
  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.
Dify version

main (580416d, api 1.20.0)

Cloud or Self Hosted

Self Hosted (Source)### Steps to reproduce

Against ghcr.io/chroma-core/chroma:0.5.20 (the image in docker-compose), with unit-length embeddings whose cosine similarity to the query is 0.95, 0.8, 0.6, 0.45 and 0.2:

vector.create(texts=docs, embeddings=embeddings)
vector.search_by_vector(query, top_k=5, score_threshold=0.0)
vector.search_by_vector(query, top_k=5, score_threshold=0.7)
✔️ Expected Behavior

metadata["score"] is the cosine similarity, as with pgvector, Weaviate and the other backends: threshold 0.0 returns all 5 chunks (scores 0.95 ... 0.2) and threshold 0.7 returns the 0.95 and 0.8 chunks.

❌ Actual Behavior

The scores are 0.9, 0.6 and 0.2 (that is, 2 x cosine - 1), threshold 0.0 returns only 3 chunks (the 0.45 and 0.2 ones are dropped), and threshold 0.7 returns only the 0.95 chunk.

ChromaVector.create_collection and add_texts call get_or_create_collection(name) without a distance space, so Chroma uses its default l2 (squared Euclidean) space, but search_by_vector computes the score as 1 - distance, which is only right for cosine distance. For unit vectors the squared L2 distance is 2 - 2 x cosine, so every score is too low, chunks with a cosine below 0.5 are never returned, and a score threshold is stricter than configured. Hit testing shows the same wrong scores.

A possible fix: create new collections with metadata={"hnsw:space": "cosine"}, and compute the score from the collection's space so existing l2 collections also report the cosine similarity (1 - distance / 2 for unit vectors). Happy to send a PR if that approach is fine.

Dominant language
Python
Stars
162k
Forks
25.6k
Avg merge
15h 9m
Merged PRs (30d)
606

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/dify

All issues in langgenius/dify

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.