Skip to content

The agent engineering platform.

core[patch]: Accept configurable keys top-level (#23806)

Nuno Campos947628311bfa
diff --git a/libs/core/langchain_core/runnables/config.py b/libs/core/langchain_core/runnables/config.py
index 4485237799..33174270a7 100644
--- a/libs/core/langchain_core/runnables/config.py
+++ b/libs/core/langchain_core/runnables/config.py
@@ -105,6 +105,20 @@ class RunnableConfig(TypedDict, total=False):
"""
+CONFIG_KEYS = [
+ "tags",
+ "metadata",
+ "callbacks",
+ "run_name",
+ "max_concurrency",
+ "recursion_limit",
+ "configurable",
+ "run_id",
+]
+
+DEFAULT_RECURSION_LIMIT = 25
+
+
var_child_runnable_config = ContextVar(
"child_runnable_config", default=RunnableConfig()
)
@@ -143,16 +157,20 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig:
tags=[],
metadata={},
callbacks=None,
- recursion_limit=25,
+ recursion_limit=DEFAULT_RECURSION_LIMIT,
+ configurable={},
)
if var_config := var_child_runnable_config.get():
empty.update(
cast(RunnableConfig, {k: v for k, v in var_config.items() if v is not None})
)
if config is not None:
- empty.update(
- cast(RunnableConfig, {k: v for k, v in config.items() if v is not None})
- )
+ for k, v in config.items():
+ if v is not None:
+ if k in CONFIG_KEYS:
+ empty[k] = v # type: ignore[literal-required]
+ else:
+ empty["configurable"][k] = v
for key, value in empty.get("configurable", {}).items():
if (
not key.startswith("__")
@@ -265,7 +283,7 @@ def merge_configs(*configs: Optional[RunnableConfig]) -> RunnableConfig:
base: RunnableConfig = {}
# Even though the keys aren't literals, this is correct
# because both dicts are the same type
- for config in (c for c in configs if c is not None):
+ for config in (ensure_config(c) for c in configs if c is not None):
for key in config:
if key == "metadata":
base[key] = { # type: ignore
@@ -340,6 +358,9 @@ def merge_configs(*configs: Optional[RunnableConfig]) -> RunnableConfig:
manager.add_handler(handler, inherit=True)
base["callbacks"] = manager
+ elif key == "recursion_limit":
+ if config["recursion_limit"] != DEFAULT_RECURSION_LIMIT:
+ base["recursion_limit"] = config["recursion_limit"]
else:
base[key] = config[key] or base.get(key) # type: ignore
return base
diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
index be95e6d972..144c4118d9 100644
--- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
+++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
@@ -11972,7 +11972,8 @@
"FakeListChatModel"
],
"name": "FakeListChatModel"
- }
+ },
+ "metadata": {}
},
{
"id": 2,
@@ -12078,7 +12079,8 @@
"FakeListChatModel"
],
"name": "FakeListChatModel"
- }
+ },
+ "metadata": {}
},
{
"id": 3,
@@ -12191,7 +12193,8 @@
"FakeListChatModel"
],
"name": "FakeListChatModel"
- }
+ },
+ "metadata": {}
},
{
"id": 6,
diff --git a/libs/core/tests/unit_tests/runnables/test_configurable.py b/libs/core/tests/unit_tests/runnables/test_configurable.py
index 7071249461..cbd249b6bb 100644
--- a/libs/core/tests/unit_tests/runnables/test_configurable.py
+++ b/libs/core/tests/unit_tests/runnables/test_configurable.py
@@ -69,12 +69,7 @@ def test_doubly_set_configurable() -> None:
)
)
- assert (
- configurable_runnable.invoke(
- "d", config=RunnableConfig(configurable={"my_property": "c"})
- )
- == "dc"
- )
+ assert configurable_runnable.invoke("d", config={"my_property": "c"}) == "dc" # type: ignore[arg-type]
def test_alias_set_configurable() -> None:
diff --git a/libs/core/tests/unit_tests/runnables/test_history.py b/libs/core/tests/unit_tests/runnables/test_history.py
index 69c35524df..b80390533d 100644
--- a/libs/core/tests/unit_tests/runnables/test_history.py
+++ b/libs/core/tests/unit_tests/runnables/test_history.py
@@ -72,10 +72,10 @@ async def test_input_messages_async() -> None:
store: Dict = {}
get_session_history = _get_get_session_history(store=store)
with_history = RunnableWithMessageHistory(runnable, get_session_history)
- config: RunnableConfig = {"configurable": {"session_id": "1_async"}}
- output = await with_history.ainvoke([HumanMessage(content="hello")], config)
+ config = {"session_id": "1_async"}
+ output = await with_history.ainvoke([HumanMessage(content="hello")], config) # type: ignore[arg-type]
assert output == "you said: hello"
- output = await with_history.ainvoke([HumanMessage(content="good bye")], config)
+ output = await with_history.ainvoke([HumanMessage(content="good bye")], config) # type: ignore[arg-type]
assert output == "you said: hello\ngood bye"
assert store == {
"1_async": InMemoryChatMessageHistory(
diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py
index 162f67cdad..bfbd3d19d3 100644
--- a/libs/core/tests/unit_tests/runnables/test_runnable.py
+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py
@@ -1298,7 +1298,10 @@ async def test_with_config(mocker: MockerFixture) -> None:
assert fake.with_config(tags=["a-tag"]).invoke("hello") == 5
assert spy.call_args_list == [
- mocker.call("hello", dict(tags=["a-tag"])),
+ mocker.call(
+ "hello",
+ dict(tags=["a-tag"], metadata={}, configurable={}),
+ ),
]
spy.reset_mock()
@@ -1327,7 +1330,10 @@ async def test_with_config(mocker: MockerFixture) -> None:
)
] == [5]
assert spy.call_args_list == [
- mocker.call("hello", dict(tags=["a-tag"], metadata={"key": "value"})),
+ mocker.call(
+ "hello",
+ dict(tags=["a-tag"], metadata={"key": "value"}, configurable={}),
+ ),
]
spy.reset_mock()
@@ -1402,7 +1408,10 @@ async def test_with_config(mocker: MockerFixture) -> None:
== 5
)
assert spy.call_args_list == [
- mocker.call("hello", dict(callbacks=[handler], metadata={"a": "b"})),
+ mocker.call(
+ "hello",
+ dict(callbacks=[handler], metadata={"a": "b"}, configurable={}, tags=[]),
+ ),
]
spy.reset_mock()
@@ -1410,7 +1419,7 @@ async def test_with_config(mocker: MockerFixture) -> None:
part async for part in fake.with_config(metadata={"a": "b"}).astream("hello")
] == [5]
assert spy.call_args_list == [
- mocker.call("hello", dict(metadata={"a": "b"})),
+ mocker.call("hello", dict(metadata={"a": "b"}, tags=[], configurable={})),
]
spy.reset_mock()
@@ -1428,6 +1437,7 @@ async def test_with_config(mocker: MockerFixture) -> None:
tags=["c"],
callbacks=None,
recursion_limit=5,
+ configurable={},
),
),
mocker.call(
@@ -1437,6 +1447,7 @@ async def test_with_config(mocker: MockerFixture) -> None:
tags=["c"],
callbacks=None,
recursion_limit=5,
+ configurable={},
),
),
]
@@ -1462,6 +1473,7 @@ async def test_with_config(mocker: MockerFixture) -> None:
tags=["c"],
callbacks=None,
recursion_limit=5,
+ configurable={},
),
)
second_call = next(call for call in spy.call_args_list if call.args[0] == "wooorld")
@@ -1472,6 +1484,7 @@ async def test_with_config(mocker: MockerFixture) -> None:
tags=["c"],
callbacks=None,
recursion_limit=5,
+ configurable={},
),
)
@@ -1542,6 +1555,7 @@ async def test_default_method_implementations(mocker: MockerFixture) -> None:
tags=[],
callbacks=None,
recursion_limit=25,
+ configurable={},
)