LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/corecontextgetContextVariable
Functionā—Since v1.0

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.

Copy
getContextVariable<T = any>(name: PropertyKey): T | undefined

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

Parameters

NameTypeDescription
name*PropertyKey

The name of the context variable.

Example

Copy
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({});
View source on GitHub