Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setContextVariable tries setting undefined runTree when used with createReactAgent #7680

Open
5 tasks done
netzhuffle opened this issue Feb 10, 2025 · 1 comment
Open
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@netzhuffle
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { getContextVariable, setContextVariable } from "@langchain/core/context";
import { RunnableLambda } from "@langchain/core/runnables";
import { tool } from "@langchain/core/tools";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";

const runner = RunnableLambda.from(async () => {
  setContextVariable('userid', 1);
  const agent = createReactAgent({
    llm: new ChatOpenAI({
      model: "gpt-4o-mini",
    }),
    tools: [
      tool(async () => {
        return getContextVariable('userid');
      }, {
        name: "userid",
        description: "Returns the user's id",
      }),
    ],
  });
  return await agent.invoke({
    messages: [
      {
        role: "user",
        content: "What is my user id?",
      },
    ],
  });
});

await runner.invoke({});

Error Message and Stack Trace (if applicable)

/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:50
runTree[_CONTEXT_VARIABLES_KEY] =
^

TypeError: Cannot set properties of undefined (setting 'Symbol(lc:context_variables)')
at AsyncLocalStorageProvider.runWithConfig (/node_modules/@langchain/core/dist/singletons/async_local_storage/index.js:50:45)
at /node_modules/@langchain/core/dist/utils/stream.js:218:53
at new Promise ()
at new AsyncGeneratorWithSetup (/node_modules/@langchain/core/dist/utils/stream.js:217:22)
at CompiledStateGraph.stream (/node_modules/@langchain/core/dist/runnables/base.js:170:34)
at CompiledStateGraph.stream (/node_modules/@langchain/langgraph/dist/pregel/index.js:778:22)
at CompiledStateGraph.invoke (/node_modules/@langchain/langgraph/dist/pregel/index.js:992:35)
at RunnableLambda.func (/src/test.ts:21:24)
at /node_modules/@langchain/core/dist/runnables/base.js:1677:45
at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)

Node.js v22.13.1

Description

Looking at the async_local_storage/index.ts implementation, runTree does not always get initialized and there is no check for undefined before the Symbol(lc:context_variables) key is tried to be set.

Passing a runtime value is both documented this way in the LangChain documentation as well as referenced in the LangGraph documentation.

Based on my understanding of the documentation, I thus would expect this code to run on Node.js v22. It might of course be possible that there is an issue with either LangGraph, the documentation, or my example code above, but going through the stack trace and seeing no checks before setting (to either handle the case or inform the user that they did something wrong) currently lets me assume a bug or at least an improvement opportunity in LangChain.

System Info

Linux
Node.js v22.13.1
@langchain/community v0.3.29
@langchain/core v0.3.39
@langchain/langgraph v0.2.45
@langchain/openai v0.4.3
langchain v0.3.15

Copy link

dosubot bot commented Feb 10, 2025

Hello, @netzhuffle! I'm here to assist you with any bugs, questions, or contributions you might have. Let's tackle this issue together!

The "Cannot set properties of undefined" error you're encountering is likely due to the asyncLocalStorageInstance not being initialized. The setContextVariable function relies on an AsyncLocalStorage instance to store context variables, and if this instance is not properly initialized, it will result in an error when attempting to set a context variable.

To resolve this issue, ensure that the AsyncLocalStorage instance is properly initialized before calling setContextVariable. This involves calling the initializeGlobalInstance method of the AsyncLocalStorageProviderSingleton with a new instance of AsyncLocalStorage:

AsyncLocalStorageProviderSingleton.initializeGlobalInstance(
  new AsyncLocalStorage()
);

Make sure this initialization is done early in your application setup, before any context variables are set or accessed [1][2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant