Skip to main content

MESSAGE_COERCION_FAILURE

LangChain 中的多个模块使用 MessageLike 对象代替正式的 BaseMessage 类。这些包括 OpenAI 风格的消息对象 ({ role: "user", content: "Hello world!" })、元组和纯字符串(转换为 HumanMessages)。

¥Several modules in LangChain take MessageLike objects in place of formal BaseMessage classes. These include OpenAI style message objects ({ role: "user", content: "Hello world!" }), tuples, and plain strings (which are converted to HumanMessages).

如果其中一个模块接收到超出这些格式的值,你将收到如下错误:

¥If one of these modules receives a value outside of one of these formats, you will receive an error like the following:

const badlyFormattedMessageObject = {
role: "foo",
randomNonContentValue: "bar",
};

await model.invoke([badlyFormattedMessageObject]);
Error: Unable to coerce message from array: only human, AI, system, or tool message coercion is currently supported.

Received: {
"role": "foo",
"randomNonContentValue": "bar",
}

故障排除

¥Troubleshooting

以下内容可能有助于解决此错误:

¥The following may help resolve this error:

  • 确保聊天模型的所有输入都是 LangChain 消息类的数组或受支持的类消息。

    ¥Ensure that all inputs to chat models are an array of LangChain message classes or a supported message-like.

    • 检查是否存在字符串化或其他意外转换。

      ¥Check that there is no stringification or other unexpected transformation occuring.

  • 检查错误的堆栈跟踪并添加日志或调试器语句。

    ¥Check the error's stack trace and add log or debugger statements.