DOC: System Prompt in Create ReAct agent is un effective #366

Closed
opened 2026-02-20 17:38:42 -05:00 by yindo · 1 comment
Owner

Originally created by @DucretJe on GitHub (Dec 19, 2024).

Issue with current documentation:

In the nodes and edge definition from How to create a ReAct agent from scratch, we declare a system_prompt variable and pass it to the model.invoke along the state.

This has in fact no effect, I replaced the prompt with:

    system_prompt = SystemMessage(
        "Speak in Italian"
    )

We get an english answer.

I also noticed that we had some warnings with this

response = model.invoke([system_prompt] + state["messages"], config)

Operator "+" not supported for types "list[SystemMessage]" and "Sequence[BaseMessage]"

Idea or request for content:

I managed to make it work with this:

    system_prompt = SystemMessage(
        "Speak in Italian"
    )
    state["messages"].insert(0, system_prompt)
    response = model.invoke(state["messages"], config)

Doing so I'm assured to have one first message containing the system prompt.

Note

If the same node is called several times (which is the case when a tool redirect to this node), the system prompt is injected multiple times... So I guess we can introduce a test checking that the first message is a System prompt.
We can imagine something like:

if not isinstance(state["messages"][0], SystemMessage):
  state["messages"].insert(0, system_prompt)
Originally created by @DucretJe on GitHub (Dec 19, 2024). ### Issue with current documentation: In the [nodes and edge definition](https://langchain-ai.github.io/langgraph/how-tos/react-agent-from-scratch/) from `How to create a ReAct agent from scratch`, we declare a `system_prompt` variable and pass it to the `model.invoke` along the state. This has in fact no effect, I replaced the prompt with: ```python system_prompt = SystemMessage( "Speak in Italian" ) ``` We get an english answer. I also noticed that we had some warnings with this ```python response = model.invoke([system_prompt] + state["messages"], config) ``` > Operator "+" not supported for types "list[SystemMessage]" and "Sequence[BaseMessage]" ### Idea or request for content: I managed to make it work with this: ```python system_prompt = SystemMessage( "Speak in Italian" ) state["messages"].insert(0, system_prompt) response = model.invoke(state["messages"], config) ``` Doing so I'm assured to have one first message containing the system prompt. > [!NOTE] > If the same node is called several times (which is the case when a tool redirect to this node), the system prompt is injected multiple times... So I guess we can introduce a test checking that the first message is a System prompt. > We can imagine something like: > ```python > if not isinstance(state["messages"][0], SystemMessage): > state["messages"].insert(0, system_prompt) > ```
yindo closed this issue 2026-02-20 17:38:42 -05:00
Author
Owner

@vbarda commented on GitHub (Dec 19, 2024):

@DucretJe you shouldn't be modifying the state object manually, it should only be modified by the updates from the nodes (i.e. return from the node function) - this is also why you're seeing the issue of the message getting injected multiple times when you modify the state manually

this is the correct way of doing it - system message is dynamically added ONLY when the model is called, but NOT added to the graph state

system_prompt = SystemMessage("Speak in Italian")
response = model.invoke([system_prompt] + state["messages"], config)

hope this helps. since this is not an issue with the documentation / langgraph, will close it, but feel free to open a discussion or another issue

@vbarda commented on GitHub (Dec 19, 2024): @DucretJe you shouldn't be modifying the state object manually, it should only be modified by the updates from the nodes (i.e. return from the node function) - this is also why you're seeing the issue of the message getting injected multiple times when you modify the state manually this is the correct way of doing it - system message is dynamically added ONLY when the model is called, but NOT added to the graph state ```python system_prompt = SystemMessage("Speak in Italian") response = model.invoke([system_prompt] + state["messages"], config) ``` hope this helps. since this is not an issue with the documentation / langgraph, will close it, but feel free to open a discussion or another issue
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#366