langchain-ai / langgraphjs

⚡ Build language agents as graphs ⚡
https://langchain-ai.github.io/langgraphjs/
MIT License
293 stars 46 forks source link

typescript error on example in the docs #168

Open willredington opened 1 month ago

willredington commented 1 month ago

This example is throwing an error in typescript and its right from the docs

const schema = {
  input: {
    value: null,
  },
  agentOutcome: {
    value: null,
  },
  steps: {
    value: (x: Array<BaseMessage>, y: Array<BaseMessage>) => x.concat(y),
    default: () => [],
  },
};

const graph = new StateGraph({
  channels: schema, // incorrect type here
});

Using the following versions:

{
    "@langchain/core": "^0.2.0",
    "@langchain/langgraph": "^0.0.17",
    "@langchain/openai": "^0.0.32"
}

The error is:

Property 'root' is missing in type '{ input: { value: null; }; agentOutcome: { value: null; }; steps: { value: (x: Array, y: Array) => BaseMessage[]; default: () => never[]; }; }' but required in type 'ChannelReducers<{ root: unknown; }>'.

hinthornw commented 1 month ago

ya good catch - I did it all in deno at first (so they all do run there) but will have to review and clean up to get the types all correct

willredington commented 1 month ago

Thanks for the response, not critical just getting on the docket

hinthornw commented 1 month ago

Pushed an update - reran all of them with TS on langgraph 0.0.18 - hopefully resolves things for ya

bharathvaj-ganesan commented 1 month ago

I'm still facing this issue on 0.0.19 version.

hinthornw commented 1 month ago

For all of the docs? What does your tsconfig look like?

The change wasn't something for the actual StateGraph code, it was just fixing the docs FYI

claritise commented 1 month ago

Also getting the same error, here is my tsconfig, it's the standard T3 starter

{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "checkJs": true,

    /* Bundled projects */
    "lib": ["dom", "dom.iterable", "ES2022"],
    "noEmit": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "jsx": "preserve",
    "plugins": [{ "name": "next" }],
    "incremental": true,

    /* Path Aliases */
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    }
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.js",
    ".next/types/**/*.ts"
  ],
  "exclude": ["node_modules"]
}
KeenanFernandes2000 commented 1 month ago

I'm still facing this issue on 0.0.19 version.

Idk if this is me only but most of the guides on the LangGraph documentation don't work with version 0.0.19.

SilverLaius commented 1 month ago

FYI all the examples are broken and require a root reducer if you don't define a type since 0.0.13. Docs examples work with 0.0.12 and below since they do not add the type to StateGraph. This is what broke it: https://github.com/langchain-ai/langgraphjs/commit/6866c22db61987ced4db19bf1c0b3e047cefceb6 @nfcampos ...Or well it was broken already before but this just made it mandatory pretty much to add the type to the StateGraph or it will require you to define some root value that is not documented anywhere. Previously it was extending Record<string, any> and you could get away with not defining it and doing whatever you wanted.

I guess you can still get away with adding the "untyped" Record as the state: const graph = new StateGraph<Record<string, any>>({ channels: state}) But that is probably not what you want to have.

KeenanFernandes2000 commented 1 month ago

FYI all the examples are broken and require a root reducer if you don't define a type since 0.0.13. Docs examples work with 0.0.12 and below since they do not add the type to StateGraph. This is what broke it: 6866c22 @nfcampos ...Or well it was broken already before but this just made it mandatory pretty much to add the type to the StateGraph or it will require you to define some root value that is not documented anywhere. Previously it was extending Record<string, any> and you could get away with not defining it and doing whatever you wanted.

I guess you can still get away with adding the "untyped" Record as the state: const graph = new StateGraph<Record<string, any>>({ channels: state}) But that is probably not what you want to have.

Thanks for this, I actually had stuff running on version 0.0.12 but I couldn't get the checkpointer to work with the memory saver(which works in 0.0.13 and above from my testing). In short I wanted to convert one of my workflows to be session based as it would break if multiple executions were run. I guess ill have to wait till there's a proper solution.

Orthdron commented 1 month ago

I agree with the previous comments. It's quite challenging to develop with outdated documentation, especially when the core examples don't work. I understand this is an open-source project, but any help in updating the documentation would be greatly appreciated. Thank you!

haisapan commented 1 month ago

yes the documentation is outdated, i tried to add root or use reducer but still got lots of error... I dare not use it as big break changes even have no documents..

image
FelixGirard commented 1 month ago

I'm new to Langchain and had some trouble figuring out why it wasn't working. The documentation is really important. From the commit and the tests I was able to make it work, I still don't know what type to put instead of any for the steps.



type StateSchema = {
  input: string | null;
  agentOutcome: string | null;
  steps: Array<string>;
};

const schema = {
  input: null,
  agentOutcome: null,
  steps: {
    value: (x: any, y: any) => x.concat(y),
    default: () => [],
  },
};

const graph = new StateGraph<StateSchema>({
  channels: schema, 
});
KeenanFernandes2000 commented 1 month ago

I'm new to Langchain and had some trouble figuring out why it wasn't working. The documentation is really important. From the commit and the tests I was able to make it work, I still don't know what type to put instead of any for the steps.



type StateSchema = {
  input: string | null;
  agentOutcome: string | null;
  steps: Array<string>;
};

const schema = {
  input: null,
  agentOutcome: null,
  steps: {
    value: (x: any, y: any) => x.concat(y),
    default: () => [],
  },
};

const graph = new StateGraph<StateSchema>({
  channels: schema, 
});

By any chance did you happen to work with memory saver and check-pointers?

FelixGirard commented 1 month ago

I'm new to Langchain and had some trouble figuring out why it wasn't working. The documentation is really important. From the commit and the tests I was able to make it work, I still don't know what type to put instead of any for the steps.

type StateSchema = {
  input: string | null;
  agentOutcome: string | null;
  steps: Array<string>;
};

const schema = {
  input: null,
  agentOutcome: null,
  steps: {
    value: (x: any, y: any) => x.concat(y),
    default: () => [],
  },
};

const graph = new StateGraph<StateSchema>({
  channels: schema, 
});

By any chance did you happen to work with memory saver and check-pointers?

I haven't no, any specific error you are getting on that guide?

hinthornw commented 1 month ago

Thanks for flagging this, everyone, and thanks for your patience. As a JS noob I suppose it was a bad idea to just write the docs in JS rather than typescript - clearly typing is more commonly enforced here than in python

I went through all the tutorials in the docs, all the how-tos, and the README and rewrote them, compiled with TSC and ran in typescript with no complaints. Hopefully this resolves this issue. Will keep this issue open another day or two to let others push back since obviously I may have missed some things, but hopefully it will be less painful for everyone writing typescript to get started moving forward.

Big thank you to all on this thread and elsewhere for raising this issue and for bearing with us!

vanb commented 3 weeks ago

Thanks for working on the docs @hinthornw. I'm still receiving a type error even with the "quick start" example.

Argument of type '"agent"' is not assignable to parameter of type '"__end__" | "__start__"'.

image

I also received an error when adding the tools node, but fixed that by typing it as new ToolNode<BaseMessage[]>(tools)

hinthornw commented 3 weeks ago

🙈 - rewrote. Thanks for reporting the issue vanb!