deepset-ai/haystack · error

Pass either 'embedding' or 'embedding_fn', not both.

Error message

Pass either 'embedding' or 'embedding_fn', not both.

What it means

MockDocumentEmbedder lets you supply either a fixed `embedding` (list of numbers) or an `embedding_fn` (callable producing embeddings), but the two are mutually exclusive — the constructor raises this ValueError if both are non-None. This is an intentional design guard so the source of the mock embedding is unambiguous.

Source

Thrown at haystack/components/embedders/mock_document_embedder.py:89

            `embedding_fn`. If neither is provided, a deterministic embedding is derived from each document's text.
        :param embedding_fn: An optional callable that receives the prepared text of a document and returns the
            embedding as a list of floats. Mutually exclusive with `embedding`. To support serialization, pass a
            named function (lambdas and nested functions cannot be serialized).
        :param dimension: The number of dimensions of the deterministic embedding. Ignored when `embedding` or
            `embedding_fn` is provided, since their length is determined by the value or callable.
        :param model: The model name reported in the metadata. Purely cosmetic; no model is loaded.
        :param meta: Additional metadata merged into the output `meta`.
        :param prefix: A string to add at the beginning of each text before embedding.
        :param suffix: A string to add at the end of each text before embedding.
        :param meta_fields_to_embed: List of metadata fields to embed along with the document text.
        :param embedding_separator: Separator used to concatenate the metadata fields to the document text.
        :param progress_bar: Accepted for interface compatibility with real Document Embedders and ignored.
        :raises ValueError: If both `embedding` and `embedding_fn` are provided, if `dimension` is not positive, or
            if `embedding` is an empty list.
        :raises TypeError: If `embedding` is not a sequence of numbers.
        """
        if embedding is not None and embedding_fn is not None:
            raise ValueError("Pass either 'embedding' or 'embedding_fn', not both.")
        if dimension <= 0:
            raise ValueError("'dimension' must be a positive integer.")

        self.embedding = _coerce_embedding(embedding, name="'embedding'") if embedding is not None else None
        self.embedding_fn = embedding_fn
        self.dimension = dimension
        self.model = model
        self.meta = meta or {}
        self.prefix = prefix
        self.suffix = suffix
        self.meta_fields_to_embed = meta_fields_to_embed or []
        self.embedding_separator = embedding_separator
        self.progress_bar = progress_bar
        self._is_warmed_up = False

    def to_dict(self) -> dict[str, Any]:
        """Serialize the component to a dictionary."""
        embedding_fn = serialize_callable(self.embedding_fn) if self.embedding_fn is not None else None

View on GitHub (pinned to e318778c9b)

Solutions

  1. Remove either the `embedding` argument or the `embedding_fn` argument so only one is passed
  2. If you need dynamic values, keep `embedding_fn` and delete the static `embedding`; for a fixed vector keep `embedding` only

Example fix

// before
MockDocumentEmbedder(embedding=[0.1, 0.2], embedding_fn=lambda texts: [[0.1, 0.2]])
// after
MockDocumentEmbedder(embedding_fn=lambda texts: [[0.1, 0.2]])
Defensive patterns

Strategy: validation

Validate before calling

if embedding is not None and embedding_fn is not None:
    raise ValueError("Choose only one of embedding / embedding_fn")
embedder = MockDocumentEmbedder(embedding=embedding, embedding_fn=embedding_fn)

Try / catch

try:
    embedder = MockDocumentEmbedder(embedding=emb, embedding_fn=fn)
except ValueError as e:
    logging.error("Mock embedder misconfigured: %s", e)
    embedder = MockDocumentEmbedder(embedding_fn=fn if fn is not None else None) or MockDocumentEmbedder(embedding=emb)

Prevention

When it happens

Trigger: `MockDocumentEmbedder(embedding=[0.1, 0.2], embedding_fn=lambda texts: [[0.0]*2])` — both parameters provided in the same constructor call.

Common situations: Merging configuration from two sources (defaults plus overrides) so both end up set; copying an example that used `embedding_fn` while your own code already passes `embedding`; refactoring to a callable but leaving the static list in place.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/5109fc2c30ccd814. Report an issue: GitHub.