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

MockTextEmbedder accepts either a fixed `embedding` or an `embedding_fn`, not both. Supplying both is ambiguous, so the constructor raises this ValueError. This keeps the mock's embedding source deterministic and explicit.

Source

Thrown at haystack/components/embedders/mock_text_embedder.py:78

        Creates an instance of MockTextEmbedder.

        :param embedding: An optional fixed embedding returned for every input. Mutually exclusive with
            `embedding_fn`. If neither is provided, a deterministic embedding is derived from the input text.
        :param embedding_fn: An optional callable that receives the prepared text (after `prefix`/`suffix` are
            applied) 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 the text before embedding.
        :param suffix: A string to add at the end of the text before embedding.
        :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._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
        return default_to_dict(
            self,
            embedding=self.embedding,

View on GitHub (pinned to e318778c9b)

Solutions

  1. Keep only one of `embedding` / `embedding_fn` in the constructor call
  2. Delete the static `embedding` if the callable should drive values, or drop `embedding_fn` for a fixed vector

Example fix

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

Strategy: validation

Validate before calling

if embedding is not None and embedding_fn is not None:
    embedding = None  # keep embedding_fn
embedder = MockTextEmbedder(embedding=embedding, embedding_fn=embedding_fn)

Try / catch

try:
    embedder = MockTextEmbedder(embedding=emb, embedding_fn=fn)
except ValueError as e:
    logging.warning("Both embedding sources given: %s", e)
    embedder = MockTextEmbedder(embedding_fn=fn)

Prevention

When it happens

Trigger: `MockTextEmbedder(embedding=[0.1], embedding_fn=lambda text: [0.1])` — both arguments non-None in one constructor call.

Common situations: Combining a base config with an override so both fields end up populated; switching from a fixed vector to a callable during refactoring and leaving the old argument; copy-pasting fixture code that already set `embedding`.

Related errors


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