fix(core): Ignore missing secrets on deserialization (#30252)

This commit is contained in:
Jacob Lee 2025-03-13 12:27:03 -07:00 committed by GitHub
parent ebea5e014d
commit e9c1765967
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -98,8 +98,7 @@ class Reviver:
else:
if self.secrets_from_env and key in os.environ and os.environ[key]:
return os.environ[key]
msg = f'Missing key "{key}" in load(secrets_map)'
raise KeyError(msg)
return None
if (
value.get("lc") == 1

View File

@ -167,3 +167,13 @@ def test_load_llmchain_with_non_serializable_arg() -> None:
chain_obj = dumpd(chain)
with pytest.raises(NotImplementedError):
load(chain_obj, secrets_map={"OPENAI_API_KEY": "hello"})
@pytest.mark.requires("openai", "langchain_openai")
def test_loads_with_missing_secrets() -> None:
import openai
llm_string = '{"lc": 1, "type": "constructor", "id": ["langchain", "llms", "openai", "OpenAI"], "kwargs": {"model_name": "davinci", "temperature": 0.5, "max_tokens": 256, "top_p": 0.8, "n": 1, "best_of": 1, "openai_api_key": {"lc": 1, "type": "secret", "id": ["OPENAI_API_KEY"]}, "batch_size": 20, "max_retries": 2, "disallowed_special": "all"}, "name": "OpenAI"}' # noqa: E501
# Should throw on instantiation, not deserialization
with pytest.raises(openai.OpenAIError):
loads(llm_string)