deepset-ai/haystack · error · TypeError
MockTextEmbedder expects a string as an input. In case you w
Error message
MockTextEmbedder expects a string as an input. In case you want to embed a list of Documents, please use the MockDocumentEmbedder.
What it means
MockTextEmbedder.run accepts only a single string. Passing anything else (a list, a Document, None) raises this TypeError, which directs you to MockDocumentEmbedder for Document-list inputs. The check keeps the mock's interface identical to the real OpenAITextEmbedder.
Source
Thrown at haystack/components/embedders/mock_text_embedder.py:140
if self.embedding is not None:
return list(self.embedding)
return _deterministic_embedding(text, self.dimension)
@component.output_types(embedding=list[float], meta=dict[str, Any])
def run(self, text: str) -> dict[str, Any]:
"""
Return a deterministic embedding for the input text without calling any API.
:param text: The text to embed.
:returns: A dictionary with the following keys:
- `embedding`: The embedding of the input text.
- `meta`: Metadata about the (mock) model.
:raises TypeError: If `text` is not a string.
"""
self.warm_up()
if not isinstance(text, str):
raise TypeError(
"MockTextEmbedder expects a string as an input. "
"In case you want to embed a list of Documents, please use the MockDocumentEmbedder."
)
text_to_embed = self.prefix + text + self.suffix
meta: dict[str, Any] = {"model": self.model, "usage": _estimate_usage([text_to_embed])}
meta.update(self.meta)
return {"embedding": self._embed(text_to_embed), "meta": meta}
@component.output_types(embedding=list[float], meta=dict[str, Any])
async def run_async(self, text: str) -> dict[str, Any]:
"""
Asynchronously return a deterministic embedding for the input text without calling any API.
:param text: The text to embed.
:returns: A dictionary with the following keys:
- `embedding`: The embedding of the input text.
- `meta`: Metadata about the (mock) model.View on GitHub (pinned to e318778c9b)
Solutions
- Pass a single string: `embedder.run("text to embed")`
- Use MockDocumentEmbedder with a list of Documents if you need document-level embedding
- Unwrap Documents first: `run(document.content)` when you have a single Document
Example fix
// before
result = embedder.run([Document(content="hello")])
// after
result = embedder.run("hello") Defensive patterns
Strategy: type-guard
Validate before calling
assert isinstance(text, str), f"MockTextEmbedder.run expects str, got {type(text)}"
result = embedder.run(text) Type guard
def is_embeddable_text(value) -> bool:
return isinstance(value, str) Try / catch
try:
result = embedder.run(text)
except TypeError:
if isinstance(text, list) and text and isinstance(text[0], Document):
result = document_embedder.run(text)
else:
raise Prevention
- TextEmbedder = one string; DocumentEmbedder = list of Documents — keep this mapping handy
- Unwrap Document.content before calling a text embedder
- Use pipeline type checks (connect) to catch mismatches at wiring time
When it happens
Trigger: `MockTextEmbedder().run(["text1", "text2"])`, `.run(Document(content="x"))`, or `.run(None)` — any non-str `text` argument.
Common situations: Wiring a DocumentEmbedder-style input into a TextEmbedder in a pipeline; batching code that passes a list where one string is expected; tests copied from the document embedder.
Related errors
- MockDocumentEmbedder expects a list of Documents as input.In
- 'response_fn' must return a string or ChatMessage, got {type
- Unsupported source type {type(source)}
- meta must be either None, a dictionary or a list of dictiona
- Pass either 'embedding' or 'embedding_fn', not both.
AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30).
Data as JSON: /api/errors/51dc856614af3812.
Report an issue: GitHub.