{"record":{"id":"63ce91e21ad8d7d4","repo":"langchain-ai/langchain","slug":"did-not-find-key-please-add-an-environment-vari-63ce91","errorCode":null,"errorMessage":"Did not find {key}, please add an environment variable `{key}` which contains it, or pass `{key}` as a named parameter.","messagePattern":"Did not find (.+?), please add an environment variable `(.+?)` which contains it, or pass `(.+?)` as a named parameter\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/utils.py","lineNumber":419,"sourceCode":"            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\n@overload\ndef secret_from_env(\n    key: str | Sequence[str], /, *, default: None\n) -> Callable[[], SecretStr | None]: ...\n\n","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/utils.py#L401-L437","documentation":"Default error from the `from_env` lookup closure when the environment variable is absent, no default was provided, and no custom `error_message` was set. The message names the exact variable LangChain expected and tells you the two remedies: set the env var or pass the value as a named parameter.","triggerScenarios":"Constructing an integration whose field default uses `from_env('SOME_KEY')` (no default, no custom message) while `SOME_KEY` is not in `os.environ`; passing a `list`/`tuple` of alternative keys where none are set also lands here.","commonSituations":"Deploying to a new environment (staging/prod/container) without propagating secrets; `.env` not loaded before import-time instantiation; shell vs subprocess env differences; variable renamed but code not updated.","solutions":["Set the named environment variable: `export <KEY>=<value>` or add `<KEY>=...` to `.env` and ensure it is loaded before construction.","Pass the parameter explicitly to the constructor: `ChatProvider(api_key=...)`.","Verify with `python -c \"import os; print(os.environ.get('<KEY>'))\"` that the variable is actually visible to the process."],"exampleFix":"# before\nfrom langchain_community.chat_models import ChatHuggingFace\nmodel = ChatHuggingFace()  # ValueError: Did not find HUGGINGFACEHUB_API_TOKEN ...\n\n# after\nimport os\nos.environ[\"HUGGINGFACEHUB_API_TOKEN\"] = \"hf_...\"\nmodel = ChatHuggingFace()","handlingStrategy":"validation","validationCode":"import os\n\ndef require_env(keys: list[str]) -> None:\n    missing = [k for k in keys if k not in os.environ]\n    if missing:\n        raise SystemExit(\n            f\"Missing environment variables: {missing}. \"\n            \"Set them or pass the corresponding named parameter.\"\n        )\n\nrequire_env([\"HUGGINGFACEHUB_API_TOKEN\"])","typeGuard":null,"tryCatchPattern":"try:\n    llm = build_llm()\nexcept ValueError as e:\n    if \"Did not find\" in str(e) and \"environment variable\" in str(e):\n        raise SystemExit(f\"Configuration incomplete: {e}\") from e\n    raise","preventionTips":["Verify with `os.environ.get(KEY)` in a preflight check before building clients.","In Docker/CI, assert required secrets exist in the entrypoint so failures carry deployment context.","Pass values explicitly in tests instead of relying on ambient env state."],"tags":["environment","secrets","configuration"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}