{"record":{"id":"88a5860486befc50","repo":"deepset-ai/haystack","slug":"the-self-class-name-expects-a-list-conta","errorCode":null,"errorMessage":"The {self.__class__.__name__} expects a list containing only ChatMessage instances. The provided list contains other types. Please ensure that all elements in the list are ChatMessage instances.","messagePattern":"The (.+?) expects a list containing only ChatMessage instances\\. The provided list contains other types\\. Please ensure that all elements in the list are ChatMessage instances\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/builders/chat_prompt_builder.py","lineNumber":253,"sourceCode":"            - `prompt`: The updated list of `ChatMessage` objects after rendering the templates.\n        :raises ValueError:\n            If `chat_messages` is empty or contains elements that are not instances of `ChatMessage`.\n        \"\"\"\n        kwargs = kwargs or {}\n        template_variables = template_variables or {}\n        template_variables_combined = {**kwargs, **template_variables}\n\n        if template is None:\n            template = self.template\n\n        if not template:\n            raise ValueError(\n                f\"The {self.__class__.__name__} requires a non-empty list of ChatMessage instances. \"\n                f\"Please provide a valid list of ChatMessage instances to render the prompt.\"\n            )\n\n        if isinstance(template, list) and not all(isinstance(message, ChatMessage) for message in template):\n            raise ValueError(\n                f\"The {self.__class__.__name__} expects a list containing only ChatMessage instances. \"\n                f\"The provided list contains other types. Please ensure that all elements in the list \"\n                f\"are ChatMessage instances.\"\n            )\n\n        processed_messages = []\n        if isinstance(template, list):\n            for message in template:\n                if message.is_from(ChatRole.USER) or message.is_from(ChatRole.SYSTEM):\n                    self._validate_variables(set(template_variables_combined.keys()))\n                    if message.text is None:\n                        raise ValueError(NO_TEXT_ERROR_MESSAGE.format(role=message.role.value, message=message))\n                    if message.text and \"templatize_part\" in message.text:\n                        raise ValueError(FILTER_NOT_ALLOWED_ERROR_MESSAGE)\n                    compiled_template = self._env.from_string(message.text)\n                    rendered_text = compiled_template.render(template_variables_combined)\n                    # use dataclasses.replace to avoid in-place mutation of the original message\n                    rendered_message: ChatMessage = replace(message, _content=[TextContent(text=rendered_text)])","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/builders/chat_prompt_builder.py#L235-L271","documentation":"ChatPromptBuilder.run raises when a list template is supplied but one or more elements are not ChatMessage instances. The builder can only render ChatMessage templates, so mixed or wrong-typed lists are rejected.","triggerScenarios":"Calling run(template=[...]) where the list contains plain strings, dicts, or a mix of str and ChatMessage.","commonSituations":"Reusing a PromptBuilder-style string list; constructing messages by hand and accidentally appending raw strings; deserialization returning dicts instead of ChatMessage objects.","solutions":["Wrap plain strings in ChatMessage objects, e.g. ChatMessage.from_user(text).","Validate all elements with isinstance(m, ChatMessage) before calling run.","If you have string templates, use PromptBuilder or convert them to ChatMessage lists first."],"exampleFix":"// before\nbuilder.run(template=['Hello {{ name }}'])\n// after\nbuilder.run(template=[ChatMessage.from_user('Hello {{ name }}')])","handlingStrategy":"type-guard","validationCode":"from haystack.dataclasses import ChatMessage\nassert all(isinstance(m, ChatMessage) for m in template), 'all elements must be ChatMessage'","typeGuard":"def all_chat_messages(t: list) -> bool:\n    return isinstance(t, list) and all(isinstance(m, ChatMessage) for m in t)","tryCatchPattern":"try:\n    res = builder.run(template=template, **kwargs)\nexcept ValueError as e:\n    if 'only ChatMessage instances' in str(e):\n        template = [ChatMessage.from_user(m) if isinstance(m, str) else m for m in template]\n        res = builder.run(template=template, **kwargs)\n    else:\n        raise","preventionTips":["Normalize incoming prompt data to ChatMessage objects at pipeline boundaries.","Avoid mixing raw strings with ChatMessage objects in template lists.","Use a shared helper to build template lists."],"tags":["type-error","chat-prompt-builder","validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}