Closed Chengdyc closed 1 day ago
Can you please upgrade your langchain version? I just tried and it seems to be working on the latest version.
Could you specify which version? I'm using langchain 0.3.1 which is the latest https://pypi.org/project/langchain/
The last release on pypi is indeed 0.3.1. But the latest langchain release is 0.3.7
Thanks, just upgraded to langchain-core 0.3.7 but the problem persists.
$ poetry show langchain-core
name : langchain-core
version : 0.3.7
description : Building applications with LLMs through composability
$ poetry show langchain
name : langchain
version : 0.3.1
description : Building applications with LLMs through composability
If I understand correctly, a type check is needed in the model_validator for AIMessage to narrow the type as suggested by Pydantic documentation.
Have you tried turning the Kernel off and on again? Worked for me! I also updated langchain_core after I tested your code with Pydantic V1 and V2
Thanks, I'm not running from a notebook environment, just running from console inside a Python virtual env.
source venv/bin/activate
poetry run python3 app/repro_langchain_bug.py
I updated langchain-core to 3.8 and I can still repro the bug.
AttributeError: 'HumanMessage' object has no attribute 'get'
Did you tried to run your code in a new environment? I tested your code in a new python venv and in a new poetry shell. It runs without an error. I only added print statements at the end.
Poetry Version: 1.8.3 Python: 3.12.7
name = "langchain-core" version = "0.3.8"
name = "pydantic" version = "2.9.2"
command:
poetry run python bug.py
output:
a: hist=[HumanMessage(content="i'm human", additional_kwargs={}, response_metadata={})]
b1: hist=[HumanMessage(content="i'm human", additional_kwargs={}, response_metadata={})]
b2: hist=[ToolMessage(content="i'm tool", tool_call_id='123')]
b3: hist=[HumanMessage(content="i'm human", additional_kwargs={}, response_metadata={}), ToolMessage(content="i'm tool", tool_call_id='123')]
c1: hist=[AIMessage(content="i'm AI", additional_kwargs={}, response_metadata={})]
c2: hist=[HumanMessage(content="i'm human", additional_kwargs={}, response_metadata={})]
c3: hist=[AIMessage(content="i'm AI", additional_kwargs={}, response_metadata={}), HumanMessage(content="i'm human", additional_kwargs={}, response_metadata={})]
Thanks, must be something with my poetry / python environment. I ended up reinstalling python 11 and poetry. now the code works and doesn't fail any more.
Checked other resources
Example Code
exception is raised when initializing 'c2' and 'c3'
Error Message and Stack Trace (if applicable)
Description
I have a pydantic BaseModel object that contains a list of messages from chat history, the type is a list of AIMessage or HumanMessage. After upgrading to Langchain 0.3.1, I started getting errors when creating the object with HumanMessages in the list.
The problem is with the 'values.get()' call in AIMessage's model validator https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/messages/ai.py#L118
the model validator is invoked on HumanMessage's when they are part of the list.
The same code was working fine in langchain 0.2 before upgrading to 0.3
System Info