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({})