跳到主要內容
Open In ColabOpen on GitHub

MESSAGE_COERCION_FAILURE

LangChain 中的數個模組接受 MessageLikeRepresentation,而不是總是要求 BaseMessage 的實例,其定義為

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,
]

這些包括 OpenAI 風格的訊息物件 ({ role: "user", content: "Hello world!" })、元組和純字串(會轉換為 HumanMessages)。

如果這些模組之一收到非這些格式的值,您將收到如下錯誤

from langchain_anthropic import ChatAnthropic

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

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

model.invoke([uncoercible_message])
API 參考:ChatAnthropic
---------------------------------------------------------------------------
``````output
KeyError Traceback (most recent call last)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/messages/utils.py:318, in _convert_to_message(message)
317 # None msg content is not allowed
--> 318 msg_content = msg_kwargs.pop("content") or ""
319 except KeyError as e:
``````output
KeyError: 'content'
``````output

The above exception was the direct cause of the following exception:
``````output
ValueError Traceback (most recent call last)
``````output
Cell In[5], line 10
3 uncoercible_message = {
4 "role": "HumanMessage",
5 "random_field": "random value"
6 }
8 model = ChatAnthropic(model="claude-3-5-sonnet-20240620")
---> 10 model.invoke([uncoercible_message])
``````output
File ~/langchain/oss-py/libs/core/langchain_core/language_models/chat_models.py:287, in BaseChatModel.invoke(self, input, config, stop, **kwargs)
275 def invoke(
276 self,
277 input: LanguageModelInput,
(...)
281 **kwargs: Any,
282 ) -> BaseMessage:
283 config = ensure_config(config)
284 return cast(
285 ChatGeneration,
286 self.generate_prompt(
--> 287 [self._convert_input(input)],
288 stop=stop,
289 callbacks=config.get("callbacks"),
290 tags=config.get("tags"),
291 metadata=config.get("metadata"),
292 run_name=config.get("run_name"),
293 run_id=config.pop("run_id", None),
294 **kwargs,
295 ).generations[0][0],
296 ).message
``````output
File ~/langchain/oss-py/libs/core/langchain_core/language_models/chat_models.py:267, in BaseChatModel._convert_input(self, input)
265 return StringPromptValue(text=input)
266 elif isinstance(input, Sequence):
--> 267 return ChatPromptValue(messages=convert_to_messages(input))
268 else:
269 msg = (
270 f"Invalid input type {type(input)}. "
271 "Must be a PromptValue, str, or list of BaseMessages."
272 )
``````output
File ~/langchain/oss-py/libs/core/langchain_core/messages/utils.py:348, in convert_to_messages(messages)
346 if isinstance(messages, PromptValue):
347 return messages.to_messages()
--> 348 return [_convert_to_message(m) for m in messages]
``````output
File ~/langchain/oss-py/libs/core/langchain_core/messages/utils.py:348, in <listcomp>(.0)
346 if isinstance(messages, PromptValue):
347 return messages.to_messages()
--> 348 return [_convert_to_message(m) for m in messages]
``````output
File ~/langchain/oss-py/libs/core/langchain_core/messages/utils.py:321, in _convert_to_message(message)
319 except KeyError as e:
320 msg = f"Message dict must contain 'role' and 'content' keys, got {message}"
--> 321 raise ValueError(msg) from e
322 _message = _create_message_from_message_type(
323 msg_type, msg_content, **msg_kwargs
324 )
325 else:
``````output
ValueError: Message dict must contain 'role' and 'content' keys, got {'role': 'HumanMessage', 'random_field': 'random value'}

疑難排解

以下可能有助於解決此錯誤

  • 確保聊天模型的所有輸入都是 LangChain 訊息類別的陣列或支援的 message-like。
    • 檢查是否沒有發生字串化或其他非預期的轉換。
  • 檢查錯誤的堆疊追蹤並新增日誌或偵錯器陳述式。

此頁面是否對您有幫助?