There's no way to update state with updates that are different from the state type #22

Closed
opened 2026-02-15 17:05:36 -05:00 by yindo · 3 comments
Owner

Originally created by @davidfant on GitHub (Apr 25, 2024).

Moving from Slack: https://langchainpart-saq4831.slack.com/archives/C06PTQF6MCK/p1714034843538009

Current types for the state:

export type BinaryOperator<Value> = (a: Value, b: Value) => Value;

export interface StateGraphArgs<Channels extends Record<string, any>> {
    channels: {
        [K in keyof Channels]: {
            value: BinaryOperator<Channels[K]> | null;
            default?: () => Channels[K];
        } | string;
    } | {
        value: BinaryOperator<unknown> | null;
        default?: () => unknown;
    };
}

What I want to do:

interface AgentState {
  messages: Messages[];
}

// this looks like how you suggest managing msgs in examples
const channels = {
  messages: (oldMsgs, newMsgs) => [...oldMsgs, ...newMsgs],
}

// to add a message, i'd return a state update like
return { messages: [newMessage] };

However, if I want to update messages in a different way than just appending (say e.g. clearing all msgs) I can't do that bc the BinaryOperator type requires a (curr value) and b (the update value, which could in theory have a different shape) to be the same

Originally created by @davidfant on GitHub (Apr 25, 2024). Moving from Slack: https://langchainpart-saq4831.slack.com/archives/C06PTQF6MCK/p1714034843538009 Current types for the state: ```ts export type BinaryOperator<Value> = (a: Value, b: Value) => Value; export interface StateGraphArgs<Channels extends Record<string, any>> { channels: { [K in keyof Channels]: { value: BinaryOperator<Channels[K]> | null; default?: () => Channels[K]; } | string; } | { value: BinaryOperator<unknown> | null; default?: () => unknown; }; } ``` What I want to do: ```ts interface AgentState { messages: Messages[]; } // this looks like how you suggest managing msgs in examples const channels = { messages: (oldMsgs, newMsgs) => [...oldMsgs, ...newMsgs], } // to add a message, i'd return a state update like return { messages: [newMessage] }; ``` However, if I want to update messages in a different way than just appending (say e.g. clearing all msgs) I can't do that bc the `BinaryOperator` type requires a (curr value) and b (the update value, which could in theory have a different shape) to be the same
yindo closed this issue 2026-02-15 17:05:37 -05:00
Author
Owner

@davidfant commented on GitHub (Apr 25, 2024):

@functorism might have some good ideas on typing for this

@davidfant commented on GitHub (Apr 25, 2024): @functorism might have some good ideas on typing for this
Author
Owner

@MirrorLimit commented on GitHub (Oct 23, 2024):

Hey, we're a group of students from the University of Toronto and we can take a look at this to see if we can help.

@MirrorLimit commented on GitHub (Oct 23, 2024): Hey, we're a group of students from the University of Toronto and we can take a look at this to see if we can help.
Author
Owner

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

This should be already solved by providing custom update type in Annotation or Zod

@dqbd commented on GitHub (Jun 17, 2025): This should be already solved by providing custom update type in `Annotation` or `Zod`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#22