Skip to content

Commit

Permalink
langgraph: fix non-empty value check in ensure_config (#3039)
Browse files Browse the repository at this point in the history
Fixes #2890
  • Loading branch information
vbarda authored Jan 15, 2025
1 parent c7e43f8 commit e2554c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions libs/langgraph/langgraph/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 (
Expand Down
12 changes: 12 additions & 0 deletions libs/langgraph/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({})

0 comments on commit e2554c9

Please sign in to comment.