{"record":{"id":"3634f71c96d98b78","repo":"chroma-core/chroma","slug":"invalid-task-self-task-3634f7","errorCode":null,"errorMessage":"Invalid task: {self.task}","messagePattern":"Invalid task: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"chromadb/utils/embedding_functions/huggingface_sparse_embedding_function.py","lineNumber":90,"sourceCode":"            Embeddings for the documents.\n        \"\"\"\n        try:\n            from sentence_transformers import SparseEncoder\n        except ImportError:\n            raise ValueError(\n                \"The sentence_transformers python package is not installed. Please install it with `pip install sentence_transformers`\"\n            )\n        model = cast(SparseEncoder, self._model)\n        if self.task == \"document\":\n            embeddings = model.encode_document(\n                list(input),\n            )\n        elif self.task == \"query\":\n            embeddings = model.encode_query(\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            # Convert sparse tensor to dense array if needed\n            if hasattr(vec, \"to_dense\"):\n                vec_dense = vec.to_dense().numpy()\n            else:\n                vec_dense = vec.numpy() if hasattr(vec, \"numpy\") else np.array(vec)\n\n            nz = np.where(vec_dense != 0)[0]\n            sparse_vectors.append(\n                normalize_sparse_vector(\n                    indices=nz.tolist(), values=vec_dense[nz].tolist()\n                )\n            )\n\n        return sparse_vectors","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/chromadb/utils/embedding_functions/huggingface_sparse_embedding_function.py#L72-L108","documentation":"HuggingFaceSparseEmbeddingFunction.__call__ dispatches on self.task: 'document' calls SparseEncoder.encode_document, 'query' calls encode_query; anything else (including None, since task is Optional) falls into the else branch and raises ValueError(f\"Invalid task: {self.task}\"). The constructor parameter is only typed as Literal[\"document\",\"query\"] (TaskType), so an invalid value is not caught at construction time — it surfaces on the first add/query. The default task is 'document'.","triggerScenarios":"Passing task='docs', 'search_document', 'embedding', or task=None to the constructor and then calling the function or collection.add(); also copying a task name valid for a different EF (e.g. Nomic's 'search_document') into this one.","commonSituations":"Porting config between embedding functions that use different task vocabularies; explicitly passing task=None expecting the default; typos from hand-written YAML/JSON config dicts fed to the constructor.","solutions":["Use exactly 'document' for indexing documents and 'query' for queries: HuggingFaceSparseEmbeddingFunction(model_name=..., device=..., task='document')","Omit the task argument entirely (default is 'document') instead of passing None","To use different tasks for documents vs queries, keep task='document' and pass query_config={'task': 'query'}"],"exampleFix":"# before\nef = HuggingFaceSparseEmbeddingFunction(\n    model_name=\"prithivida/Splade_PP_en_v1\", device=\"cpu\", task=\"docs\"  # invalid\n)\n\n# after\nef = HuggingFaceSparseEmbeddingFunction(\n    model_name=\"prithivida/Splade_PP_en_v1\", device=\"cpu\", task=\"document\",\n    query_config={\"task\": \"query\"},\n)","handlingStrategy":"validation","validationCode":"VALID_TASKS = {\"document\", \"query\"}\n\ndef make_ef(task: str):\n    if task not in VALID_TASKS:\n        raise ValueError(f\"task must be one of {sorted(VALID_TASKS)}, got {task!r}\")\n    return HuggingFaceSparseEmbeddingFunction(\n        model_name=\"prithivida/Splade_PP_en_v1\", device=\"cpu\", task=task\n    )","typeGuard":"from typing import Literal\n\nTask = Literal[\"document\", \"query\"]\n\ndef is_valid_task(t: object) -> bool:\n    return t in (\"document\", \"query\")","tryCatchPattern":"try:\n    ef(docs)\nexcept ValueError as e:\n    if str(e).startswith(\"Invalid task:\"):\n        log.error(\"task must be 'document' or 'query', got %s\", ef.task)\n        raise\n    raise","preventionTips":["Derive the allowed set from the TaskType Literal instead of hardcoding strings in two places","Never pass task=None; omit the argument to get the 'document' default","Validate task values at config-load time (fail before any data ingestion starts)"],"tags":["python","validation","enum","sparse-embeddings","constructor-argument"],"backgroundTag":"invalid-parameter-value","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}