State Management doesn't works for objects #140

Closed
opened 2026-02-15 17:16:13 -05:00 by yindo · 1 comment
Owner

Originally created by @alanlozano-coru on GitHub (Nov 26, 2024).

When I use state management for the messages array it works, but when I'm trying to used them with an object doesn't works, actually I have my object like this:

const information = await bd.getInformation();
const GraphState = Annotation.Root({
    messages: Annotation<BaseMessage[]>({
      reducer: (x, y) => x.concat(y),
      default: () => [],
    }),
    userInfo: Annotation<UserInfo>({
      reducer: (x, y) => ({ ...x, ...y }),
      default: () => {
        console.log("SET INFO", information);
        return {
           name: information.name || "",
           age: information.age || 0,
        }
     },
    }),
});

The userInfo state is only used to read the default information, actually i don't want to update it, but the value that I ever get when I used on the node is the same:

SET INFO
{
name: "Bob",
age: 25
}

SUPERVISOR INFO
{
name: "user",
age: 0
}

This is an example about how I'm trying to use the state:

async function Supervisor(state: typeof GraphState.State) {
      const { messages, userInfo } = state;
      console.log("--- START SUPERVISOR ---");
      console.log("SUPERVISOR INFO", userInfo);

      const prompt = ChatPromptTemplate.fromTemplate(`... `);
      const llm = new ChatOpenAI({
        apiKey: openai_key,
        model: "gpt-4o-mini",
      });
      const response = await prompt.pipe(llm).invoke({ messages, userInfo });
      return {
        messages: [response],
      };
    }
Originally created by @alanlozano-coru on GitHub (Nov 26, 2024). When I use state management for the messages array it works, but when I'm trying to used them with an object doesn't works, actually I have my object like this: ``` const information = await bd.getInformation(); const GraphState = Annotation.Root({ messages: Annotation<BaseMessage[]>({ reducer: (x, y) => x.concat(y), default: () => [], }), userInfo: Annotation<UserInfo>({ reducer: (x, y) => ({ ...x, ...y }), default: () => { console.log("SET INFO", information); return { name: information.name || "", age: information.age || 0, } }, }), }); ``` The userInfo state is only used to read the default information, actually i don't want to update it, but the value that I ever get when I used on the node is the same: ``` SET INFO { name: "Bob", age: 25 } SUPERVISOR INFO { name: "user", age: 0 } ``` This is an example about how I'm trying to use the state: ``` async function Supervisor(state: typeof GraphState.State) { const { messages, userInfo } = state; console.log("--- START SUPERVISOR ---"); console.log("SUPERVISOR INFO", userInfo); const prompt = ChatPromptTemplate.fromTemplate(`... `); const llm = new ChatOpenAI({ apiKey: openai_key, model: "gpt-4o-mini", }); const response = await prompt.pipe(llm).invoke({ messages, userInfo }); return { messages: [response], }; } ```
yindo closed this issue 2026-02-15 17:16:13 -05:00
Author
Owner

@dqbd commented on GitHub (Jun 17, 2025):

Hello! We recommend passing the common configuration in RunnableConfig["configurable"] especially if the value does not need to be updated.

graph.invoke({ ... }, { configurable: { name: "Bob", age: 25 } })
@dqbd commented on GitHub (Jun 17, 2025): Hello! We recommend passing the common configuration in `RunnableConfig["configurable"]` especially if the value does not need to be updated. ```tsx graph.invoke({ ... }, { configurable: { name: "Bob", age: 25 } }) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#140