encode_document() never uses a "passage" or "corpus" prompt
encode_document() looks for a prompt named "document", then "passage", then "corpus", and uses the first one that exists in model.prompts. But SentenceTransformer, SparseEncoder and MultiVectorEncoder inject _default_prompts = {"query": None, "document": None} into model.prompts at init, and _validate_prompts turns the None into "". So "document" always exists (as an empty prompt), and "passage" / "corpus" are never used.
This is exactly the example the prompts docstring gives (the E5 convention), so documents are silently encoded without their prompt:
import numpy as np
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(
"sentence-transformers-testing/stsb-bert-tiny-safetensors",
prompts={"query": "query: ", "passage": "passage: "},
)
print(model.prompts) # {'query': 'query: ', 'document': '', 'passage': 'passage: '}
docs = ["Paris is the capital of France."]
print(np.allclose(model.encode_document(docs), model.encode(docs, prompt_name="passage"))) # False, expected True
print(np.allclose(model.encode_document(docs), model.encode(docs))) # True: no prompt usedCode (main @ 4a3b5cd6):
- https://github.com/huggingface/sentence-transformers/blob/4a3b5cd6/sentence_transformers/sentence_transformer/model.py#L141 and L529
- https://github.com/huggingface/sentence-transformers/blob/4a3b5cd6/sentence_transformers/sparse_encoder/model.py#L134 and L477
- https://github.com/huggingface/sentence-transformers/blob/4a3b5cd6/sentence_transformers/multi_vector_encoder/model.py#L142 and L555
- https://github.com/huggingface/sentence-transformers/blob/4a3b5cd6/sentence_transformers/base/model.py#L189 (merging the defaults) and L336-L337 (
None->"")
The same happens when the prompts come from a saved config_sentence_transformers.json. Since the evaluators and mine_hard_negatives call encode_document, they are affected too. The existing prompt-priority tests set model.prompts = {...} directly, which bypasses the injected default, so they don't catch it.
A possible fix is to prefer the first candidate with a non-empty prompt and otherwise keep the current choice, in the three encode_document methods (or a shared helper). I'd be happy to open a PR if that approach works for you, or a different one if you'd rather not inject the empty "document" prompt at all.
Source: huggingface/sentence-transformers