Replies: 2 comments
-
To resolve the "Must write to at least one of []" error in your LangGraph application, you need to ensure that your graph nodes are correctly configured to write to at least one state key. This error typically occurs when a node does not produce any output that updates the state. In your provided code, each node function should return a dictionary that updates the state. For example, the Here is an example of how to ensure that each node function updates the state correctly: class AgentState(TypedDict):
messages: Annotated[list, add_messages]
self.tool_node = ToolNode(tools=self.tools)
agent_runnable = create_tool_calling_agent(llm=self.llm_with_tools, tools=self.tools, prompt=prompt_obj)
async def call_model_b(self, state):
messages = state["messages"]
response = await self.agent_runnable.ainvoke(messages)
return {"messages": messages + [response]} # Ensure the state is updated with the new messages
async def should_continue_b(self, state) -> Literal["end", "continue"]:
messages = state["messages"]
last_message = messages[-1]
# If there is no tool call, then we finish
if not last_message.tool_calls:
return "end"
# Otherwise if there is, we continue
else:
return "continue"
graph_builder = StateGraph(AgentState)
graph_builder.add_node("agent", self.call_model_b)
graph_builder.add_node("action", self.tool_node)
graph_builder.set_entry_point("agent")
graph_builder.add_conditional_edges(
"agent",
self.should_continue_b,
{
# If tools, then we call the tool node.
"continue": "action",
# Otherwise we finish.
"end": END
}
)
graph_builder.add_edge("action", "agent")
graph = graph_builder.compile()
result = await self.graph.ainvoke(inputs, debug=True) Ensure that each node function in your graph returns a dictionary that updates the state with the necessary keys. This will resolve the "Must write to at least one of []" error [1]. |
Beta Was this translation helpful? Give feedback.
-
@mrctito Not sure if you ever solved this but I've just came across the same error and what solved it for me was changing the state to have a base class of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Dear friends,
I need help with this error: "Must write to at least one of []"
Context: I cannot get my Langgraph application to work. I reproduced almost the same code that I found in examples in the official LangGraph documentation.
Relevant points from my source code:
class AgentState(TypedDict):
messages: Annotated[list, add_messages]
self.tool_node = ToolNode(tools=self.tools)
agent_runnable = create_tool_calling_agent(llm=self.llm_with_tools, tools=self.tools, prompt=prompt_obj)
async def call_model_b(self, state):
messages = state["messages"]
response = await self.agent_runnable.ainvoke(messages)
return {"messages": [response]}
async def should_continue_b(self, state) -> Literal["end", "continue"]:
messages = state["messages"]
last_message = messages[-1]
# If there is no tool call, then we finish
if not last_message.tool_calls:
return "end"
# Otherwise if there is, we continue
else:
return "continue"
graph_builder = StateGraph(StateGraph)
graph_builder.add_node("agent", self.call_model_b)
graph_builder.add_node("action", self.tool_node)
graph_builder.set_entry_point("agent")
graph_builder.add_conditional_edges(
"agent",
self.should_continue_b,
{
# If
tools
, then we call the tool node."continue": "action",
# Otherwise we finish.
"end": END
}
)
graph_builder.add_edge("action", "agent")
graph = graph_builder.compile()
result = await self.graph.ainvoke(inputs, debug=True)
This is where the error happens: "Must write to at least one of []"
The error message is simply: "Must write to at least one of []"
Can someone please help me?
Thank you!
System Info
langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.10
langchain-experimental==0.0.62
langchain-openai==0.1.13
langchain-qdrant==0.1.0
langchain-text-splitters==0.2.1
Beta Was this translation helpful? Give feedback.
All reactions