createReactAgent does not support stateModifier with custom stateSchema types #160

Closed
opened 2026-02-15 17:16:35 -05:00 by yindo · 2 comments
Owner

Originally created by @luizzappa on GitHub (Jan 25, 2025).

Issue Description

The createReactAgent function does not accept a stateSchema different from MessagesAnnotation.

Current Behavior

When trying to use a custom stateSchema, the linter throws an error as shown below:

Image

Image

import { ChatOpenAI } from "@langchain/openai";
import { Annotation} from "@langchain/langgraph";
import { MessagesAnnotation } from "@langchain/langgraph";
import { createReactAgent } from '@langchain/langgraph/prebuilt';

const StateAnnotation = Annotation.Root({
  ...MessagesAnnotation.spec,
  // user provided
  lastName: Annotation<string>,
  // updated by the tool
  userInfo: Annotation<Record<string, any>>,
  });

const stateModifier = (state: typeof StateAnnotation.State) => {
  const userInfo = state.userInfo;
  if (userInfo == null) {
    return state.messages;
  }
  const systemMessage = `User name is ${userInfo.name}. User lives in ${userInfo.location}`;
  return [{
    role: "system",
    content: systemMessage,
  }, ...state.messages];
};

const model = new ChatOpenAI({
  model: "gpt-4o",
});

const agent = createReactAgent({
  llm: model,
  stateSchema: StateAnnotation,
  stateModifier: stateModifier,
})

Additional Information

Repository Version: "@langchain/langgraph": "^0.2.38",

Originally created by @luizzappa on GitHub (Jan 25, 2025). # Issue Description The `createReactAgent` function does not accept a `stateSchema` different from `MessagesAnnotation`. # Current Behavior When trying to use a custom `stateSchema`, the linter throws an error as shown below: ![Image](https://github.com/user-attachments/assets/dd61c101-3e8c-41e6-a5ed-9343f368a13f) ![Image](https://github.com/user-attachments/assets/21752d4d-5eb1-44cb-978e-b9f787e8a7c5) ```typescript import { ChatOpenAI } from "@langchain/openai"; import { Annotation} from "@langchain/langgraph"; import { MessagesAnnotation } from "@langchain/langgraph"; import { createReactAgent } from '@langchain/langgraph/prebuilt'; const StateAnnotation = Annotation.Root({ ...MessagesAnnotation.spec, // user provided lastName: Annotation<string>, // updated by the tool userInfo: Annotation<Record<string, any>>, }); const stateModifier = (state: typeof StateAnnotation.State) => { const userInfo = state.userInfo; if (userInfo == null) { return state.messages; } const systemMessage = `User name is ${userInfo.name}. User lives in ${userInfo.location}`; return [{ role: "system", content: systemMessage, }, ...state.messages]; }; const model = new ChatOpenAI({ model: "gpt-4o", }); const agent = createReactAgent({ llm: model, stateSchema: StateAnnotation, stateModifier: stateModifier, }) ``` # Additional Information Repository Version: "@langchain/langgraph": "^0.2.38",
yindo closed this issue 2026-02-15 17:16:35 -05:00
Author
Owner

@jacoblee93 commented on GitHub (Jan 29, 2025):

Thanks for flagging, will have a look

@jacoblee93 commented on GitHub (Jan 29, 2025): Thanks for flagging, will have a look
Author
Owner

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

Hello! For custom schema, please use stateSchema instead of stateModifier

import { Annotation, MessagesAnnotation } from "@langchain/langgraph";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";

const agent = createReactAgent({
  llm: new ChatOpenAI({ model: "gpt-4o-mini" }),
  tools: [],
  stateSchema: Annotation.Root({
    ...MessagesAnnotation.spec,
    random: Annotation<number>(),
  }),
});
@dqbd commented on GitHub (Jun 17, 2025): Hello! For custom schema, please use `stateSchema` instead of `stateModifier` ``` import { Annotation, MessagesAnnotation } from "@langchain/langgraph"; import { createReactAgent } from "@langchain/langgraph/prebuilt"; import { ChatOpenAI } from "@langchain/openai"; const agent = createReactAgent({ llm: new ChatOpenAI({ model: "gpt-4o-mini" }), tools: [], stateSchema: Annotation.Root({ ...MessagesAnnotation.spec, random: Annotation<number>(), }), }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#160