(item) AttributeError: messages

2025年09月26日 09:58 响应: 2.87秒 状态: success

🚨 错误信息

Traceback (most recent call last): File "D:\新建文件夹\langchain-base\Chatbot.py", line 25, in <module> response = with_message_history.invoke( [HumanMessage(content="Hi I'm Bob")], config=config ) File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 5710, in invoke return self.bound.invoke( ~~~~~~~~~~~~~~~~~^ input, ^^^^^^ self._merge_configs(config), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **{**self.kwargs, **kwargs}, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 5710, in invoke return self.bound.invoke( ~~~~~~~~~~~~~~~~~^ input, ^^^^^^ self._merge_configs(config), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **{**self.kwargs, **kwargs}, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 3243, in invoke input_ = context.run(step.invoke, input_, config, **kwargs) File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 5710, in invoke return self.bound.invoke( ~~~~~~~~~~~~~~~~~^ input, ^^^^^^ self._merge_configs(config), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **{**self.kwargs, **kwargs}, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 5024, in invoke return self._call_with_config( ~~~~~~~~~~~~~~~~~~~~~~^ self._invoke, ^^^^^^^^^^^^^ ...<2 lines>... **kwargs, ^^^^^^^^^ ) ^ File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 2089, in _call_with_config context.run( ~~~~~~~~~~~^ call_func_with_variable_args, # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<4 lines>... **kwargs, ^^^^^^^^^ ), ^ File "D:\software\Python\Lib\site-packages\langchain_core\runnables\config.py", line 430, in call_func_with_variable_args return func(input, **kwargs) # type: ignore[call-arg] File "D:\software\Python\Lib\site-packages\langchain_core\runnables\base.py", line 4881, in _invoke output = call_func_with_variable_args( self.func, input_, config, run_manager, **kwargs ) File "D:\software\Python\Lib\site-packages\langchain_core\runnables\config.py", line 430, in call_func_with_variable_args return func(input, **kwargs) # type: ignore[call-arg] File "D:\software\Python\Lib\site-packages\langchain_core\runnables\history.py", line 516, in _enter_history messages = hist.messages.copy() ^^^^^^^^^^^^^ File "D:\software\Python\Lib\site-packages\pydantic\_internal\_model_construction.py", line 271, in __getattr__ raise AttributeError(item) AttributeError: messages

🤖 AI解决方案

AttributeError: messages

错误翻译

AttributeError: messages

原因分析
with_message_history 对象没有名为 messages 的属性。这通常发生在您尝试访问一个不存在的属性时,或者在使用 langchain_core.runnables.history.MessageHistory 时,其内部的 messages 属性尚未正确初始化或被错误地修改。

解决方案
请检查 with_message_history 对象的初始化过程,确保它是一个有效的 MessageHistory 实例,并且其 messages 属性在被访问前是可用的。如果 with_message_history 是通过某种自定义方式创建的,请确保它实现了 langchain_core.runnables.history.MessageHistory 的接口,并且 messages 属性是其一部分。

以下是一个使用 MessageHistory 的标准示例:

from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.runnables.history import MessageHistory
from langchain_core.runnables import RunnableWithMessageHistory

# 假设您有一个 chain,这里用一个简单的示例代替
chain = lambda x: AIMessage(content="Hello") # 替换为您的实际 chain

# 创建 MessageHistory 实例
message_history = MessageHistory()

# 创建带消息历史的 Runnable
with_message_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: message_history, # 返回 MessageHistory 实例
    input_messages_key="input",
    output_messages_key="output",
)

# 示例调用
config = {"configurable": {"session_id": "any_unique_session_id"}}
response = with_message_history.invoke(
    {"input": "Hi I'm Bob"}, # 确保输入字典包含 input_messages_key
    config=config
)

print(response)

预防措施

  • 在访问任何对象的属性前,请确保该属性确实存在于对象中,可以使用 hasattr() 或查阅相关库的文档来确认。

  • 对于 RunnableWithMessageHistory,请始终遵循其预期的输入格式,并确保 MessageHistory 实例被正确地传递和初始化。