{"record":{"id":"8798450a6ff6e0ab","repo":"chroma-core/chroma","slug":"invalid-task-self-task","errorCode":null,"errorMessage":"Invalid task: {self.task}","messagePattern":"Invalid task: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"chromadb/utils/embedding_functions/bm25_embedding_function.py","lineNumber":119,"sourceCode":"            Embeddings for the documents.\n        \"\"\"\n        try:\n            from fastembed.sparse.bm25 import Bm25\n        except ImportError:\n            raise ValueError(\n                \"The fastembed python package is not installed. Please install it with `pip install fastembed`\"\n            )\n        model = cast(Bm25, self._model)\n        if self.task == \"document\":\n            embeddings = model.embed(\n                list(input),\n            )\n        elif self.task == \"query\":\n            embeddings = model.query_embed(\n                list(input),\n            )\n        else:\n            raise ValueError(f\"Invalid task: {self.task}\")\n\n        sparse_vectors: SparseVectors = []\n\n        for vec in embeddings:\n            sparse_vectors.append(\n                normalize_sparse_vector(\n                    indices=vec.indices.tolist(), values=vec.values.tolist()\n                )\n            )\n\n        return sparse_vectors\n\n    def embed_query(self, input: Documents) -> SparseVectors:\n        try:\n            from fastembed.sparse.bm25 import Bm25\n        except ImportError:\n            raise ValueError(\n                \"The fastembed python package is not installed. Please install it with `pip install fastembed`\"","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/chromadb/utils/embedding_functions/bm25_embedding_function.py#L101-L137","documentation":"Bm25EmbeddingFunction dispatches on self.task: \"document\" calls model.embed() (corpus indexing) and \"query\" calls model.query_embed() (short queries with different term statistics). Any other value raises this ValueError — but only at call time, because __init__ accepts task without validating it (the type hint is Literal[\"document\", \"query\"] but hints are not enforced at runtime). The two modes are not interchangeable: indexing with query mode produces vectors incompatible with document embeddings.","triggerScenarios":"Bm25EmbeddingFunction(task=\"doc\") or task=\"passage\" (sentence-transformers convention), then calling ef(texts); also a task injected from an unvalidated config via build_from_config(config.get(\"task\")).","commonSituations":"Porting retrieval code from sentence-transformers/fastembed conventions where \"query\"/\"passage\" is the pairing; typos and casing (\"Document\"); task read from a YAML/JSON config that was never schema-checked.","solutions":["Use exactly one of the two literals: task=\"document\" when embedding corpus texts, task=\"query\" when embedding search queries.","Validate the value at construction in your own wrapper (see type guard) so the failure surfaces early.","Check ef.get_config()[\"task\"] when debugging a function restored from config."],"exampleFix":"# before: ValueError \"Invalid task: doc\"\nef = Bm25EmbeddingFunction(task=\"doc\")\nindex_vectors = ef(corpus)\n\n# after\ndoc_ef = Bm25EmbeddingFunction(task=\"document\")\nquery_ef = Bm25EmbeddingFunction(task=\"query\")\nindex_vectors = doc_ef(corpus)","handlingStrategy":"type-guard","validationCode":"VALID_TASKS = {\"document\", \"query\"}\n\ndef make_bm25_ef(task: str, **kw):\n    if task not in VALID_TASKS:\n        raise ValueError(f\"task must be one of {sorted(VALID_TASKS)}, got {task!r}\")\n    from chromadb.utils.embedding_functions import Bm25EmbeddingFunction\n    return Bm25EmbeddingFunction(task=task, **kw)","typeGuard":"from typing import Literal\n\nTaskType = Literal[\"document\", \"query\"]\n\ndef is_valid_bm25_task(value: object) -> bool:\n    return value in (\"document\", \"query\")","tryCatchPattern":"try:\n    vectors = ef(texts)\nexcept ValueError as e:\n    if \"Invalid task\" in str(e):\n        raise RuntimeError(\"task must be 'document' (corpus) or 'query' (search); got \" + str(ef.task)) from e\n    raise","preventionTips":["Validate task against ('document', 'query') at construction — __init__ does not check it.","Map foreign conventions explicitly: 'passage' → 'document' when porting sentence-transformers code.","Check get_config()['task'] when debugging functions restored from persisted configs."],"tags":["python","bm25","sparse-embedding","enum-validation","invalid-argument","chromadb"],"backgroundTag":"invalid-enum-value","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}