{"record":{"id":"700afa82d7dc3e19","repo":"langchain-ai/langchain","slug":"got-unsupported-message-type-m","errorCode":null,"errorMessage":"Got unsupported message type: {m}","messagePattern":"Got unsupported message type: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":284,"sourceCode":"        The type string for the message element.\n\n    Raises:\n        ValueError: If an unsupported message type is encountered.\n    \"\"\"\n    if isinstance(m, HumanMessage):\n        return human_prefix.lower()\n    if isinstance(m, AIMessage):\n        return ai_prefix.lower()\n    if isinstance(m, SystemMessage):\n        return system_prefix.lower()\n    if isinstance(m, FunctionMessage):\n        return function_prefix.lower()\n    if isinstance(m, ToolMessage):\n        return tool_prefix.lower()\n    if isinstance(m, ChatMessage):\n        return m.role\n    msg = f\"Got unsupported message type: {m}\"\n    raise ValueError(msg)\n\n\ndef get_buffer_string(\n    messages: Sequence[BaseMessage],\n    human_prefix: str = \"Human\",\n    ai_prefix: str = \"AI\",\n    *,\n    system_prefix: str = \"System\",\n    function_prefix: str = \"Function\",\n    tool_prefix: str = \"Tool\",\n    message_separator: str = \"\\n\",\n    format: Literal[\"prefix\", \"xml\"] = \"prefix\",  # noqa: A002\n) -> str:\n    r\"\"\"Convert a sequence of messages to strings and concatenate them into one string.\n\n    Args:\n        messages: Messages to be converted to strings.\n        human_prefix: The prefix to prepend to contents of `HumanMessage`s.","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L266-L302","documentation":"Raised by the internal `_get_message_type_str` helper used by `get_buffer_string` when a message is not one of the known types (`HumanMessage`, `AIMessage`, `SystemMessage`, `FunctionMessage`, `ToolMessage`, `ChatMessage`). `get_buffer_string` renders a chat history as a single string, so it must map every message to a role prefix.","triggerScenarios":"Calling `get_buffer_string(messages)` (or a prompt template that uses it) with a custom `BaseMessage` subclass that is none of the six recognized classes.","commonSituations":"Defining a custom message class for a bespoke agent protocol and feeding a history containing it into `get_buffer_string`; third-party libraries adding novel message types that predate this check.","solutions":["Represent custom roles with `ChatMessage(content=..., role='my_role')`, which is handled via its `role` attribute","Convert custom messages to one of the six supported classes before calling `get_buffer_string`","Write your own buffer-string function if you truly need custom prefixes for new message classes"],"exampleFix":"# before\nclass MyMessage(BaseMessage): ...\nget_buffer_string([MyMessage(content='hi')])\n\n# after\nfrom langchain_core.messages import ChatMessage\nget_buffer_string([ChatMessage(content='hi', role='custom')])","handlingStrategy":"type-guard","validationCode":"from langchain_core.messages import (HumanMessage, AIMessage, SystemMessage,\n    FunctionMessage, ToolMessage, ChatMessage)\n\nSUPPORTED = (HumanMessage, AIMessage, SystemMessage, FunctionMessage, ToolMessage, ChatMessage)\n\ndef all_renderable(messages) -> bool:\n    return all(isinstance(m, SUPPORTED) for m in messages)","typeGuard":"def is_buffer_string_safe(m) -> bool:\n    return isinstance(m, (HumanMessage, AIMessage, SystemMessage,\n                          FunctionMessage, ToolMessage, ChatMessage))","tryCatchPattern":null,"preventionTips":["Use ChatMessage with an explicit role for non-standard message kinds","Run an isinstance check over the history before formatting prompts as strings","Keep custom BaseMessage subclasses out of paths that end in get_buffer_string"],"tags":["prompt-formatting","custom-messages","chat-history"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}