langchain-ai/langchain · error · NotImplementedError
Unsupported message type: {type(message)}
Error message
Unsupported message type: {type(message)} What it means
NotImplementedError raised in _convert_to_message_template when a message passed to from_messages is not one of the supported types: Base(BaseChat)Message(Base)PromptTemplate, BaseMessage, str, tuple, or dict. This is the catch-all for unsupported input shapes.
Source
Thrown at libs/core/langchain_core/prompts/chat.py:1489
message_type_str, template, template_format=template_format
)
elif (
hasattr(message_type_str, "model_fields")
and "type" in message_type_str.model_fields
):
message_type = message_type_str.model_fields["type"].default
message_ = _create_template_from_message_type(
message_type, template, template_format=template_format
)
else:
message_ = message_type_str(
prompt=PromptTemplate.from_template(
cast("str", template), template_format=template_format
)
)
else:
msg = f"Unsupported message type: {type(message)}" # type: ignore[unreachable]
raise NotImplementedError(msg)
return message_
# For backwards compat:
_convert_to_message = _convert_to_message_template
View on GitHub (pinned to e32fa9a52e)
Solutions
- Check `type(message)` in the error output and convert the item to str/tuple/dict/BaseMessage.
- Flatten nested lists: from_messages([*inner]) instead of from_messages([inner]).
- Guard dynamic pipelines by asserting each item is str/tuple/dict/BaseMessage before building the prompt.
Example fix
# before
ChatPromptTemplate.from_messages([[("human", "hi")]])
# after
ChatPromptTemplate.from_messages([("human", "hi")]) Defensive patterns
Strategy: type-guard
Validate before calling
from langchain_core.messages import BaseMessage
def supported_message(m):
return isinstance(m, (str, tuple, dict, BaseMessage)) Type guard
from langchain_core.messages import BaseMessage
from typing import Any
def is_message_like(m: Any) -> bool:
return isinstance(m, (str, tuple, dict, BaseMessage)) Try / catch
try:
prompt = ChatPromptTemplate.from_messages(messages)
except NotImplementedError as e:
raise ValueError(f"Unsupported message shape in list: {e}") from e Prevention
- Flatten nested lists of messages before passing to from_messages.
- Validate each element is str/tuple/dict/BaseMessage in dynamic prompt builders.
- Add type hints (list[MessageLikeRepresentation]) so mypy catches bad element types early.
When it happens
Trigger: `from_messages([42])`, `from_messages([None])`, `from_messages([b"bytes message"])`, or passing a generator/iterable of messages as a single element rather than unpacking it.
Common situations: Passing bytes, None, or a number because a variable was never rendered; passing a nested list [[...]] instead of splatting; custom objects that aren't message subclasses.
Related errors
- Invalid placeholder template: {template}. Expected a variabl
- Unexpected arguments for placeholder message type. Expected
- Expected is_optional to be a boolean. Got: {is_optional}
- Expected variable name to be a string. Got: {var_name_wrappe
- Invalid placeholder template: {var_name_wrapped}. Expected a
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/97dd451e514dcc00.
Report an issue: GitHub.