core[minor]: Support multiple keys in get_from_dict_or_env (#23086)

Support passing multiple keys for ge_from_dict_or_env
pull/23132/head
Eugene Yurtsev 4 months ago committed by GitHub
parent 226802f0c4
commit aa6415aa7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import os import os
from typing import Any, Dict, Optional from typing import Any, Dict, List, Optional, Union
def env_var_is_set(env_var: str) -> bool: def env_var_is_set(env_var: str) -> bool:
@ -22,13 +22,37 @@ def env_var_is_set(env_var: str) -> bool:
def get_from_dict_or_env( def get_from_dict_or_env(
data: Dict[str, Any], key: str, env_key: str, default: Optional[str] = None data: Dict[str, Any],
key: Union[str, List[str]],
env_key: str,
default: Optional[str] = None,
) -> str: ) -> str:
"""Get a value from a dictionary or an environment variable.""" """Get a value from a dictionary or an environment variable.
if key in data and data[key]:
return data[key] 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.
"""
if isinstance(key, (list, tuple)):
for k in key:
if k in data and data[k]:
return data[k]
if isinstance(key, str):
if key in data and data[key]:
return data[key]
if isinstance(key, (list, tuple)):
key_for_err = key[0]
else: else:
return get_from_env(key, env_key, default=default) key_for_err = key
return get_from_env(key_for_err, env_key, default=default)
def get_from_env(key: str, env_key: str, default: Optional[str] = None) -> str: def get_from_env(key: str, env_key: str, default: Optional[str] = None) -> str:

@ -0,0 +1,64 @@
import pytest
from langchain_core.utils.env import get_from_dict_or_env
def test_get_from_dict_or_env() -> None:
assert (
get_from_dict_or_env(
{
"a": "foo",
},
["a"],
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
["b", "a"],
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"a",
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"not exists",
"__SOME_KEY_IN_ENV",
default="default",
)
== "default"
)
# Not the most obvious behavior, but
# this is how it works right now
with pytest.raises(ValueError):
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"not exists",
"__SOME_KEY_IN_ENV",
)
is None
)
Loading…
Cancel
Save