langchain.js
    Preparing search index...

    Function getContextVariable

    • Get the value of a previously set context variable. Context variables are scoped to any child runnables called by the current runnable, or globally if set outside of any runnable.

      Type Parameters

      • T = any

      Parameters

      • name: PropertyKey

        The name of the context variable.

      Returns undefined | T

      This function is only supported in environments that support AsyncLocalStorage, including Node.js, Deno, and Cloudflare Workers.

      import { RunnableLambda } from "@langchain/core/runnables";
      import {
      getContextVariable,
      setContextVariable
      } from "@langchain/core/context";

      const nested = RunnableLambda.from(() => {
      // "bar" because it was set by a parent
      console.log(getContextVariable("foo"));

      // Override to "baz", but only for child runnables
      setContextVariable("foo", "baz");

      // Now "baz", but only for child runnables
      return getContextVariable("foo");
      });

      const runnable = RunnableLambda.from(async () => {
      // Set a context variable named "foo"
      setContextVariable("foo", "bar");

      const res = await nested.invoke({});

      // Still "bar" since child changes do not affect parents
      console.log(getContextVariable("foo"));

      return res;
      });

      // undefined, because context variable has not been set yet
      console.log(getContextVariable("foo"));

      // Final return value is "baz"
      const result = await runnable.invoke({});