Skip to main content
This error occurs when message objects don’t conform to the expected format.

Accepted message formats

LangChain modules accept MessageLikeRepresentation, which is defined as:
from typing import Union

from langchain_core.prompts.chat import (
    BaseChatPromptTemplate,
    BaseMessage,
    BaseMessagePromptTemplate,
)

MessageLikeRepresentation = Union[
    Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate],
    tuple[
        Union[str, type],
        Union[str, list[dict], list[object]],
    ],
    str,
]
These include OpenAI style message objects ({ role: "user", content: "Hello world!" }), tuples, and plain strings (which are converted to HumanMessage objects). If a module receives a value outside of one of these formats, you will receive an error:
from langchain_anthropic import ChatAnthropic

uncoercible_message = {"role": "HumanMessage", "random_field": "random value"}

model = ChatAnthropic(model="claude-3-5-sonnet-latest")

model.invoke([uncoercible_message])
ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random_field': 'random value'}

Troubleshooting

To resolve this error:
  1. Ensure proper format: All inputs to chat models must be an array of LangChain message classes or a supported message-like format
  2. Verify no unintended stringification or transformation occurs to your messages
  3. Examine the error’s stack trace and add logging statements to inspect message objects before they’re passed to the model

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.