langchain-ai/langchain · error · ValueError
Did not find {key}, please add an environment variable `{key
Error message
Did not find {key}, please add an environment variable `{key}` which contains it, or pass `{key}` as a named parameter. What it means
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.
Source
Thrown at libs/core/langchain_core/utils/utils.py:419
The value from the environment.
"""
if isinstance(key, (list, tuple)):
for k in key:
if k in os.environ:
return os.environ[k]
if isinstance(key, str) and key in os.environ:
return os.environ[key]
if isinstance(default, (str, type(None))):
return default
if error_message:
raise ValueError(error_message)
msg = (
f"Did not find {key}, please add an environment variable"
f" `{key}` which contains it, or pass"
f" `{key}` as a named parameter."
)
raise ValueError(msg)
return get_from_env_fn
@overload
def secret_from_env(key: str | Sequence[str], /) -> Callable[[], SecretStr]: ...
@overload
def secret_from_env(key: str, /, *, default: str) -> Callable[[], SecretStr]: ...
@overload
def secret_from_env(
key: str | Sequence[str], /, *, default: None
) -> Callable[[], SecretStr | None]: ...
View on GitHub (pinned to e32fa9a52e)
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.
Example fix
# before from langchain_community.chat_models import ChatHuggingFace model = ChatHuggingFace() # ValueError: Did not find HUGGINGFACEHUB_API_TOKEN ... # after import os os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_..." model = ChatHuggingFace()
Defensive patterns
Strategy: validation
Validate before calling
import os
def require_env(keys: list[str]) -> None:
missing = [k for k in keys if k not in os.environ]
if missing:
raise SystemExit(
f"Missing environment variables: {missing}. "
"Set them or pass the corresponding named parameter."
)
require_env(["HUGGINGFACEHUB_API_TOKEN"]) Try / catch
try:
llm = build_llm()
except ValueError as e:
if "Did not find" in str(e) and "environment variable" in str(e):
raise SystemExit(f"Configuration incomplete: {e}") from e
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- {error_message}
- maxsize must be greater than 0
- Tracing using LangChainTracerV1 is no longer supported. Plea
- Could not resolve content_key {full_path!r}: expected a mapp
- Could not resolve content_key {full_path!r}: missing key {ke
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/63ce91e21ad8d7d4.
Report an issue: GitHub.