How to add an initial greeting with no input #919
Replies: 5 comments
-
If you want to invoke it with an empty state, I think you can invoke it with an empty version of your state: app.invoke({}) # Empty input If this doesn't satisfy your requirements, could you clarify why? Thanks! |
Beta Was this translation helpful? Give feedback.
-
This does not work. I get "langgraph.errors.InvalidUpdateError: Must write to at least one of ['messages']" |
Beta Was this translation helpful? Give feedback.
-
app.invoke({"messages": []}) # Empty input then? It depends on your graph state. Or could add a state variable
that you toggle on once you've sent the intro sequence |
Beta Was this translation helpful? Give feedback.
-
Of course, that error is being introduced because there is no way to set up a LangGraph agent to send an initial message to the user from the start node. If there were, I wouldn't be trying to change the state manually. |
Beta Was this translation helpful? Give feedback.
-
@francisjervis , the error you are seeing is because "state" in It doesn't have a method called The The following code seems to be what you're looking for? from langgraph.graph import StateGraph, END
from langgraph.graph.message import add_messages
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from typing import Annotated
from typing_extensions import TypedDict
from langchain_core.runnables import chain, RunnableLambda
class State(TypedDict):
# Messages have the type "list". The `add_messages` function
# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
graph_state = StateGraph(State)
welcome_message = AIMessage(content="Welcome to the hello, world! state machine.")
def handle_greeting_node(state):
if state["messages"]:
# We've already greeted the user
return {"messages": []}
# Return a greeting ,since no message has been sent
return {"messages": [welcome_message]}
graph_state.add_node("handle_greeting_node", lambda state: handle_greeting_node(state))
# graph_state.add_node("handle_user_input", handle_user_input)
graph_state.set_entry_point("handle_greeting_node")
# graph_state.add_edge("handle_greeting_node", "handle_user_input")
graph_state.add_edge("handle_greeting_node", END)
app = graph_state.compile()
app.invoke({"messages": []}) |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Traceback (most recent call last):
File "scratch_4.py", line 51, in
app.invoke()
TypeError: Pregel.invoke() missing 1 required positional argument: 'input'
Description
It appears to be impossible to set up a LangGraph agent to send an initial message to the user from the start node. This means that the user is always presented with a "blank screen" which is unacceptable in production. This is due to "input" being required when it should be optional.
System Info
langgraph 0.0.66
python 3.11
mac os 14.5
Beta Was this translation helpful? Give feedback.
All reactions