Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load: Optionally disable reading secrets from env #19596

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions libs/core/langchain_core/load/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def __init__(
self,
secrets_map: Optional[Dict[str, str]] = None,
valid_namespaces: Optional[List[str]] = None,
secrets_from_env: bool = True,
) -> None:
self.secrets_from_env = secrets_from_env
self.secrets_map = secrets_map or dict()
# By default only support langchain, but user can pass in additional namespaces
self.valid_namespaces = (
Expand All @@ -48,7 +50,7 @@ def __call__(self, value: Dict[str, Any]) -> Any:
if key in self.secrets_map:
return self.secrets_map[key]
else:
if key in os.environ and os.environ[key]:
if self.secrets_from_env and key in os.environ and os.environ[key]:
return os.environ[key]
raise KeyError(f'Missing key "{key}" in load(secrets_map)')

Expand Down Expand Up @@ -116,6 +118,7 @@ def loads(
*,
secrets_map: Optional[Dict[str, str]] = None,
valid_namespaces: Optional[List[str]] = None,
secrets_from_env: bool = True,
) -> Any:
"""Revive a LangChain class from a JSON string.
Equivalent to `load(json.loads(text))`.
Expand All @@ -129,7 +132,9 @@ def loads(
Returns:
Revived LangChain objects.
"""
return json.loads(text, object_hook=Reviver(secrets_map, valid_namespaces))
return json.loads(
text, object_hook=Reviver(secrets_map, valid_namespaces, secrets_from_env)
)


@beta()
Expand All @@ -138,6 +143,7 @@ def load(
*,
secrets_map: Optional[Dict[str, str]] = None,
valid_namespaces: Optional[List[str]] = None,
secrets_from_env: bool = True,
) -> Any:
"""Revive a LangChain class from a JSON object. Use this if you already
have a parsed JSON object, eg. from `json.load` or `orjson.loads`.
Expand All @@ -151,7 +157,7 @@ def load(
Returns:
Revived LangChain objects.
"""
reviver = Reviver(secrets_map, valid_namespaces)
reviver = Reviver(secrets_map, valid_namespaces, secrets_from_env)

def _load(obj: Any) -> Any:
if isinstance(obj, dict):
Expand Down
Loading