-
Notifications
You must be signed in to change notification settings - Fork 14.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error using Langgraph + Langchain -- Unknown message type exception #27052
Comments
Hmm, this code ran fine for me. Can you try upgrading your packages to the latest versions? langchain/langchain_core should be at 0.3.1 and langchain_openai should be at 0.2.1. You can check the pypi website to see the latest versions for all langchain/langgraph related packages. |
@isahers1 langchain_core at 0.3.1 will also require pydantic version to be >=2 right? Actually currently I am using pydantic 1.10.14. upgrading it to >=2 will require lot more changes in the configs |
I get the same error with the modified code (remove comments and use ChatOpenAI). import operator
from typing import Annotated, Literal, TypedDict
from dotenv import load_dotenv
from langchain.agents import create_tool_calling_agent
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.messages import AnyMessage, SystemMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, StateGraph, add_messages
from langgraph.prebuilt.tool_executor import ToolExecutor
from rich import get_console
load_dotenv()
@tool
def search(query: str) -> list[str]:
"""Call to surf the web."""
return [
"It's sunny in San Francisco, but you better look out if you're a Gemini 😈."
]
class AgentState(TypedDict):
input: str
chat_history: list[AnyMessage]
output: AgentAction | AgentFinish | None
intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]
messages: Annotated[list[AnyMessage], add_messages]
def should_continue(state: AgentState) -> Literal["end", "continue"]:
print("should_continue")
get_console().print(state) # message_log is AIMessage
if isinstance(state["output"], AgentFinish):
return "end"
return "continue"
def execute_tools(state):
print("execute_tools")
get_console().print(state) # message_log is BaseMessage
intermediate_steps = []
for agent_action in state["output"]:
output = tool_executor.invoke(agent_action)
intermediate_steps.append((agent_action, str(output)))
return {"intermediate_steps": intermediate_steps}
def call_model(state):
output = tool_calling_agent.invoke(state)
return {"output": output}
tools = [search]
tool_executor = ToolExecutor(tools)
model = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools)
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content="You are a weather reporter, and using the search tool you find the weather of the places"
),
HumanMessagePromptTemplate.from_template("{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
tool_calling_agent = create_tool_calling_agent(model, tools, prompt)
workflow = StateGraph(AgentState)
workflow.add_node("agent", call_model)
workflow.add_node("action", execute_tools)
workflow.add_edge(START, "agent")
workflow.add_conditional_edges(
"agent", should_continue, {"continue": "action", "end": END}
)
workflow.add_edge("action", "agent")
app = workflow.compile(checkpointer=MemorySaver(), interrupt_before=["action"])
thread_config = {"configurable": {"thread_id": "1"}}
app.invoke(input={"input": "search for the weather in sf now"}, config=thread_config)
print("get_state")
get_console().print(app.get_state(thread_config)) # message_log is BaseMessage
response = app.invoke(None, config=thread_config) Package Versionlangchain-core==0.3.7 python-version==3.12.6 (windows) |
This is an issue with langgraph checkpoint serializer, investigating more |
This is an issue in @Subham07 in the meantime, if you're blocked by this i can recommend a couple of solutions:
|
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I am using Langgraph + Human in the loop implementation.
I checked the state snapshot values and in that the "message_log" attribute is containing BaseMessage() type data, instead of what should be AIMessage()
After we try to resume the graph execution, it gives the below stacktrace (same as what is mentioned in the above question)
System Info
Below are the versions
The text was updated successfully, but these errors were encountered: