From c78eb55859c34db8ed81fe367f801d8a1d920555 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 26 Mar 2024 13:32:56 -0700 Subject: [PATCH] load: Optionally disable reading secrets from env (#19596) Thank you for contributing to LangChain! - [ ] **PR title**: "package: description" - Where "package" is whichever of langchain, community, core, experimental, etc. is being modified. Use "docs: ..." for purely docs changes, "templates: ..." for template changes, "infra: ..." for CI changes. - Example: "community: add foobar LLM" - [ ] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** a description of the change - **Issue:** the issue # it fixes, if applicable - **Dependencies:** any dependencies required for this change - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17. --- libs/core/langchain_core/load/load.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/core/langchain_core/load/load.py b/libs/core/langchain_core/load/load.py index 7ef2e561019..55710eb3e36 100644 --- a/libs/core/langchain_core/load/load.py +++ b/libs/core/langchain_core/load/load.py @@ -29,7 +29,9 @@ class Reviver: 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 = ( @@ -48,7 +50,7 @@ class Reviver: 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)') @@ -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))`. @@ -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() @@ -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`. @@ -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):