{"record":{"id":"51dc856614af3812","repo":"deepset-ai/haystack","slug":"mocktextembedder-expects-a-string-as-an-input-in","errorCode":null,"errorMessage":"MockTextEmbedder expects a string as an input. In case you want to embed a list of Documents, please use the MockDocumentEmbedder.","messagePattern":"MockTextEmbedder expects a string as an input\\. In case you want to embed a list of Documents, please use the MockDocumentEmbedder\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"haystack/components/embedders/mock_text_embedder.py","lineNumber":140,"sourceCode":"        if self.embedding is not None:\n            return list(self.embedding)\n        return _deterministic_embedding(text, self.dimension)\n\n    @component.output_types(embedding=list[float], meta=dict[str, Any])\n    def run(self, text: str) -> dict[str, Any]:\n        \"\"\"\n        Return a deterministic embedding for the input text without calling any API.\n\n        :param text: The text to embed.\n        :returns: A dictionary with the following keys:\n            - `embedding`: The embedding of the input text.\n            - `meta`: Metadata about the (mock) model.\n        :raises TypeError: If `text` is not a string.\n        \"\"\"\n        self.warm_up()\n\n        if not isinstance(text, str):\n            raise TypeError(\n                \"MockTextEmbedder expects a string as an input. \"\n                \"In case you want to embed a list of Documents, please use the MockDocumentEmbedder.\"\n            )\n\n        text_to_embed = self.prefix + text + self.suffix\n        meta: dict[str, Any] = {\"model\": self.model, \"usage\": _estimate_usage([text_to_embed])}\n        meta.update(self.meta)\n        return {\"embedding\": self._embed(text_to_embed), \"meta\": meta}\n\n    @component.output_types(embedding=list[float], meta=dict[str, Any])\n    async def run_async(self, text: str) -> dict[str, Any]:\n        \"\"\"\n        Asynchronously return a deterministic embedding for the input text without calling any API.\n\n        :param text: The text to embed.\n        :returns: A dictionary with the following keys:\n            - `embedding`: The embedding of the input text.\n            - `meta`: Metadata about the (mock) model.","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/embedders/mock_text_embedder.py#L122-L158","documentation":"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.","triggerScenarios":"`MockTextEmbedder().run([\"text1\", \"text2\"])`, `.run(Document(content=\"x\"))`, or `.run(None)` — any non-str `text` argument.","commonSituations":"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.","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"],"exampleFix":"// before\nresult = embedder.run([Document(content=\"hello\")])\n// after\nresult = embedder.run(\"hello\")","handlingStrategy":"type-guard","validationCode":"assert isinstance(text, str), f\"MockTextEmbedder.run expects str, got {type(text)}\"\nresult = embedder.run(text)","typeGuard":"def is_embeddable_text(value) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    result = embedder.run(text)\nexcept TypeError:\n    if isinstance(text, list) and text and isinstance(text[0], Document):\n        result = document_embedder.run(text)\n    else:\n        raise","preventionTips":["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"],"tags":["type-error","mock","python","input-validation"],"backgroundTag":"wrong-input-type","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}