From e2554c9616571d5548b1689abadc4cd81b6d8a37 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Wed, 15 Jan 2025 11:54:31 -0500 Subject: [PATCH] langgraph: fix non-empty value check in ensure_config (#3039) Fixes https://github.com/langchain-ai/langgraph/issues/2890 --- libs/langgraph/langgraph/utils/config.py | 13 ++++++++++--- libs/langgraph/tests/test_utils.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/libs/langgraph/langgraph/utils/config.py b/libs/langgraph/langgraph/utils/config.py index 5bff9e848..0712b55cf 100644 --- a/libs/langgraph/langgraph/utils/config.py +++ b/libs/langgraph/langgraph/utils/config.py @@ -250,6 +250,13 @@ def get_async_callback_manager_for_config( ) +def _is_not_empty(value: Any) -> bool: + if isinstance(value, (list, tuple, dict)): + return len(value) > 0 + else: + return value is not None + + def ensure_config(*configs: Optional[RunnableConfig]) -> RunnableConfig: """Ensure that a config is a dict with all keys present. @@ -272,20 +279,20 @@ def ensure_config(*configs: Optional[RunnableConfig]) -> RunnableConfig: { k: v.copy() if k in COPIABLE_KEYS else v # type: ignore[attr-defined] for k, v in var_config.items() - if v is not None + if _is_not_empty(v) }, ) for config in configs: if config is None: continue for k, v in config.items(): - if v is not None and k in CONFIG_KEYS: + if _is_not_empty(v) and k in CONFIG_KEYS: if k == CONF: empty[k] = cast(dict, v).copy() else: empty[k] = v # type: ignore[literal-required] for k, v in config.items(): - if v is not None and k not in CONFIG_KEYS: + if _is_not_empty(v) and k not in CONFIG_KEYS: empty[CONF][k] = v for key, value in empty[CONF].items(): if ( diff --git a/libs/langgraph/tests/test_utils.py b/libs/langgraph/tests/test_utils.py index e77cbd3a2..8c9e304c9 100644 --- a/libs/langgraph/tests/test_utils.py +++ b/libs/langgraph/tests/test_utils.py @@ -20,6 +20,7 @@ from langgraph.graph import END, StateGraph from langgraph.graph.graph import CompiledGraph +from langgraph.utils.config import _is_not_empty from langgraph.utils.fields import ( _is_optional_type, get_enhanced_type_hints, @@ -284,3 +285,14 @@ class MyPydanticModelWithAnnotated(BaseModel): assert hints[0] == ("val_1", str, None, "A description") assert hints[1] == ("val_2", int, 42, None) assert hints[2] == ("val_3", str, "default", "Another description") + + +def test_is_not_empty() -> None: + assert _is_not_empty("foo") + assert _is_not_empty("") + assert _is_not_empty(1) + assert _is_not_empty(0) + assert not _is_not_empty(None) + assert not _is_not_empty([]) + assert not _is_not_empty(()) + assert not _is_not_empty({})