env.py — langchain Source File
Architecture documentation for env.py, a python file in the langchain codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 359da5b8_85a8_468a_12ae_f45ea1dc33e4["env.py"] 0029f612_c503_ebcf_a452_a0fae8c9f2c3["os"] 359da5b8_85a8_468a_12ae_f45ea1dc33e4 --> 0029f612_c503_ebcf_a452_a0fae8c9f2c3 feec1ec4_6917_867b_d228_b134d0ff8099["typing"] 359da5b8_85a8_468a_12ae_f45ea1dc33e4 --> feec1ec4_6917_867b_d228_b134d0ff8099 style 359da5b8_85a8_468a_12ae_f45ea1dc33e4 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Utilities for environment variables."""
from __future__ import annotations
import os
from typing import Any
def env_var_is_set(env_var: str) -> bool:
"""Check if an environment variable is set.
Args:
env_var: The name of the environment variable.
Returns:
`True` if the environment variable is set, `False` otherwise.
"""
return env_var in os.environ and os.environ[env_var] not in {
"",
"0",
"false",
"False",
}
def get_from_dict_or_env(
data: dict[str, Any],
key: str | list[str],
env_key: str,
default: str | None = None,
) -> str:
"""Get a value from a dictionary or an environment variable.
Args:
data: The dictionary to look up the key in.
key: The key to look up in the dictionary.
This can be a list of keys to try in order.
env_key: The environment variable to look up if the key is not
in the dictionary.
default: The default value to return if the key is not in the dictionary
or the environment.
Returns:
The dict value or the environment variable value.
"""
if isinstance(key, (list, tuple)):
for k in key:
if value := data.get(k):
return str(value)
if isinstance(key, str) and key in data and data[key]:
return str(data[key])
key_for_err = key[0] if isinstance(key, (list, tuple)) else key
return get_from_env(key_for_err, env_key, default=default)
def get_from_env(key: str, env_key: str, default: str | None = None) -> str:
"""Get a value from a dictionary or an environment variable.
Args:
key: The key to look up in the dictionary.
env_key: The environment variable to look up if the key is not
in the dictionary.
default: The default value to return if the key is not in the dictionary
or the environment.
Returns:
The value of the key.
Raises:
ValueError: If the key is not in the dictionary and no default value is
provided or if the environment variable is not set.
"""
if env_value := os.getenv(env_key):
return env_value
if default is not None:
return default
msg = (
f"Did not find {key}, please add an environment variable"
f" `{env_key}` which contains it, or pass"
f" `{key}` as a named parameter."
)
raise ValueError(msg)
Domain
Subdomains
Dependencies
- os
- typing
Source
Frequently Asked Questions
What does env.py do?
env.py is a source file in the langchain codebase, written in python. It belongs to the Observability domain, CallbackManager subdomain.
What functions are defined in env.py?
env.py defines 3 function(s): env_var_is_set, get_from_dict_or_env, get_from_env.
What does env.py depend on?
env.py imports 2 module(s): os, typing.
Where is env.py in the architecture?
env.py is located at libs/core/langchain_core/utils/env.py (domain: Observability, subdomain: CallbackManager, directory: libs/core/langchain_core/utils).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free