From 62b6965d2ac2489b883def790ac4a08ab447a7d0 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Thu, 18 Jul 2024 15:28:52 -0700 Subject: [PATCH] core: In ensure_config don't copy dunder configurable keys to metadata (#24420) --- libs/core/langchain_core/runnables/config.py | 6 +++++- libs/core/tests/unit_tests/runnables/test_runnable.py | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libs/core/langchain_core/runnables/config.py b/libs/core/langchain_core/runnables/config.py index a0e03703762..4485237799a 100644 --- a/libs/core/langchain_core/runnables/config.py +++ b/libs/core/langchain_core/runnables/config.py @@ -154,7 +154,11 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig: cast(RunnableConfig, {k: v for k, v in config.items() if v is not None}) ) for key, value in empty.get("configurable", {}).items(): - if isinstance(value, (str, int, float, bool)) and key not in empty["metadata"]: + if ( + not key.startswith("__") + and isinstance(value, (str, int, float, bool)) + and key not in empty["metadata"] + ): empty["metadata"][key] = value return empty diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py index 969615f9bed..162f67cdad9 100644 --- a/libs/core/tests/unit_tests/runnables/test_runnable.py +++ b/libs/core/tests/unit_tests/runnables/test_runnable.py @@ -1271,7 +1271,11 @@ async def test_with_config_metadata_passthrough(mocker: MockerFixture) -> None: assert ( fakew.with_config(tags=["a-tag"]).invoke( - "hello", {"configurable": {"hello": "there"}, "metadata": {"bye": "now"}} + "hello", + { + "configurable": {"hello": "there", "__secret_key": "nahnah"}, + "metadata": {"bye": "now"}, + }, ) == 5 ) @@ -1281,7 +1285,7 @@ async def test_with_config_metadata_passthrough(mocker: MockerFixture) -> None: tags=["a-tag"], callbacks=None, recursion_limit=25, - configurable={"hello": "there"}, + configurable={"hello": "there", "__secret_key": "nahnah"}, metadata={"hello": "there", "bye": "now"}, ), )