{"record":{"id":"3620874603915851","repo":"langchain-ai/langchain","slug":"error-message","errorCode":null,"errorMessage":"{error_message}","messagePattern":"\\{error_message\\}","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/utils.py","lineNumber":413,"sourceCode":"\n        Raises:\n            ValueError: If the environment variable is not set and no default is\n                provided.\n\n        Returns:\n            The value from the environment.\n        \"\"\"\n        if isinstance(key, (list, tuple)):\n            for k in key:\n                if k in os.environ:\n                    return os.environ[k]\n        if isinstance(key, str) and key in os.environ:\n            return os.environ[key]\n\n        if isinstance(default, (str, type(None))):\n            return default\n        if error_message:\n            raise ValueError(error_message)\n        msg = (\n            f\"Did not find {key}, please add an environment variable\"\n            f\" `{key}` which contains it, or pass\"\n            f\" `{key}` as a named parameter.\"\n        )\n        raise ValueError(msg)\n\n    return get_from_env_fn\n\n\n@overload\ndef secret_from_env(key: str | Sequence[str], /) -> Callable[[], SecretStr]: ...\n\n\n@overload\ndef secret_from_env(key: str, /, *, default: str) -> Callable[[], SecretStr]: ...\n\n","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/utils.py#L395-L431","documentation":"Raised inside the closure produced by `from_env(...)` when the requested environment variable is missing, no usable default exists, and the caller supplied a custom `error_message`. It is LangChain's mechanism for declaring a field whose value must come from the environment (common in integration `SecretStr`/string fields), with a tailored message explaining what to set.","triggerScenarios":"Instantiating an integration whose field default was built with `from_env('API_KEY', error_message='...')` while the environment variable is unset and no fallback default was configured; any direct use of `from_env(key)( )` without the variable present.","commonSituations":"Missing `.env` file or forgetting to load it (`load_dotenv()` not called); CI/containers where the secret was never injected; typo'd variable name; `.env` listed in `.gitignore` so fresh clones lack it.","solutions":["Set the environment variable named in the constructor's `from_env` declaration (the message text states which credential it needs), e.g. `export MY_API_KEY=...` or add it to `.env` and load it.","Pass the value directly as a constructor argument, which takes precedence over the env lookup.","For optional credentials, provide a default: use the `default=` overload or wrap instantiation in a check."],"exampleFix":"# before\nllm = ChatXAI()  # ValueError: <custom error_message> because XAI_API_KEY unset\n\n# after\nexport XAI_API_KEY=sk-...\nllm = ChatXAI()","handlingStrategy":"validation","validationCode":"import os\n\nREQUIRED_ENV = [\"MY_PROVIDER_API_KEY\"]\n\nmissing = [k for k in REQUIRED_ENV if not os.environ.get(k)]\nif missing:\n    raise SystemExit(f\"Missing env vars: {missing}. Set them or pass the value as a constructor argument.\")","typeGuard":null,"tryCatchPattern":"try:\n    client = MyIntegration()\nexcept ValueError as e:\n    if \"API key\" in str(e) or \"environment variable\" in str(e):\n        client = MyIntegration(api_key=get_secret_from_vault())  # fallback source\n    else:\n        raise","preventionTips":["Load `.env` at process start (`dotenv.load_dotenv()`) before importing integrations.","Fail fast at startup with an explicit env checklist instead of waiting for the constructor to fail.","Keep a documented list of every env var each integration consumes."],"tags":["environment","secrets","configuration"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}