{"record":{"id":"8f2133a6264c547e","repo":"deepset-ai/haystack","slug":"responses-must-be-a-string-chatmessage-or-a-se","errorCode":null,"errorMessage":"'responses' must be a string, ChatMessage, or a sequence of them, got {type(responses)}.","messagePattern":"'responses' must be a string, ChatMessage, or a sequence of them, got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"haystack/components/generators/chat/mock.py","lineNumber":153,"sourceCode":"        self.streaming_callback = streaming_callback\n        self._call_count = 0\n        self._is_warmed_up = False\n\n    @staticmethod\n    def _normalize_responses(\n        responses: str | ChatMessage | Sequence[str | ChatMessage] | None,\n    ) -> list[ChatMessage] | None:\n        \"\"\"Normalize the `responses` argument into a non-empty list of `ChatMessage`, or `None` for echo mode.\"\"\"\n        if responses is None:\n            return None\n\n        items: list[str | ChatMessage]\n        if isinstance(responses, (str, ChatMessage)):\n            items = [responses]\n        elif isinstance(responses, Sequence):\n            items = list(responses)\n        else:\n            raise TypeError(f\"'responses' must be a string, ChatMessage, or a sequence of them, got {type(responses)}.\")\n\n        if len(items) == 0:\n            raise ValueError(\"'responses' must not be an empty list.\")\n\n        normalized: list[ChatMessage] = []\n        for item in items:\n            if isinstance(item, str):\n                normalized.append(ChatMessage.from_assistant(item))\n            elif isinstance(item, ChatMessage):\n                if item.role != ChatRole.ASSISTANT:\n                    raise ValueError(\n                        f\"Each ChatMessage response must have the 'assistant' role, got '{item.role.value}'.\"\n                    )\n                normalized.append(item)\n            else:\n                raise TypeError(f\"Each response must be a string or ChatMessage, got {type(item)}.\")\n        return normalized\n","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/generators/chat/mock.py#L135-L171","documentation":"MockChatGenerator._normalize_responses only accepts a single string, a single ChatMessage, or a sequence (list/tuple) of those. Any other type (dict, int, a set, a generator-like object not a Sequence) raises TypeError naming the offending type.","triggerScenarios":"Calling `MockChatGenerator(responses={...})` or with a non-iterable scalar like an int/float, or with a custom object masquerading as a response list.","commonSituations":"Passing a dict of {prompt: reply} expecting dict support; JSON round-trips turning the list into another type; typos like responses=\"hi\" split incorrectly or responses=True.","solutions":["Wrap a single value in a list: responses=[\"my reply\"]","Use a list of strings or ChatMessages: responses=[ChatMessage.from_assistant(\"hi\")]","If you have a dict, convert to a list of ChatMessages yourself or use response_fn","Check the variable actually holds the list you think (print/log type before constructing)"],"exampleFix":"// before\nmock = MockChatGenerator(responses=\"hi\")  # actually valid only if str; dicts raise\nmock = MockChatGenerator(responses={\"a\": \"b\"})\n// after\nmock = MockChatGenerator(responses=[ChatMessage.from_assistant(\"b\")])","handlingStrategy":"type-guard","validationCode":"if not isinstance(responses, (str, ChatMessage, Sequence)):\n    raise TypeError(f\"responses must be str, ChatMessage, or sequence, got {type(responses)}\")","typeGuard":"def is_valid_responses(r) -> bool:\n    if isinstance(r, (str, ChatMessage)):\n        return True\n    return isinstance(r, Sequence) and all(isinstance(i, (str, ChatMessage)) for i in r)","tryCatchPattern":null,"preventionTips":["Always pass a list: responses=[...], even for a single reply","Convert dict-based fixtures to ChatMessage objects at load time","Type-annotate responses: list[str | ChatMessage]"],"tags":["mock","type-error","validation","testing"],"backgroundTag":"type-mismatch","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}