Skip to content

Commit

Permalink
core[minor]: Add support for multiple env keys for secrets_from_env (#…
Browse files Browse the repository at this point in the history
…25971)

- Add support to look up secret using more than one env variable
- Add overload to help mypy

Needed for #25491
  • Loading branch information
eyurtsev authored Sep 3, 2024
1 parent fdeaff4 commit fa8402e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions libs/core/langchain_core/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def from_env(


@overload
def from_env(key: str, /, *, default: None) -> Callable[[], Optional[str]]: ...
def from_env(
key: Union[str, Sequence[str]], /, *, default: None
) -> Callable[[], Optional[str]]: ...


def from_env(
Expand Down Expand Up @@ -360,7 +362,7 @@ def secret_from_env(key: str, /, *, default: str) -> Callable[[], SecretStr]: ..

@overload
def secret_from_env(
key: str, /, *, default: None
key: Union[str, Sequence[str]], /, *, default: None
) -> Callable[[], Optional[SecretStr]]: ...


Expand All @@ -369,7 +371,7 @@ def secret_from_env(key: str, /, *, error_message: str) -> Callable[[], SecretSt


def secret_from_env(
key: str,
key: Union[str, Sequence[str]],
/,
*,
default: Union[str, _NoDefaultType, None] = _NoDefault,
Expand All @@ -390,9 +392,14 @@ def secret_from_env(

def get_secret_from_env() -> Optional[SecretStr]:
"""Get a value from an environment variable."""
if key in os.environ:
return SecretStr(os.environ[key])
elif isinstance(default, str):
if isinstance(key, (list, tuple)):
for k in key:
if k in os.environ:
return SecretStr(os.environ[k])
if isinstance(key, str):
if key in os.environ:
return SecretStr(os.environ[key])
if isinstance(default, str):
return SecretStr(default)
elif isinstance(default, type(None)):
return None
Expand Down

0 comments on commit fa8402e

Please sign in to comment.