{"record":{"id":"1493d9aaebe56634","repo":"deepset-ai/haystack","slug":"invalid-messages-type-expected-list-chatmessage","errorCode":null,"errorMessage":"Invalid messages type. Expected list[ChatMessage] or str.","messagePattern":"Invalid messages type\\. Expected list\\[ChatMessage\\] or str\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"haystack/components/generators/utils.py","lineNumber":225,"sourceCode":"    \"\"\"\n    if hasattr(obj, \"model_dump\"):\n        return obj.model_dump()\n    if hasattr(obj, \"__dict__\"):\n        return {k: _serialize_object(v) for k, v in obj.__dict__.items() if not k.startswith(\"_\")}\n    if isinstance(obj, dict):\n        return {k: _serialize_object(v) for k, v in obj.items()}\n    if isinstance(obj, list):\n        return [_serialize_object(item) for item in obj]\n    return obj\n\n\ndef _normalize_messages(messages: list[ChatMessage] | str) -> list[ChatMessage]:\n    \"\"\"Normalize messages to a list of ChatMessage objects.\"\"\"\n    if isinstance(messages, str):\n        return [ChatMessage.from_user(messages)]\n    if isinstance(messages, list) and all(isinstance(msg, ChatMessage) for msg in messages):\n        return messages\n    raise TypeError(\"Invalid messages type. Expected list[ChatMessage] or str.\")\n","sourceCodeStart":207,"sourceCodeEnd":226,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/generators/utils.py#L207-L226","documentation":"_normalize_messages accepts either a single string (treated as one user message) or a list of ChatMessage objects. Anything else raises TypeError, which is also surfaced for lists containing non-ChatMessage items.","triggerScenarios":"Passing a plain dict, a list of strings (e.g. [\"hi\"]), a single ChatMessage (not wrapped in a list), or None to a generator/util that expects list[ChatMessage] | str.","commonSituations":"After a version change where a generator switched from accepting str|list[str] to list[ChatMessage]; passing prompt strings or dicts built for other APIs.","solutions":["Convert legacy strings: [ChatMessage.from_user(s) for s in list_of_strings]","Wrap a single ChatMessage in a list: [msg]","If a dict is passed, convert to ChatMessage via the appropriate constructor"],"exampleFix":"// before\nllm.run(messages=[\"hello\", \"world\"])\n// after\nllm.run(messages=[ChatMessage.from_user(\"hello\"), ChatMessage.from_user(\"world\")])","handlingStrategy":"type-guard","validationCode":"if isinstance(messages, str):\n    messages = [ChatMessage.from_user(messages)]\nelif not (isinstance(messages, list) and all(isinstance(m, ChatMessage) for m in messages)):\n    raise TypeError(\"Expected list[ChatMessage] or str\")","typeGuard":"def is_valid_messages(v) -> bool:\n    return isinstance(v, str) or (isinstance(v, list) and all(isinstance(m, ChatMessage) for m in v))","tryCatchPattern":"try:\n    gen.run(messages=messages)\nexcept TypeError as e:\n    if \"Invalid messages type\" in str(e):\n        messages = [ChatMessage.from_user(s) for s in messages]\n        gen.run(messages=messages)","preventionTips":["Wrap single ChatMessage in a list","Convert raw strings with ChatMessage.from_user","Check migration notes when upgrading generators"],"tags":["python","haystack","type-error","messages"],"backgroundTag":"invalid-messages-type","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}